2013年12月30日 星期一

Raspicam Code Review

*** This Note is the Code Review of Raspicam Video ***
RASPIVID_STATE

PORT_USERDATA : 



MMAL_COMPONENT_T
MMAL_CONNECTION_T
MMAL_POOL_T In void main() :

Initialization Section
  • RASPIVID_STATE  state // is data storage vessel ,資料都從這裡存取
  • MMAL_STATUS_T 
  • MMAL_PORT_T
    • camera_video_port (動態)
    • camera_still_port    (靜態)
    • preview_input_port
    • encoder 
      • encoder_input_port
      • encoder_output_port
signal(SIGINT, singnal_hanlder)
default_status( &state );
  • h = state.height
  • w = state.width
Initiailize window and OpenCV stuff

dstImage = cvCreateImage( cvSize(w,h), IPL_DEPTH_8U, 3) // 3 Channels
  • py, pu, pv        : py 大小 w*h, pu, pv 大小只有 w/2 * h/2 
  • pu_big, pv_big : big u, v 將上面的長寬統一,變為 w * h
由於編碼格式的關係 (planar format - I420),所以所以u, v長度較短,在組合成影像時必須經過內插。

  • 以YUV 4:2:0的方式記錄。 
  • YUV4:2:0並不是說只有U(即Cb), V(即Cr)一定為0,而是指U:V互相援引,時見時隱,也就是說對於每一個行,只有一個U或者V份量,如果一行是4:2:0的話,下一行就是4:0:2,再下一行是4:2:0...以此類推。 

為節省頻寬起見,大多數YUV格式平均使用的每像素位數都少於24位元。
主要的抽樣(subsample)格式有YCbCr 4:2:0、YCbCr 4:2:2、YCbCr 4:1:1和YCbCr 4:4:4。

YUV的表示法稱為A:B:C表示法:
  • 4:4:4表示完全取樣。
  • 4:2:2表示2:1的水平取樣,沒有垂直下採樣。
  • 4:2:0表示2:1的水平取樣,2:1的垂直下採樣。
  • 4:1:1表示4:1的水平取樣,沒有垂直下採樣。
Create Camera

create_camera_component( &state );
重要的  function ,裡面包括了mmal的初始化以及 video_buffer 的 callback function.

raspipreview_create( &state.preview_parameters ) ;

  • if not success : destroy_camera_component( &state )

PORT_USERDATA callback_data

camera_video_port
state.camera_component -> output[MMAL_CAMERA_VIDEO_PORT]; 

callback_data.pstate = &state

VCOS_STATUS_T vcos_status; // what is VCOS?
vcos_status = vcos_semaphore_create( &callback_data.complete_semaphore..);
vcos_assert(...);

這裡將來自camera 的 data pointer 指派給 user 讓 callback function 可以使用
camera_video_port -> userdata = 
(struct MMAL_PORT_USERDATA_T*) & callback_data;

 接下來的事情比較看不懂了,分別是

  • 初始化 timer
  • 開始抓取
  • 將所有的 buffer 送到 video port
  • wait until need to stop
  • disable all connections
  • destroy_encoder_components 
  • cvReleaseImage(IplImages)
之後仔細閱讀在詳述,現在應該把焦點放到 callback function 以及如何加速


In MMAL_COMPONENT_T *crate_camera_component( *state) : 

MMAL_ES_FORMAT_T *format
MMAL_COMPONENT_T *camera;
MMAL_PORT_T *video_port, *still_port, *preview_port; 

function 的流程為 :

  • Create camera component 
    • mmal_create_component(DEFAULT_CAMREA, &camera)
  • Set up camera configuration
    • 可以注意這種config設定的寫法
    • stills_yuv422 = 0; 暗示靜態有另一種編碼?
    • num_preview_video_frame = 3; (preview_video的意思待確定)

  • Set the encode format on the video port
    • format = video_port -> format;
  • Plug the callback to the video port
    • mmal_port_enable(video_port, video_buffer_callback); 
    • then ensure there is enough buffers to avoid dropping frames

  • Set the encode format on the still port 
    • Is still port necessary for video?
  • Create pool of message on video port
    • What is pool ?
  • Enable component

