古人智慧

Just Do it!
上士聞道,勤而行之;中士聞道,若存若亡;下士聞道,大笑之。不笑,不足以爲道。
~ 道德經 41

「實現夢想不是追逐成功,而是在於賦予生命意義,人生中的每個決定與聲音都有其重要含義。」"The key to realizing a dream is to focus not on success but on significance — and then even the small steps and little victories along your path will take on greater meaning."
電視名人-歐普拉·溫芙蕾(OPRAH WINFREY)

搜尋此網誌

Translation

2018年6月21日 星期四

OpenCV on MacOS 在MacOS安裝OpenCV開發

最近開始研究商用機器人,必須運用高階的開發工具來與AI做結合。
OpenCV,從維基百科全稱是Open Source Computer Vision Library,是一個跨平台的電腦視覺庫。
OpenCV可用於解決如下領域的問題:
  • 增強現實
  • 人臉識別
  • 手勢識別
  • 人機互動
  • 動作識別
  • 運動跟蹤
  • 物體識別
  • 圖像分割
  • 機器人
所以,學習OpenCV是有其必要性。

安裝Python開發

環境

MacOS 10.15.5 High Sierra
Python 3.6
OpenCV 3.4.1_5
先google前人的經驗,找到幾篇記錄下來:

Python 環境

參考Install OpenCV 3 on MacOS,建議用virtualenv建立一個虛擬pyhton3.6的環境,避免與macOS弄混。
  • 建立opencv虛擬python3.6環境,並安裝必要的package:
    virtualenv -p python3 opencv
    source opencv/bin/activate
    pip install numpy scipy matplotlib scikit-image scikit-learn ipython pandas jupyter
    deactivate
    
  • 用brew安裝opencv,個人是認為比較簡單的方式:
brew install opencv
或者之前已經安裝過舊版,用upgrade方式。
brew upgrade opencv
  • 環境變數設定:
echo /usr/local/opt/opencv/lib/python3.6/site-packages > /usr/local/lib/python3.6/site-packages/opencv3.pth
  • 進入到剛剛建立的opencv的virtualenv
cd ~/opencv/lib/python3.6/site-packages
ln -s /usr/local/opt/opencv/lib/python3.6/site-packages/cv2.cpython-36m-darwin.so cv2.so
  • 測試是否安裝完成:
source opencv/bin/activate
ipython
import cv2
print(cv2.__version__)

安裝Xcode開發

這部分有點複雜,分為command mode與xcode IDE模式。
參考這份文章,與這篇
  • command mode

先確定是否已經安裝 pkg-config
 * brew install pkg-config
  • 如果安裝順利,應該可以導出compiler需要的include、lib的參數:
pkg-config --cflags --libs opencv
  • 如同這麼多的參數:
-I/usr/local/Cellar/opencv/3.4.1_5/include/opencv -I/usr/local/Cellar/opencv/3.4.1_5/include -L/usr/local/Cellar/opencv/3.4.1_5/lib -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dpm -lopencv_face -lopencv_photo -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_optflow -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_dnn -lopencv_plot -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ml -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_flann -lopencv_xobjdetect -lopencv_imgcodecs -lopencv_objdetect -lopencv_xphoto -lopencv_imgproc -lopencv_core
  • 再來寫個簡單的code,main.cpp來測試,記得在desktop放個png:
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv) {
    Mat image;
    image = imread("/Users/regis/Desktop/aaa.png", 1);
    namedWindow("Display Image", WINDOW_AUTOSIZE);
    imshow("Display Image", image);
    waitKey(0);
    return 0;
}
  • build code的寫法:
    g++ $(pkg-config --cflags --libs opencv) -std=c++11  main.cpp -o test1
    
  • 執行:
    ./test1
    
  • Xcode IDE模式

這部分我搞了好一陣子,主要是卡在search path的地方,找了好久。。。

《include path》

《Lib path》
  • 再來就是把lib/.a抓進來,如果沒做這個動作,會build失敗。只要把.dylib.a抓進來即可。
  • 再把剛剛的main.cpp拷貝過來,build與run看看,應該會成功的!

接下來,就是要好好研究openCV的功能了!

2018年6月6日 星期三

[STM32F4x] OpenOCD install on MacOS

單片機MCU的價格不斷下降內含Flash與RAM,開發的工具也越來越成熟與穩定,隨著IOT興起推動MCU的熱潮!!!
手上剛好有一片STM32F407的核心板子,正好可以進入ARM 32bits MCU的世界。
STM32系列是很廣泛被使用在各領域的MCU,內含Cotex-M1到M7的ARM 32bits CPU,是值得深入學習的晶片系列。
在淘寶買到的核心板與Jtag
單片機MCU的價格不斷下降內含FlashRAM,開發的工具也越來越成熟與穩定,隨著IOT興起推動MCU的熱潮!!!
手上剛好有一片STM32F407的核心板子,正好可以進入ARM 32bits MCU的世界。
STM32系列是很廣泛被使用在各領域的MCU,內含Cotex-M1到M7的ARM 32bits CPU,是值得深入學習的晶片系列。

