Skip to content

Commit 1a13681

Browse files
authored
Merge pull request #14 from TNCT-Mechatech/release-v1.0.0beta
Release v1.0.0beta
2 parents 98bba6a + 4730b4b commit 1a13681

29 files changed

+2088
-2
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright [yyyy] [name of copyright owner]
189+
Copyright 2021 ToyamaNCT-Mechatech
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.

README.md

Lines changed: 277 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,277 @@
1-
# SerialBridge
1+
# SerialBridge
2+
## 概要
3+
本ライブラリは、PC(Linux)-MCU(Arduino/Mbed)間でUARTによるシリアル通信を行うためのライブラリです。
4+
用途に合わせて、簡単にパケットデータを割り当てることができる仕組みをとっています。
5+
本ライブラリは、クロスプラットフォーム化のために非STL依存のC++及びC言語によって記述されています。
6+
7+
## リリースバージョン
8+
9+
* **1.0.0-beta**
10+
11+
## 導入
12+
### Linux
13+
* プロジェクトでgit管理を既に用いている場合
14+
submoduleとして利用します
15+
1. プロジェクトディレクトリに移動します
16+
2. terminalで以下のコマンドを実行しsubmoduleとしてクローンします
17+
```shell
18+
git submodule add https://github.com/TNCT-Mechatech/SerialBridge SerialBridge
19+
echo 'commit'
20+
git add SerialBridge
21+
git commit -m 'Add SerialBridge as a module'
22+
```
23+
* プロジェクトにgitを利用していない場合
24+
1. プロジェクトディレクトリに移動します
25+
2. terminalで以下のコマンドを実行しレポジトリをクローンします
26+
```
27+
git clone https://github.com/TNCT-Mechatech/SerialBridge
28+
```
29+
* 最後にmake allをして静的ライブラリを生成して、リンクさせます
30+
1. SerialBridgeディレクトリに移動します
31+
2. makeコマンド(下記)により、静的ライブラリを生成します
32+
```shell
33+
make all
34+
```
35+
3. ビルド時
36+
* CMakeを用いる場合は、CMakeLists.txtに以下のコードを追加
37+
```cmake
38+
target_link_libraries(YOUR_PROJECT_NAME ${CMAKE_SOURCE_DIR}/SerialBridge/bin/libSerialBridge.a)
39+
```
40+
* gccから直接ビルドする場合は、ターゲットと同じディレクトリ内で次のようにコマンドを実行
41+
```shell
42+
g++ (target file).cpp (your directory path)/SerialBridge/bin/libSerialBridge.a \
43+
-I (your directory path)/SerialBridge/src
44+
```
45+
`(target file)`: ビルドターゲットのファイル名
46+
`(your directory path)`: 本ライブラリをクローンしたディレクトリパス
47+
48+
49+
### Arduino
50+
1. librariesに移動します
51+
2. terminalで以下のコマンドを実行しレポジトリをクローンします
52+
```
53+
git clone https://github.com/TNCT-Mechatech/SerialBridge
54+
```
55+
56+
### Mbed
57+
**[Online Compiler](https://ide.mbed.com/)での開発のみ動作確認されています**
58+
1. 任意のプロジェクトを作成します
59+
2. レポジトリをプログラムにクローンします
60+
[任意のプロジェクトを右クリック] -> [ライブラリのインポート] -> [URLからインポート] ->
61+
[Source URLに[レポジトリのURL](https://github.com/TNCT-Mechatech/SerialBridge/)を入力] -> [Import AsをLibrary] -> [Import]
62+
※必要に応じてリビジョンから任意のコミットをマージしてください
63+
64+
## 使用方法
65+
### 初期化
66+
67+
初期化手順のみが各環境に依存します。
68+
`SerialDev`に渡されるシリアルデバイスのボーレートはデバイス間で同じ速度である必要があります。
69+
またフォーマットは、8データビット、ノンパリティが推奨されます。
70+
71+
#### **Linux**
72+
```c++
73+
#include <SerialBridge.hpp>
74+
// serial driver for linux/ros
75+
#include <LinuxHardwareSerial.hpp>
76+
77+
SerialDev *dev = new LinuxHardwareSerial("/dev/ttyUSB0", B9600);
78+
SerialBridge serial(dev);
79+
```
80+
Serialパスはデバイス接続時に自動生成されるシンボリックリンクも使用できます。
81+
このリンクはUSBの接続位置によって固有になるので、複数の同じデバイスをつないでも識別ができるようになります。
82+
以下に示す例のように、ボードがどのUSBポートに接続されているか確認できます。
83+
```shell
84+
$ ls /dev/serial/by-path/
85+
pci-0000:00:1a.0-usb-0:1:1.0-port0 //右側USBポート
86+
pci-0000:00:1d.0-usb-0:2:1.0-port0 //左側手前
87+
pci-0000:00:1d.0-usb-0:1:1.0-port0 //左側奥
88+
pci-0000:00:1d.7-usb-0:1.4:1.0-port0 //左側奥+USBハブ
89+
pci-0000:00:1d.7-usb-0:1.4.1:1.0-port0 //左側奥+USBハブ+USBハブ
90+
```
91+
とはいえ可視性が悪いので1個ずつシリアルデバイスを接続して確かめることをおすすめします。
92+
利用する際はマクロ定義で置き換えると引数部分がすっきりする。
93+
ex) 右側USBポートを使用する場合
94+
```c++
95+
#define SERIAL_PATH "/dev/serial/by-path/pci-0000:00:1a.0-usb-0:1:1.0-port0"
96+
SerialDev *dev = new LinuxHardwareSerial(SERIAL_PATH, B9600);
97+
```
98+
99+
#### **Arduino**
100+
```c++
101+
#include <SerialBridge.hpp>
102+
// serial driver for arduino
103+
#include <InoHardwareSerial.hpp>
104+
105+
// Pass the serial derived class entity for Arduino to SerialDev.
106+
SerialDev *dev = new InoHardwareSerial(&Serial);
107+
SerialBridge serial(dev);
108+
109+
void setup()
110+
{
111+
Serial.begin(9600);
112+
}
113+
114+
```
115+
116+
#### **Mbed**
117+
* Mbed-os2(Nucleo F303K8, etc.)の場合
118+
```c++
119+
#include <SerialBridge.hpp>
120+
#include <MbedHardwareSerial.hpp>
121+
122+
SerialDev *dev = new MbedHardwareSerial(new Serial(USBTX, USBRX, 9600));
123+
SerialBridge serial(dev);
124+
```
125+
126+
* Mbed-os5,6の場合
127+
```c++
128+
#include <SerialBridge.hpp>
129+
#include <MbedHardwareSerial.hpp>
130+
131+
SerialDev *dev = new MbedHardwareSerial(new BufferedSerial(USBTX, USBRX, 9600));
132+
SerialBridge serial(dev);
133+
```
134+
135+
### Messaseの用意とフレームの追加
136+
1. Messageを用意する
137+
structを使ってMessageの内容を決めます
138+
```c++
139+
typedef struct Vector3
140+
{
141+
float x;
142+
float y;
143+
float z;
144+
} vector3_t;
145+
```
146+
このようなstructをメッセージとして使ってみましょう
147+
Messageを作成するには[Message.hpp](src/Message.hpp)をインクルードする必要があります
148+
Vector3.h
149+
```c++
150+
#ifndef VECTOR3_H_
151+
#define VECTOR3_H_
152+
153+
#include <Message.hpp>
154+
155+
typedef struct Vector3
156+
{
157+
float x;
158+
float y;
159+
float z;
160+
} vector3_t;
161+
162+
// create message
163+
typedef sb::Message<vector3_t> Vector3;
164+
165+
#endif
166+
```
167+
2. SerialBridgeにMessageを登録
168+
作成したMessageを利用するには、フレームとして登録する必要があります
169+
SerialBridge::add_frame(id, msg)を使います。
170+
引数として、id(任意の値)とMessageのポインターを渡す必要があります。
171+
idは重複しないようにしてください。
172+
```c++
173+
#include <SerialBridge.hpp>
174+
// your message header
175+
#include <Vector3.h>
176+
177+
SerialBridge serial(...);
178+
179+
// Message
180+
Vector3 msg0;
181+
182+
void main(){
183+
serial.add_frame(0, &msg0);
184+
}
185+
```
186+
この場合はmsg0をid 0番と登録したことになります。
187+
188+
### 通信してみる
189+
#### **送信**
190+
関数
191+
- SerialBridge::write(id)
192+
idはadd_frame(id, msg)で追加したものを使用します
193+
Vector3に値を入れて送信してみましょう
194+
```c++
195+
#include <SerialBridge.hpp>
196+
// your message header
197+
#include <Vector3.h>
198+
199+
SerialBridge serial(...);
200+
201+
// Message
202+
Vector3 msg0; // sender
203+
204+
void main(){
205+
serial.add_frame(0, &msg0);
206+
207+
msg0.data.x = 0.123;
208+
msg0.data.y = 0.456;
209+
msg0.data.z = 0.789;
210+
}
211+
212+
void loop(){
213+
// write
214+
serial.write(0);
215+
}
216+
```
217+
Messageの値には、Message.data.VARIABLE_NAME でアクセスできます
218+
送信には、serial.write(id)をつかいます
219+
220+
#### **受信**
221+
関数
222+
- SerialBridge::update()
223+
受信したパケットをデコードします。read()関数を使用するたびに呼び出してください。
224+
- SerialBridge::read()
225+
メッセージを読み込みます。受信に成功すると0を返します。
226+
Vector3を受信してみましょう
227+
```c++
228+
#include <SerialBridge.hpp>
229+
// your message header
230+
#include <Vector3.h>
231+
232+
SerialBridge serial(...);
233+
234+
// Message
235+
Vector3 msg1; // listener
236+
237+
void main(){
238+
serial.add_frame(0, &msg1);
239+
}
240+
241+
void loop(){
242+
// update and read
243+
serial.update()
244+
if (serial.read() == 0)
245+
{
246+
printf("%f %f %f \n\r", msg1.data.x, mag1.data.y, msg1.data.z);
247+
}
248+
}
249+
```
250+
251+
252+
## 開発環境
253+
- Ubuntu 20.04 LTS
254+
- C++11 (GNU GCC 10.3.0)
255+
- Arduino IDE 1.8.13
256+
- Mbed OS 6.13.0 based
257+
258+
## Commit Prefix
259+
|Prefix |内容 |
260+
|---------|--------|
261+
|[Add] |ファイル追加 / 機能追加|
262+
|[Delete] | ファイル削除 / 機能削除|
263+
|[Update] | 機能修正 (バグ修正を除く)|
264+
|[Fix] |バグ修正|
265+
|[HotFix] |クリティカルなバグ修正|
266+
|[Clean] |リファクタリング / コード整理|
267+
|[Change] | 仕様変更|
268+
|[Rename] | 名前変更|
269+
|[Docs] | ドキュメント(説明)系の編集|
270+
|[Debug] | デバッグコードに関する編集 |
271+
272+
## License
273+
Apache-2.0 License
274+
275+
## Developer
276+
- [TaiyouKomazawa](https://github.com/TaiyouKomazawa)(Outside Contributor)
277+
- [testusuke](https://github.com/testusuke)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @file TestMsg.h
3+
* @brief Example of self-made message type definition.
4+
* @author Taiyou Komazawa
5+
* @date 2021/9/5
6+
*/
7+
8+
#ifndef TEST_MSG_H_
9+
#define TEST_MSG_H_
10+
11+
#include <Message.hpp>
12+
13+
//Define a structure with three unsigned integer 32-bit types.
14+
typedef struct TestMsgType
15+
{
16+
uint32_t cur;
17+
uint32_t prv;
18+
uint32_t prv_prv;
19+
} test_msg_t;
20+
21+
//Name the sb::Message type,which has a data structure of the test_msg_t type,the TestMsg type.
22+
typedef sb::Message<test_msg_t> TestMsg;
23+
24+
#endif
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @file echoback_server.ino
3+
* @brief Echoback example: A server-side program that sends back the transmitted data as is.
4+
* @author Taiyou Komazawa
5+
* @date 2021/9/5
6+
* @details In the Arduino IDE, this file is displayed as an example of the program
7+
* at the location of "SerialBridge > example > arduino > echoback_server" in
8+
* the "Examples from Custom Libraries" column of "File> Examples".
9+
*
10+
* @note Operation confirmed with Arduino UNO.
11+
*
12+
*/
13+
14+
#include <SerialBridge.hpp>
15+
#include <InoHardwareSerial.hpp>
16+
17+
#include "TestMsg.h"
18+
19+
//Pass the serial derived class entity for Arduino to SerialDev.
20+
SerialDev *dev = new InoHardwareSerial(&Serial);
21+
SerialBridge serial(dev);
22+
23+
//Declare a two-element message array. (Two for receiving and for sending,)
24+
TestMsg msg[2];
25+
26+
void setup()
27+
{
28+
Serial.begin(9600);
29+
30+
//Register "msg[0]" message with ID = 0.
31+
serial.add_frame(0, &msg[0]);
32+
//Register "msg[1]" message with ID = 1.
33+
serial.add_frame(1, &msg[1]);
34+
}
35+
36+
void loop()
37+
{
38+
//Get data from the serial bus.
39+
serial.update();
40+
//Get the message. (If there is an update, the return value is 0)
41+
int err = serial.read();
42+
43+
if(err == 0){ //If read () succeeds.
44+
//Assign the received message to the message for sending.
45+
//Since it is a structure, you can write it in the following way.
46+
// msg[1].data = msg[0].data;
47+
48+
msg[1].data.prv_prv = msg[0].data.prv_prv;
49+
msg[1].data.prv = msg[0].data.prv;
50+
msg[1].data.cur = msg[0].data.cur;
51+
52+
//Send a message with ID = 1 ("msg[1]" message).
53+
serial.write(1);
54+
}
55+
}

0 commit comments

Comments
 (0)