It seems that MMAL is the boardcom wrapper over OpenMAX. (Ref:Stack overflow

MMAL API runs over OpenMAX = Media Acceleration (中文/英文)
程式碼可以在Pi提供的userland中找到。
OpenMAX 的 簡介,他包含三層
  • DL (Development Layer) 
    • OpenMAX AL是多媒體應用程式(如Media Player)和平台媒體框架之間的介面。它允許公司開發應用程序方便地移植到不同平台的應用程序(客戶),並支持OpenMAX AL API。 
  • IL (Integration Layer) 
    • OpenMAX IL API透過C語言致力於打造可移植的媒體組件的陣列的平台。 
    • 這些組件可以是來源(source)、匯出(sink)、編解碼器(codec)、過濾器(filter)、分離器(splitter)、混頻器(mixers),或任何其他資料操作。
  • AL (Application Layer)
    • AL功能包括 視頻播放和錄製
    • 音頻播放和錄製
    • 圖像捕捉(攝像機)並顯示
    • 攝影控制
    • 廣播和RDS
    • 基本MIDI播放
    • 元數據(Metadata)的提取和插入

In video_buffer_callback(MMAL_PORT_T *port,                        MMAL_BUFFER_HEADER_T *buffer) : 

PORT_USERDATA *pData = (PORT_USERDATA*) port -> userdata; 

防止錯誤
...  if (pData) 
...  if (buffer -> length)

mmal_buffer_header_mem_lock( buffer ); 

不是很了解Memory lock再做什麼,但常常看到,似乎是重要的技巧。(待查

OpenCV  here
w = pData-> pstate->width;
h = pData-> pstate->height;
h4 = h >> 2;  // h= h/4 

接下來透過讀取YUV,得到影像,分成兩種情況 (determined by graymode in RASPIVID_STATE)

  • 如果要得到Color map,YUV三者接需要
  • 如果只要用到gray image ,計算Y值即可

mmal_buffer_header_mem_unlock( buffer ); 

There are two algorithm that we want implement on my Pi.

  • Template Matching
  • Optical flow 
為了實踐這兩種演算法我想用ARM提供的NEON SIMD來加速。

在Github上找到了neon的(浮點)加速測試範例 :vfp-test
浮點運算也很重要,因為optical flow將會用到)
但是可以看得出來,Pi 會產生錯誤訊息 :failed to run and generated an Illegal instruction exception.



Reference : 
補充知識 :

YUV format (Luminance, Chrominance, Chroma)
  • 緊縮格式(packed formats):將Y、U、V值儲存成Macro Pixels陣列,和RGB的存放方式類似。 
  • 平面格式(planar formats):將Y、U、V的三個份量分別存放在不同的矩陣中,Raspicam用的是這種方式。每Y份量,U份量和V份量都是以獨立的平面組織的,也就是說所有的U份量必須在Y分量後面,而V份量在所有的U份量後面,此一格式適用於採樣(subsample)。









2013年12月24日 星期二

Days of my Pi (Useful Web Source of Pi programming)

Some information, good websites about Raspberry Pi


Adafruit Learning System

Basics
OpenCV 

SIMD on Pi
Stack overflow Articles 
Core of Pi
Architecture of Pi



Visual Servo Project

影像伺服開發日誌

目標:用攝影機定位輸送帶上的物件,當偵測正確的物品時,利用機械手臂夾持。


硬體設備



  • Raspberry Pi model B 
  • Raspi camera 
  • 機械手臂
預算
  • 六自由度舵機支架含底盤 - 3738 NTD
  • 32路舵機控制板 + 五個伺服馬達(舵機)- 2426 NTD





---------------------------------------------------------------


日期:2013 / 12 / 23 


由於32路舵機控制器需要用到UART下達指令,我們先安裝 bcm2835 的 C library for Raspberry Pi方便 Pi 週邊的操作。

      Refer to C library for Broadcom BCM 2835 as used in Raspberry Pi








今天最後卡在不知道怎麼用PI上的GPIO送出UART訊號,由Console接收。帶明天解決。

<Note>
How about using stm32f4 for servo motor controller ?
Use pwm to control servo, and read UART as commands. Let can be better.
Need a servo motors' power controller (board)

Reference :




---------------------------------------------------------------

2013 / 12 / 25 :  聖誕快樂XD

Today I face the mmal_vc_port_info_set failed ERROR Q_Q

Disappointing for setting raspicam. The problem should be pending and i need dealing with UART stuff.

About GPIO


Refer to GPIO functions 


Well, I get something without using bcm2835 but using <termios.h>


Reference : 

----------------------------------------------------------------------------------------------------------------------

2013/12/26 


舵機測試



  • 電壓:1.5 V * 4 = 6 V
  • 尚未接 PWM,之後量測Duty cycle與角度關係是否準確


Specs : MG995 Servo
  • Weight  : 55g 
  • Stall torque 8.5kg/cm (4.8V) / 10kg/cm (6V)
  • Operating voltage : 4.8 ~ 7.2 V
  • Dead bandwidth : 5 us
  • 金屬齒輪塑膠外殼


手臂組裝過程
這次買的跺機組與支架品質不太好,賣家似乎也不太專業:(




目前組裝為兩個自由度,再加上一個夾持共三個馬達。
底座不能旋轉,因為實際物品跟拍賣圖上不一樣 Orz
這個底座台沒辦法妥當安置Base Servo。



接下來要想辦法從Raspberry Pi 控制手臂,送UART命令到
  • 32 Channels Servo Controller 
  • STM32F4 Discovery
接下來得了解32路控制器。Tomorrow I will focus on servo control with PWM.

----------------------------------------------------------------------------------------------------------------------

2013 / 12 / 30

Well, after two days break, time to get back to the track XD

今天量測了PD12送出的PWM 是否符合預期


  • Period :50 Hz ( 20ms )
  • Duty Cycle :5% ~ 10 %

讀到Counter的數值也符合預期,應該可以直接用在伺服馬達控制上。


開始 trace RaspiCam + OpenCV的程式。
-----------------------------------------------------------------------------------------

Raspiberry Pi camera 用的是ov5647晶片,參考Specs連結

Code Trace 的部份正在進行,不過內容太多了寫到另外一篇中,請參考 raspicam-code-review

2014 / 1 / 1 
--------------------------------------------------------------------------------------
在把Code 中的 imshow() 關掉後可以得到 30 fps 的結果(640 * 480 ),這樣的速度還算不錯,不過要進行運算的時候還是需要硬體上的加速,接著得研究ARM提供的NEON了。

Reference : 

Maybe it should be halted!!

< Robot Arm Controller Note > 
In main: serial_msg rx_msg; 
In sys_manager: xQueueHandle serial_rx_queue;
In main : serial_rx_queue = xQueueCreate(1, sizeof(serial_msg));

今天改裝了手臂,買了小零件讓底盤可以轉動


小盤有四個螺絲孔,順利的鎖上支架,成為手臂裡最牢固的一個零件XD


為了讓中柱可以穩固,中間加上了墊片,鎖到伺服馬達的中心上。

側視圖


目前手臂的外觀
接下來需要一個配電的電路板(可能來不及作穩流電路了),所以選用電池時先不考慮鋰電池了。

It is probably useful : Intro of OpenVG


2014 / 1 / 3 
--------------------------------------------------------------------------------------
將收到的 usart message 堆在 queue 中,接著透過這個函式送回來。
xQueueSendToBackFromISR()

2014 / 1 / 10
--------------------------------------------------------------------------------------




代辦事項
  • 測試相機修改一些多餘的程式碼,希望可以讓 640 * 480 大小下達到 30 fps 的水準
  • 研究NEON,利用SIMD加速運算
  • 經過 Macron 的提醒,可以嘗試開啟GPU,加速影像方面的計算
    • 他說可以開到1080p彩圖,資源消耗 ~ 30%
    • 將Raspbian 中不必要的程式刪除,最後留下(~20KB,不太記得了)的網路與影像套件
  • 試著加入Optical flow ,一方面測試利一方面作為之後專題使用
  • 完成 STM32F4 的 USART 接收功能(接近完成,希望能加上RTOS的Queue)
  • 海報照片和圖片
  • USB update
    • 怎麼量測封包與電源??@@
    • 查詢 550 mA 的電流上限


2013年12月17日 星期二

ASCII Camera Project

Hardware 

  • STM32F407  Discovery
  • OV7670 w/o fifo
  • Memory stick - Fat32 
Target 


Development Logs

previous note, write a text file in the portable disk.




2013年12月9日 星期一

Road detector?

 I read a paper today for more understanding of SVM assignment. It is Road Detection Using Support Vector Machine based on Online Learning and Evaluation.

It is a great topic for me! So pity that I found it too late. Road detector is a great device that combine all the knowledge I learned in this semester.

  • Machine Learning 
  • Machine Vision 
  • Embedded System

(Previous idea is tracker or obstacle avoidance but there are high technical barrier)
Maybe i can jump into it?