在淘寶買到的核心板與Jtag

<zu’zh

OpenOCD 安裝 installation


Regis-MacPro:~ regis$ brew update

Already up-to-date.

Regis-MacPro:~ regis$ brew install openocd
結果

==> Installing dependencies for open-ocd: libusb, libusb-compat, libftdi, hidapi

==> Installing open-ocd dependency: libusb

==> Downloading https://homebrew.bintray.com/bottles/libusb-1.0.22.high_sierra.b

######################################################################## 100.0%

==> Pouring libusb-1.0.22.high_sierra.bottle.tar.gz

🍺  /usr/local/Cellar/libusb/1.0.22: 29 files, 514.8KB

==> Installing open-ocd dependency: libusb-compat

==> Downloading https://homebrew.bintray.com/bottles/libusb-compat-0.1.5_1.high_

######################################################################## 100.0%

==> Pouring libusb-compat-0.1.5_1.high_sierra.bottle.tar.gz

🍺  /usr/local/Cellar/libusb-compat/0.1.5_1: 14 files, 94.3KB

==> Installing open-ocd dependency: libftdi

==> Downloading https://homebrew.bintray.com/bottles/libftdi-1.4.high_sierra.bot

######################################################################## 100.0%

==> Pouring libftdi-1.4.high_sierra.bottle.1.tar.gz

🍺  /usr/local/Cellar/libftdi/1.4: 19 files, 164.3KB

==> Installing open-ocd dependency: hidapi

==> Downloading https://homebrew.bintray.com/bottles/hidapi-0.8.0-rc1.high_sierr

######################################################################## 100.0%

==> Pouring hidapi-0.8.0-rc1.high_sierra.bottle.2.tar.gz

🍺  /usr/local/Cellar/hidapi/0.8.0-rc1: 17 files, 131.2KB

==> Installing open-ocd

==> Downloading https://homebrew.bintray.com/bottles/open-ocd-0.10.0.high_sierra

######################################################################## 100.0%

==> Pouring open-ocd-0.10.0.high_sierra.bottle.1.tar.gz

🍺  /usr/local/Cellar/open-ocd/0.10.0: 632 files, 4.7MB
首先,尋找interface\是否有J-Link的設定檔。但是,如何找到這個interface\的folder?

sudo find / |grep interface/jlink.cfg
出現在這裡!!!

/usr/local/Cellar/open-ocd/0.10.0/share/openocd/scripts/interface/jlink.cfg
再來就下命令來連上STM32F4核心板

Regis-MacPro:50_openocd regis$ openocd -f interface/jlink.cfg -f target/stm32f4x.cfg
結果成功透過J-Link連上STM32F4核心板

Open On-Chip Debugger 0.10.0

Licensed under GNU GPL v2

For bug reports, read

 http://openocd.org/doc/doxygen/bugs.html

Info : auto-selecting first available session transport "jtag". To override use 'transport select <transport>'.

adapter speed: 2000 kHz

adapter_nsrst_delay: 100

jtag_ntrst_delay: 100

none separate

cortex_m reset_config sysresetreq

Info : No device selected, using first device.

Info : J-Link V9 compiled Jun  2 2222 22:22:22

Info : Hardware version: 9.40

Info : VTarget = 3.192 V

Info : clock speed 2000 kHz

Info : JTAG tap: stm32f4x.cpu tap/device found: 0x4ba00477 (mfg: 0x23b (ARM Ltd.), part: 0xba00, ver: 0x4)

Info : JTAG tap: stm32f4x.bs tap/device found: 0x06413041 (mfg: 0x020 (STMicroelectronics), part: 0x6413, ver: 0x0)

Info : stm32f4x.cpu: hardware has 6 breakpoints, 4 watchpoints
到這裡就成功把OpenOCD執行,並開通port 4444接收telnet來連線執行命令。

測試 Test

打開另一個terminal終端,輸入以下telnet指令

telnet localhost 4444
出現Open On-Chip Debugger,表示連上

Trying ::1...

telnet: connect to address ::1: Connection refused

Trying 127.0.0.1...

Connected to localhost.

Escape character is '^]'.

Open On-Chip Debugger

> poll

background polling: on

TAP: stm32f4x.cpu (enabled)
help指令了解更多
> help
adapter_khz [khz]
      With an argument, change to the specified maximum jtag speed.  For
      JTAG, 0 KHz signifies adaptive  clocking. With or without argument,
      display current setting. (command valid any time)
