マイコン x Linux のお勉強
やりたいこと
UbuntuやTX2からマイコンを操作し、いろいろなデバイスを動かしたい
TX2での準備
以下を実行し、ACMを使えるようにする必要がある
Build Kernel and ttyACM Module – NVIDIA Jetson TX2 | JetsonHacks
Arduinoの設定
UbuntuにArduino IDE(1.8.5)インストール | ねこめも を参考に64bitをダウンロード
sudo chmod a+rw /dev/ttyACM0
ビルド -> arduinoに転送
ToDo: /dev/ttyACM0に権限をつけておく方法調査
mbedの設定
OSにあまり関係しない
ブラウザで開発
ビルドでできたバイナリをmbedフォルダに移動
マイコンとの通信
準備
- pip install pyserial
PC側実装
標準入力のコマンドをマイコンに送る
注意!
serialのopen()してからtime.sleep(2.0)のように2秒以上いれないと通信できないことがあった! Debugの時は念の為長くとっておこう。
python3では文字のエンコードがpython2と異なるため、serial writeする時に<送りたい文字列>.encode()とする必要がある。
#!/usr/bin/env python # coding: utf-8 import serial import time if __name__ == '__main__': print "hello serial!" ser = serial.Serial() ser.port = '/dev/ttyACM0' ser.baudrate = 9600 ser.open() time.sleep(2.0) while(1): position = bytes(raw_input()) ser.write(position) # for Debug read_ser = ser.readline().rstrip() print read_ser
Arduino側実装
#include "enum.h" const char RX_FOOT = ';'; void setup() { // settings Serial.begin(9600); Serial.setTimeout(1500); } void loop() { // シリアル受信 if(Serial.available() > 0) { // 受信しきるまでのバッファ時間 delay(5); // データ受信 String buf = Serial.readStringUntil(RX_FOOT); Serial.println(buf); } delay(1); }
mbed側実装
#include "mbed.h" #include "MODSERIAL.h" #include <list> #include <string> #include <cstdlib> int main() { MODSERIAL pc(USBTX, USBRX, 8192); pc.baud(9600); char data[1100]; int data_index = 0; while(1) { if (!pc.readable()) continue; char c = pc.getc(); data[data_index++] = c; if (c == '\n') { data[data_index] = '\0'; string command = data; pc.puts(command.c_str()); data_index = 0; } }