FBTUG-FarmBot 韌體設計
韌體
- Arduino Related : Overview, commands, PINs
- Software Overview
- Command line flash tool installation安裝在rpi中編譯和下載ino到arduino的工具
- sudo apt-get install arduino gcc-avr avr-libc avrdude python-configobj python-jinja2 python-serial
- mkdir tmp
- cd tmp
- git clone https://github.com/miracle2k/python-glob2
- cd python-glob2
- wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python
- sudo python setup.py install
- git clone git://github.com/amperka/ino.git
- cd ino
- sudo make install
- 其中arduino 是arduino環境
- gcc-avr,avr-libc 是arduino mcu的編譯器和lib
- avrdude 是 debug和燒錄的工具
- ConfigObj - a Python module for easy reading and writing of config files.
- Jinja2 is a full featured template engine for Python.
- python-serial Python serial port access library
command line flash tool use farmbot程式碼下載和上傳
NOTE: We tag releases when they are stable. The latest version (on master) is not guaranteed to be stable.
See releases to find a stable release.
OPTION A: For less stable "edge" version: 方法一,下載穩最新的版本
- git clone https://github.com/FarmBot/farmbot-arduino-firmware
OPTION B: For stable release 1.0:方法,下載穩定的 1.0版本
- git clone -b ’alpha-1.0’ --single-branch https://github.com/FarmBot/farmbot-arduino-firmware
To flash the firmware onto the device, run this: 編繹和上傳
- cd farmbot-arduino-firmware
- ino build
- ino upload
整體的系統包含了 setup和 main,以上是main的架構
- +--------------------------+
- |farmbot_arduino_controller| farmbot的主要控制核心
- +-----------+--------------+
- v
- +--------------------------+
- |Command | 指令接收層
- +-----------+--------------+
- v
- +--------------------------+
- |GCodeProcessor | gcode的處理(辨識g-code交由對應的handler執行)
- +-----------+--------------+
- v
- +--------------------------+
- |***Handler | 執行g-code的handler有好幾隻
- +-------+-----------+------+
- | |
- | +---+
- v v
- +--------------+ +-----------+
- |StepperControl| | PinControl| 包含馬達的控制和io的讀取
- +--------------+ +-----------+
- 目前的接腳請參考 腳位表
- 目前能接收的g-code請參考 g-code表
看了一下,控制水閥的程式 F01(用時間控制水量)和F02(用流速器控制水量),發現目前都沒有對相應的程式
跟據這個圖
發現,他用D9來控制水閥,但並沒有偵測流速器的地方
目前PINS.H中,9是用來控制風扇,10和8是用來控制加熱器(跟本是3DP的用途啊)
所以原團隊應該還沒處理這一個部份
加入RAMPS 1.4 詳細Pin 腳圖示, 方便參考-
Servo controll
根據 官方命令列表 , Pin 4 & 5 可以控制Servo 的角度
SERVO_0_PIN |
4 |
Servo motor 0 signal pin |
FarmBot |
SERVO_1_PIN |
5 |
Servo motor 1 signal pin |
FarmBot |
Pin 4 & 5 Servo 的code, 目前有些code 是被remark 掉的, 加回測試後可以使用.
//pinMode(SERVO_0_PIN , OUTPUT);
//pinMode(SERVO_1_PIN , OUTPUT);
Command 格式:
"F61 P4 V45\n" -- Pin4 的 servo 轉45度
Water Flow Sensor
目前Arduino MEGA 2560 共有6 個外部中斷可以使用, 加入 Pin20 給water flow sensor 使用.
2 (interrupt 0) |
X_MIN_PIN |
3 |
X axis end stop at home position |
Farmbot |
3 (interrupt 1) |
X_MAX_PIN |
2 |
X axis end stop at far position |
Farmbot |
18 (interrupt 5) |
Z_MIN_PIN |
18 |
Z axis end stop at home position |
Farmbot |
19 (interrupt 4) |
Z_MAX_PIN |
19 |
Z axis end stop at far position |
Farmbot |
20 (interrupt 3) |
WATER_FLOW_PIN |
20 |
Water Flow Sensor |
FBTUS |
21 (interrupt 2) |
RESERVED |
21 |
Reserved |
|
測試代碼:
volatile double waterFlow;
void setup() {
Serial.begin(115200); //baudrate
waterFlow = 0;
attachInterrupt(3, pulse, RISING); //DIGITAL Pin 20: Interrupt 3
}
void loop() {
Serial.print("waterFlow:");
Serial.print(waterFlow*1000);
Serial.println(" mL");
delay(500);
}
void pulse() //measure the quantity of square wave
{
waterFlow += 1.0 / 5880.0;
}
測試code 的原理:
Water flow sensor 每 1L trigger 5880 次的方波.---> 請參考各廠牌的spec
使用attachInterrupt() 的正緣觸發(RISING), 每次都發interrupt 到pulse() 來計算流量.
Source Code Explaination
- farmbot_arduino_controller.cpp 一切的程式由此開始
- setup()
- pin_setup
- 依據 pins.h 去設定各個週邊的腳位,如果未來有加入自已的硬體,也需要在此加入
- // Dump all values to the serial interface
- timer attach interrupt and start
- loop()
- parsing commands from serial input
- gCodeProcessor->execute(command);
- GCodeProcessor.cpp
- GCodeHandler* handler = getGCodeHandler(command->getCodeEnum());
- Command.cpp
const char axisCodes[3] = { ’X’, ’Y’, ’Z’ };
const char axisSpeedCodes[3] = { ’A’, ’B’, ’C’ };
const char speedCode = ’S’;
const char parameterIdCode = ’P’;
const char parameterValueCode = ’V’;
const char parameterValue2Code= ’W’;
const char elementCode = ’E’;
const char timeCode = ’T’;
const char modeCode = ’M’;
const char msgQueueCode = ’Q’;
Study - FarmBot OS Build Process
Problem Fixed
- Elixir need installed (Default apt version too old)
- hex need manual installed
- NERVES_TARGET need to setup as rpi3
- # these bring-up experience include many try and error, the sequence of environment setup maybe out of order or loss some of the necessary detail. The purpose to log these is to save another try and error that have been done.
- # environment Linux 16.10 VirtualBox image. http://www.osboxes.org/ubuntu/ , Ubuntu 16.10 Yakkety Yak (Final)
- # install erlang, elixir to get mix work
- wget https://packages.erlang-solutions.com/erlang-solutions_1.0_all.deb && sudo dpkg -i erlang-solutions_1.0_all.deb
- sudo apt-get update
- sudo apt-get install esl-erlang
- sudo apt-get install elixir
- # basic build environment setup
- sudo apt install git
- sudo apt install build-essential
- sudo apt install erlang-dev
- sudo apt install libmnl-dev
- sudo apt install ssh-askpass
- #install ellixir package manager and utility
- mix local.hex
- mix local.rebar
- #expected some nerves bootstrap
- mix archive.install https://github.com/nerves-project/archives/raw/master/nerves_bootstrap.ez
- #need fwup environment
- wget https://github.com/fhunleth/fwup/releases/download/v0.12.0/fwup_0.12.0_amd64.deb
- sudo dpkg -i fwup_0.12.0_amd64.deb
- export MIX_ENV=prod
- export NERVES_TARGET=rpi3
- mix firmware
Makefile Description: tree information to show dependences and tasks
- release-rpi3: scripts/build_release_images.sh rpi3 3.0.1
- rpi3:
- env-rpi3: export NERVES_TARGET=rpi3
- prod_env: export MIX_ENV=prod
- system-rpi3: scripts/build_system.sh rpi3
- create-build-rpi3: scripts/clone_system.sh rpi3
- firmware-rpi3: scripts/build_firmware.sh rpi3
Study - Arduino + RAMPS
BuildUp Steps
- Drive RAMPS test code from wiki to check hardware
- Compile and upload current Arduino firmware(need mark //motorEnabled = false;)
Study - RPI/Arduino/RAMPS/Web
- Using Web to control motors - (Firmware 3.0.2)
- Web function check ( control checked, sequence test ok, upgrade arduino OK)
Arduino 命令介面驗證
Arduino 透過 USB線(實際上是 UART), 透過定義好的命令來操控。
改裝與 debug 的需求,常會需要直接針對 Arduino 這部分來確認。
通訊介面: UART 115200 N81
命令基本上是以換行(\n, \r\n 不行)來切開,欄位符號為空白
請參考官方命令列表,以下為簡單運行範例。"" 內為輸入,其他為輸出,\n 為換行
"F83\n" => Report software version
R01
R83 GENESIS V.01.04
R02
"F82\n" => Report current position
R01
R82 X0 Y0 Z0
R02
"F21 P2\n" => Read parameter P2
R01
R21 P2 V0X Y Z S
R02
R00
"G00 X2 Y2 Z2\n" => Move to location at given speed
R01
R81 XA0 XB0 YA0 YB0 ZA0 ZB0
R99 stopped
R81 XA0 XB0 YA0 YB0 ZA0 ZB0
R82 X2 Y2 Z0
R02
R00
宜蘭深溝目前安裝 firmware版本
git commit hash : a6028a0 (其實就是目前最新版本)