adapter_name
      Returns the name of the currently selected adapter (driver) (command
      valid any time)
adapter_nsrst_assert_width [milliseconds]
      delay after asserting SRST in ms (command valid any time)
adapter_nsrst_delay [milliseconds]
      delay after deasserting SRST in ms (command valid any time)
add_help_text command_name helptext_string
      Add new command help text; Command can be multiple tokens. (command
      valid any time)
add_script_search_dir <directory>
      dir to search for config files and scripts (command valid any time)
add_usage_text command_name usage_string
      Add new command usage text; command can be multiple tokens. (command
      valid any time)
arm
      ARM command group (command valid any time)
  arm core_state ['arm'|'thumb']
        display/change ARM core state
  arm disassemble address [count ['thumb']]
        disassemble instructions 
  arm mcr cpnum op1 CRn CRm op2 value
        write coprocessor register
  arm mrc cpnum op1 CRn CRm op2
        read coprocessor register
  arm reg
        display ARM core registers
  arm semihosting ['enable'|'disable']
        activate support for semihosting operations
  arm semihosting_fileio ['enable'|'disable']
        activate support for semihosting fileio operations
array2mem arrayname bitwidth address count
      convert a TCL array to memory locations and write the 8/16/32 bit
      values
bindto [name]
      Specify address by name on which to listen for incoming TCP/IP
      connections (command valid any time)
bp <address> [<asid>]<length> ['hw'|'hw_ctx']
      list or set hardware or software breakpoint
command
      core command group (introspection) (command valid any time)
  command mode [command_name ...]
        Returns the command modes allowed by a  command:'any', 'config', or
        'exec'.  If no command isspecified, returns the current command
        mode.  Returns 'unknown' if an unknown command is given. Command
        can be multiple tokens. (command valid any time)
  command type command_name [...]
        Returns the type of built-in command:'native', 'simple', 'group',
        or 'unknown'. Command can be multiple tokens. (command valid any
        time)
cortex_m
      Cortex-M command group
  cortex_m maskisr ['auto'|'on'|'off']
        mask cortex_m interrupts
  cortex_m reset_config ['srst'|'sysresetreq'|'vectreset']
        configure software reset handling (command valid any time)
  cortex_m vector_catch ['all'|'none'|('bus_err'|'chk_err'|...)*]
        configure hardware vectors to trigger debug entry
dap
      DAP command group
  dap apcsw [sprot]
        Set csw access bit 
  dap apid [ap_num]
        return ID register from AP (default currently selected AP)
  dap apreg ap_num reg [value]
        read/write a register from AP (reg is byte address of a word
        register, like 0 4 8...)
  dap apsel [ap_num]
        Set the currently selected AP (default 0) and display the result
  dap baseaddr [ap_num]
        return debug base address from MEM-AP (default currently selected
        AP)
  dap info [ap_num]
        display ROM table for MEM-AP (default currently selected AP)
  dap memaccess [cycles]
        set/get number of extra tck for MEM-AP memory bus access [0-255]
  dap ti_be_32_quirks [enable]
        set/get quirks mode for TI TMS450/TMS570 processors (configuration
        command)
debug_level number
      Sets the verbosity level of debugging output. 0 shows errors only; 1
      adds warnings; 2 (default) adds other info; 3 adds debugging.
      (command valid any time)
drscan tap_name [num_bits value]* ['-endstate' state_name]
      Execute Data Register (DR) scan for one TAP.  Other TAPs must be in
      BYPASS mode.
dump_image filename address size
echo [-n] string
      Logs a message at "user" priority. Output message to stdout. Option
      "-n" suppresses trailing newline (command valid any time)
exit
      exit telnet session
fast_load
      loads active fast load image to current target - mainly for profiling
      purposes
fast_load_image filename address ['bin'|'ihex'|'elf'|'s19'] [min_address [max_length]]
      Load image into server memory for later use by fast_load; primarily
      for profiling (command valid any time)
find <file>
      print full path to file according to OpenOCD search rules (command
      valid any time)
flash
      NOR flash command group (command valid any time)
  flash bank bank_id driver_name base_address size_bytes chip_width_bytes
            bus_width_bytes target [driver_options ...]
        Define a new bank with the given name, using the specified NOR
        flash driver. (configuration command)
  flash banks
        Display table with information about flash banks. (command valid
        any time)
  flash erase_address ['pad'] ['unlock'] address length
        Erase flash sectors starting at address and continuing for length
        bytes.  If 'pad' is specified, data outside that range may also be
        erased: the start address may be decreased, and length increased,
        so that all of the first and last sectors are erased. If 'unlock'
        is specified, then the flash is unprotected before erasing.
  flash erase_check bank_id
先到這裡,下次再研究深入些。