PX4 Firmware
PX4 Autopilot Software http://px4.io
RoboClaw.hpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2013 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file RoboClas.hpp
36  *
37  * RoboClaw Motor Driver
38  *
39  * references:
40  * http://downloads.orionrobotics.com/downloads/datasheets/motor_controller_robo_claw_R0401.pdf
41  *
42  */
43 
44 #pragma once
45 
46 #include <poll.h>
47 #include <stdio.h>
48 #include <termios.h>
49 #include <lib/parameters/param.h>
54 #include <drivers/device/i2c.h>
55 #include <sys/select.h>
56 #include <sys/time.h>
57 #include <pthread.h>
58 
59 /**
60  * This is a driver for the RoboClaw motor controller
61  */
62 class RoboClaw
63 {
64 public:
65 
66  void taskMain();
67  static bool taskShouldExit;
68 
69  /** control channels */
70  enum e_channel {
73  };
74 
75  /** motors */
76  enum e_motor {
77  MOTOR_1 = 0,
79  };
80 
81  /**
82  * constructor
83  * @param deviceName the name of the
84  * serial port e.g. "/dev/ttyS2"
85  * @param address the adddress of the motor
86  * (selectable on roboclaw)
87  * @param baudRateParam Name of the parameter that holds the baud rate of this serial port
88  */
89  RoboClaw(const char *deviceName, const char *baudRateParam);
90 
91  /**
92  * deconstructor
93  */
94  virtual ~RoboClaw();
95 
96  /**
97  * @return position of a motor, rev
98  */
99  float getMotorPosition(e_motor motor);
100 
101  /**
102  * @return speed of a motor, rev/sec
103  */
104  float getMotorSpeed(e_motor motor);
105 
106  /**
107  * set the speed of a motor, rev/sec
108  */
109  int setMotorSpeed(e_motor motor, float value);
110 
111  /**
112  * set the duty cycle of a motor
113  */
114  int setMotorDutyCycle(e_motor motor, float value);
115 
116  /**
117  * Drive both motors. +1 = full forward, -1 = full backward
118  */
119  int drive(float value);
120 
121  /**
122  * Turn. +1 = full right, -1 = full left
123  */
124  int turn(float value);
125 
126  /**
127  * reset the encoders
128  * @return status
129  */
130  int resetEncoders();
131 
132  /**
133  * read data from serial
134  */
135  int readEncoder();
136 
137  /**
138  * print status
139  */
140  void printStatus(char *string, size_t n);
141 
142 private:
143 
144  // commands
145  // We just list the commands we want from the manual here.
146  enum e_command {
147 
148  // simple
153 
158 
159  // encoder commands
167 
168  // advanced motor control
173 
175  };
176 
177  struct {
179  int32_t counts_per_rev;
182  int32_t address;
183  } _parameters{};
184 
185  struct {
191  } _param_handles{};
192 
193  int _uart;
194  fd_set _uart_set;
195  struct timeval _uart_timeout;
196 
197  /** actuator controls subscription */
198  int _actuatorsSub{-1};
200 
201  int _armedSub{-1};
203 
204  int _paramSub{-1};
206 
207  orb_advert_t _wheelEncodersAdv[2] {nullptr, nullptr};
209 
210  uint32_t _lastEncoderCount[2] {0, 0};
211  int64_t _encoderCounts[2] {0, 0};
212  int32_t _motorSpeeds[2] {0, 0};
213 
214  void _parameters_update();
215 
216  static uint16_t _calcCRC(const uint8_t *buf, size_t n, uint16_t init = 0);
218  int _sendSigned16Bit(e_command command, float data);
219  int _sendNothing(e_command);
220 
221  /**
222  * Perform a round-trip write and read.
223  *
224  * NOTE: This function is not thread-safe.
225  *
226  * @param cmd Command to send to the Roboclaw
227  * @param wbuff Write buffer. Must not contain command, address, or checksum. For most commands, this will be
228  * one or two bytes. Can be null iff wbytes == 0.
229  * @param wbytes Number of bytes to write. Can be 0.
230  * @param rbuff Read buffer. Will be filled with the entire response, including a checksum if the Roboclaw sends
231  * a checksum for this command.
232  * @param rbytes Maximum number of bytes to read.
233  * @param send_checksum If true, then the checksum will be calculated and sent to the Roboclaw.
234  * This is an option because some Roboclaw commands expect no checksum.
235  * @param recv_checksum If true, then this function will calculate the checksum of the returned data and compare
236  * it to the checksum received. If they are not equal, OR if fewer than 2 bytes were received, then an
237  * error is returned.
238  * If false, then this function will expect to read exactly one byte, 0xFF, and will return an error otherwise.
239  * @return If successful, then the number of bytes read from the Roboclaw is returned. If there is a timeout
240  * reading from the Roboclaw, then 0 is returned. If there is an IO error, then a negative value is returned.
241  */
242  int _transaction(e_command cmd, uint8_t *wbuff, size_t wbytes,
243  uint8_t *rbuff, size_t rbytes, bool send_checksum = true, bool recv_checksum = false);
244 };
actuator_armed_s _actuatorArmed
Definition: RoboClaw.hpp:202
void _parameters_update()
Definition: RoboClaw.cpp:569
int _sendNothing(e_command)
Definition: RoboClaw.cpp:450
struct timeval _uart_timeout
Definition: RoboClaw.hpp:195
float getMotorPosition(e_motor motor)
Definition: RoboClaw.cpp:331
int readEncoder()
read data from serial
Definition: RoboClaw.cpp:255
int32_t counts_per_rev
Definition: RoboClaw.hpp:179
struct RoboClaw::@20 _parameters
static bool taskShouldExit
Definition: RoboClaw.hpp:67
parameter_update_s _paramUpdate
Definition: RoboClaw.hpp:205
int _uart
Definition: RoboClaw.hpp:193
int32_t _motorSpeeds[2]
Definition: RoboClaw.hpp:212
e_channel
control channels
Definition: RoboClaw.hpp:70
actuator_controls_s _actuatorControls
Definition: RoboClaw.hpp:199
RoboClaw(const char *deviceName, const char *baudRateParam)
constructor
Definition: RoboClaw.cpp:80
int setMotorSpeed(e_motor motor, float value)
set the speed of a motor, rev/sec
Definition: RoboClaw.cpp:359
param_t encoder_read_period_ms
Definition: RoboClaw.hpp:188
param_t serial_baud_rate
Definition: RoboClaw.hpp:186
speed_t serial_baud_rate
Definition: RoboClaw.hpp:178
e_motor
motors
Definition: RoboClaw.hpp:76
int _actuatorsSub
actuator controls subscription
Definition: RoboClaw.hpp:198
Global flash based parameter store.
int turn(float value)
Turn.
Definition: RoboClaw.cpp:412
wheel_encoders_s _wheelEncoderMsg[2]
Definition: RoboClaw.hpp:208
int32_t encoder_read_period_ms
Definition: RoboClaw.hpp:180
float getMotorSpeed(e_motor motor)
Definition: RoboClaw.cpp:345
orb_advert_t _wheelEncodersAdv[2]
Definition: RoboClaw.hpp:207
param_t actuator_write_period_ms
Definition: RoboClaw.hpp:189
void init()
Activates/configures the hardware registers.
int resetEncoders()
reset the encoders
Definition: RoboClaw.cpp:418
void printStatus(char *string, size_t n)
print status
Definition: RoboClaw.cpp:322
param_t counts_per_rev
Definition: RoboClaw.hpp:187
uint8_t * data
Definition: dataman.cpp:149
param_t address
Definition: RoboClaw.hpp:190
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
Definition: uORB.h:134
int _paramSub
Definition: RoboClaw.hpp:204
int setMotorDutyCycle(e_motor motor, float value)
set the duty cycle of a motor
Definition: RoboClaw.cpp:387
int _sendUnsigned7Bit(e_command command, float data)
Definition: RoboClaw.cpp:423
int _sendSigned16Bit(e_command command, float data)
Definition: RoboClaw.cpp:436
uint32_t _lastEncoderCount[2]
Definition: RoboClaw.hpp:210
int _transaction(e_command cmd, uint8_t *wbuff, size_t wbytes, uint8_t *rbuff, size_t rbytes, bool send_checksum=true, bool recv_checksum=false)
Perform a round-trip write and read.
Definition: RoboClaw.cpp:476
static uint16_t _calcCRC(const uint8_t *buf, size_t n, uint16_t init=0)
Definition: RoboClaw.cpp:456
int64_t _encoderCounts[2]
Definition: RoboClaw.hpp:211
int drive(float value)
Drive both motors.
Definition: RoboClaw.cpp:406
int _armedSub
Definition: RoboClaw.hpp:201
int32_t actuator_write_period_ms
Definition: RoboClaw.hpp:181
virtual ~RoboClaw()
deconstructor
Definition: RoboClaw.cpp:128
fd_set _uart_set
Definition: RoboClaw.hpp:194
int32_t address
Definition: RoboClaw.hpp:182
struct RoboClaw::@21 _param_handles
This is a driver for the RoboClaw motor controller.
Definition: RoboClaw.hpp:62
uint32_t param_t
Parameter handle.
Definition: param.h:98
void taskMain()
Definition: RoboClaw.cpp:135
Base class for devices connected via I2C.