port_handler_arduino.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright 2017 ROBOTIS CO., LTD.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
16 
17 /* Author: Ryu Woon Jung (Leon) */
18 
19 #if defined(ARDUINO) || defined(__OPENCR__) || defined(__OPENCM904__)
20 
21 #include <Arduino.h>
22 
23 
24 #include "../../include/dynamixel_sdk/port_handler_arduino.h"
25 
26 #if defined (__OPENCR__)
27 #define DYNAMIXEL_SERIAL Serial3
28 #endif
29 
30 #define LATENCY_TIMER 4 // msec (USB latency timer)
31 
32 using namespace dynamixel;
33 
34 PortHandlerArduino::PortHandlerArduino(const char *port_name)
35  : baudrate_(DEFAULT_BAUDRATE_),
36  packet_start_time_(0.0),
37  packet_timeout_(0.0),
38  tx_time_per_byte(0.0)
39 {
40  is_using_ = false;
41  setPortName(port_name);
42 
43 #if defined(__OPENCR__)
44  pinMode(BDPIN_DXL_PWR_EN, OUTPUT);
45 
46  setPowerOff();
47 #elif defined(__OPENCM904__)
48  if (port_name[0] == '1')
49  {
50  socket_fd_ = 0;
51  p_dxl_serial = &Serial1;
52  }
53  else if (port_name[0] == '2')
54  {
55  socket_fd_ = 1;
56  p_dxl_serial = &Serial2;
57  }
58  else if (port_name[0] == '3')
59  {
60  socket_fd_ = 2;
61  p_dxl_serial = &Serial3;
62  }
63  else
64  {
65  socket_fd_ = 0;
66  p_dxl_serial = &Serial1;
67  }
68 
69  drv_dxl_begin(socket_fd_);
70 #endif
71 
72  setTxDisable();
73 }
74 
76 {
77 #if defined(__OPENCR__)
78  pinMode(BDPIN_DXL_PWR_EN, OUTPUT);
79 
80  setPowerOn();
81  delay(1000);
82 #endif
83 
84  return setBaudRate(baudrate_);
85 }
86 
88 {
89  setPowerOff();
90 }
91 
93 {
94  int temp __attribute__((unused));
95 #if defined(__OPENCR__)
96  while (DYNAMIXEL_SERIAL.available())
97  {
98  temp = DYNAMIXEL_SERIAL.read();
99  }
100 #elif defined(__OPENCM904__)
101  while (p_dxl_serial->available())
102  {
103  temp = p_dxl_serial->read();
104  }
105 #endif
106 }
107 
108 void PortHandlerArduino::setPortName(const char *port_name)
109 {
110  strcpy(port_name_, port_name);
111 }
112 
114 {
115  return port_name_;
116 }
117 
118 bool PortHandlerArduino::setBaudRate(const int baudrate)
119 {
120  baudrate_ = checkBaudrateAvailable(baudrate);
121 
122  if (baudrate_ == -1)
123  return false;
124 
125  setupPort(baudrate_);
126 
127  return true;
128 }
129 
131 {
132  return baudrate_;
133 }
134 
136 {
137  int bytes_available;
138 
139 #if defined(__OPENCR__)
140  bytes_available = DYNAMIXEL_SERIAL.available();
141 #elif defined(__OPENCM904__)
142  bytes_available = p_dxl_serial->available();
143 #endif
144 
145  return bytes_available;
146 }
147 
148 int PortHandlerArduino::readPort(uint8_t *packet, int length)
149 {
150  int rx_length;
151 
152 #if defined(__OPENCR__)
153  rx_length = DYNAMIXEL_SERIAL.available();
154 #elif defined(__OPENCM904__)
155  rx_length = p_dxl_serial->available();
156 #endif
157 
158  if (rx_length > length)
159  rx_length = length;
160 
161  for (int i = 0; i < rx_length; i++)
162  {
163 #if defined(__OPENCR__)
164  packet[i] = DYNAMIXEL_SERIAL.read();
165 #elif defined(__OPENCM904__)
166  packet[i] = p_dxl_serial->read();
167 #endif
168  }
169 
170  return rx_length;
171 }
172 
173 int PortHandlerArduino::writePort(uint8_t *packet, int length)
174 {
175  int length_written;
176 
177  setTxEnable();
178 
179 #if defined(__OPENCR__)
180  length_written = DYNAMIXEL_SERIAL.write(packet, length);
181 #elif defined(__OPENCM904__)
182  length_written = p_dxl_serial->write(packet, length);
183 #endif
184 
185  setTxDisable();
186 
187  return length_written;
188 }
189 
190 void PortHandlerArduino::setPacketTimeout(uint16_t packet_length)
191 {
192  packet_start_time_ = getCurrentTime();
193  packet_timeout_ = (tx_time_per_byte * (double)packet_length) + (LATENCY_TIMER * 2.0) + 2.0;
194 }
195 
196 void PortHandlerArduino::setPacketTimeout(double msec)
197 {
198  packet_start_time_ = getCurrentTime();
199  packet_timeout_ = msec;
200 }
201 
203 {
204  if (getTimeSinceStart() > packet_timeout_)
205  {
206  packet_timeout_ = 0;
207  return true;
208  }
209 
210  return false;
211 }
212 
214 {
215  return (double)millis();
216 }
217 
219 {
220  double elapsed_time;
221 
222  elapsed_time = getCurrentTime() - packet_start_time_;
223  if (elapsed_time < 0.0)
224  packet_start_time_ = getCurrentTime();
225 
226  return elapsed_time;
227 }
228 
229 bool PortHandlerArduino::setupPort(int baudrate)
230 {
231 #if defined(__OPENCR__)
232  DYNAMIXEL_SERIAL.begin(baudrate);
233 #elif defined(__OPENCM904__)
234  p_dxl_serial->setDxlMode(true);
235  p_dxl_serial->begin(baudrate);
236 #endif
237 
238  delay(100);
239 
240  tx_time_per_byte = (1000.0 / (double)baudrate) * 10.0;
241  return true;
242 }
243 
245 {
246  switch(baudrate)
247  {
248  case 9600:
249  return 9600;
250  case 57600:
251  return 57600;
252  case 115200:
253  return 115200;
254  case 1000000:
255  return 1000000;
256  case 2000000:
257  return 2000000;
258  case 3000000:
259  return 3000000;
260  case 4000000:
261  return 4000000;
262  case 4500000:
263  return 4500000;
264  default:
265  return -1;
266  }
267 }
268 
270 {
271 #if defined(__OPENCR__)
272  digitalWrite(BDPIN_DXL_PWR_EN, HIGH);
273 #endif
274 }
275 
277 {
278 #if defined(__OPENCR__)
279  digitalWrite(BDPIN_DXL_PWR_EN, LOW);
280 #endif
281 }
282 
284 {
285 #if defined(__OPENCR__)
286  drv_dxl_tx_enable(TRUE);
287 #elif defined(__OPENCM904__)
288  drv_dxl_tx_enable(socket_fd_, TRUE);
289 #endif
290 }
291 
293 {
294 #if defined(__OPENCR__)
295 #ifdef SERIAL_WRITES_NON_BLOCKING
296  DYNAMIXEL_SERIAL.flush(); // make sure it completes before we disable...
297 #endif
298  drv_dxl_tx_enable(FALSE);
299 
300 #elif defined(__OPENCM904__)
301 #ifdef SERIAL_WRITES_NON_BLOCKING
302  p_dxl_serial->flush();
303 #endif
304  drv_dxl_tx_enable(socket_fd_, FALSE);
305 #endif
306 }
307 
308 #endif
int writePort(uint8_t *packet, int length)
The function that writes bytes on the port buffer The function writes bytes on the port buffer...
bool setupPort(const int cflag_baud)
void setPacketTimeout(uint16_t packet_length)
The function that sets and starts stopwatch for watching packet timeout The function sets the stopwa...
int checkBaudrateAvailable(int baudrate)
int readPort(uint8_t *packet, int length)
The function that reads bytes from the port buffer The function gets bytes from the port buffer...
int getBaudRate()
The function that returns current baudrate set into the port handler The function returns current ba...
void closePort()
The function that closes the port The function closes the port.
bool setBaudRate(const int baudrate)
The function that sets baudrate into the port handler The function sets baudrate into the port handl...
PortHandlerArduino(const char *port_name)
The function that initializes instance of PortHandler and gets port_name The function initializes in...
bool openPort()
The function that opens the port The function calls PortHandlerArduino::setBaudRate() to open the po...
int getBytesAvailable()
The function that checks how much bytes are able to be read from the port buffer The function checks...
void clearPort()
The function that clears the port The function clears the port.
char * getPortName()
The function that returns port name set into the port handler The function returns current port name...
void setPortName(const char *port_name)
The function that sets port name into the port handler The function sets port name into the port han...
bool is_using_
shows whether the port is in use
Definition: port_handler.h:67
virtual bool setBaudRate(const int baudrate)=0
The function that sets baudrate into the port handler The function sets baudrate into the port handl...
bool isPacketTimeout()
The function that checks whether packet timeout is occurred The function checks whether current time...
virtual void setPortName(const char *port_name)=0
The function that sets port name into the port handler The function sets port name into the port han...


dynamixel_sdk
Author(s): Gilbert , Zerom , Darby Lim , Leon
autogenerated on Fri Apr 16 2021 02:25:55