/
usb_to_dynamixel.ino
88 lines (76 loc) · 2.01 KB
/
usb_to_dynamixel.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2022 ROBOTIS CO., LTD.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <Dynamixel2Arduino.h>
#define DXL_BUS Serial1
#define USB Serial
#define DXL_PACKET_BUFFER_LENGTH 1024
uint8_t packet_buffer[DXL_PACKET_BUFFER_LENGTH];
unsigned long led_update_time = 0;
Dynamixel2Arduino dxl(DXL_BUS);
void setup() {
// put your setup code here, to run once:
pinMode(LED_BUILTIN, OUTPUT);
// Use UART port of DYNAMIXEL Shield to debug.
USB.begin(57600);
// Set Port baudrate to 57600bps. This has to match with DYNAMIXEL baudrate.
dxl.begin(USB.baud());
// Set Port Protocol Version. This has to match with DYNAMIXEL protocol version.
}
void loop() {
// put your main code here, to run repeatedly:
dataTransceiver();
if(USB.baud() != dxl.getPortBaud()) {
dxl.begin(USB.baud());
}
}
void dataTransceiver()
{
int length = 0;
int i = 0;
// USB -> DXL
length = USB.available();
if( length > 0 )
{
for(i = 0; i < length; i++)
{
DXL_BUS.write(USB.read());
}
ledStatus();
}
// DXL -> USB
length = DXL_BUS.available();
if( length > 0 )
{
if( length > DXL_PACKET_BUFFER_LENGTH )
{
length = DXL_PACKET_BUFFER_LENGTH;
}
for(i = 0; i < length; i++)
{
packet_buffer[i] = DXL_BUS.read();
}
USB.write(packet_buffer, length);
ledStatus();
}
}
void ledStatus()
{
if((millis() - led_update_time) > 200 )
{
digitalWrite(LED_BUILTIN, 1);
led_update_time = millis();
} else {
digitalWrite(LED_BUILTIN, 0);
}
}