xx

Committer:
anfontanelli
Date:
Tue Sep 14 11:15:45 2021 +0000
Revision:
12:b423b3cbec47
Parent:
11:04d8899b5d82
A

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sype 0:af5cf35e1a25 1 #ifndef ROBOCLAW_H
sype 0:af5cf35e1a25 2 #define ROBOCLAW_H
sype 0:af5cf35e1a25 3 #include "mbed.h"
sype 0:af5cf35e1a25 4 #include "registers.h"
anfontanelli 11:04d8899b5d82 5 #include "UARTSerial_mio.h"
sype 2:a2c141d922b3 6 /** The RoboClaw class, yo ucan use the library with : http://www.ionmc.com/RoboClaw-2x15A-Motor-Controller_p_10.html
sype 2:a2c141d922b3 7 * Used to control one or two motors with (or not) encoders
sype 2:a2c141d922b3 8 * @code
sype 2:a2c141d922b3 9 * #include "mbed.h"
sype 2:a2c141d922b3 10 * #include "RoboClaw.h"
sype 0:af5cf35e1a25 11 *
sype 2:a2c141d922b3 12 * RoboClaw roboclaw(115200, PA_11, PA_12);
sype 2:a2c141d922b3 13 *
sype 2:a2c141d922b3 14 * int main() {
sype 2:a2c141d922b3 15 * roboclaw.ForwardM1(ADR, 127);
sype 2:a2c141d922b3 16 * while(1);
sype 2:a2c141d922b3 17 * }
sype 2:a2c141d922b3 18 * @endcode
sype 0:af5cf35e1a25 19 */
sype 0:af5cf35e1a25 20
sype 0:af5cf35e1a25 21 class RoboClaw
sype 0:af5cf35e1a25 22 {
sype 0:af5cf35e1a25 23 public:
sype 2:a2c141d922b3 24 /** Create RoboClaw instance
sype 2:a2c141d922b3 25 */
anfontanelli 12:b423b3cbec47 26 RoboClaw(uint8_t adr, int baudrate, PinName rx, PinName tx, double _read_timeout);
sype 0:af5cf35e1a25 27
sype 2:a2c141d922b3 28 /** Forward and Backward functions
sype 2:a2c141d922b3 29 * @param address address of the device
sype 2:a2c141d922b3 30 * @param speed speed of the motor (between 0 and 127)
sype 2:a2c141d922b3 31 * @note Forward and Backward functions
sype 2:a2c141d922b3 32 */
sype 9:0fc5514faed9 33 void ForwardM1(int speed);
sype 9:0fc5514faed9 34 void BackwardM1(int speed);
sype 9:0fc5514faed9 35 void ForwardM2(int speed);
sype 9:0fc5514faed9 36 void BackwardM2(int speed);
sype 0:af5cf35e1a25 37
sype 2:a2c141d922b3 38 /** Forward and Backward functions
sype 2:a2c141d922b3 39 * @param address address of the device
sype 2:a2c141d922b3 40 * @param speed speed of the motor (between 0 and 127)
sype 2:a2c141d922b3 41 * @note Forward and Backward functions, it turns the two motors
sype 2:a2c141d922b3 42 */
sype 9:0fc5514faed9 43 void Forward(int speed);
sype 9:0fc5514faed9 44 void Backward(int speed);
sype 0:af5cf35e1a25 45
sype 2:a2c141d922b3 46 /** Read the Firmware
sype 2:a2c141d922b3 47 * @param address address of the device
sype 2:a2c141d922b3 48 */
sype 9:0fc5514faed9 49 void ReadFirm();
sype 0:af5cf35e1a25 50
sype 2:a2c141d922b3 51 /** Read encoder and speed of M1 or M2
sype 2:a2c141d922b3 52 * @param address address of the device
sype 2:a2c141d922b3 53 * @note Read encoder in ticks
sype 2:a2c141d922b3 54 * @note Read speed in ticks per second
sype 2:a2c141d922b3 55 */
anfontanelli 11:04d8899b5d82 56 bool ReadEncM1(int32_t &encPulse);
anfontanelli 11:04d8899b5d82 57 bool ReadEncM2(int32_t &encPulse);
anfontanelli 11:04d8899b5d82 58 bool ReadSpeedM1(int32_t &speed);
anfontanelli 11:04d8899b5d82 59 bool ReadSpeedM2(int32_t &speed);
anfontanelli 11:04d8899b5d82 60 bool ReadCurrentM1M2(int32_t &currentM1, int32_t &currentM2);
anfontanelli 11:04d8899b5d82 61
sype 0:af5cf35e1a25 62
sype 2:a2c141d922b3 63 /** Set both encoders to zero
sype 2:a2c141d922b3 64 * @param address address of the device
sype 2:a2c141d922b3 65 */
sype 9:0fc5514faed9 66 void ResetEnc();
sype 0:af5cf35e1a25 67
sype 2:a2c141d922b3 68 /** Set speed of Motor with different parameter (only in ticks)
sype 2:a2c141d922b3 69 * @param address address of the device
sype 2:a2c141d922b3 70 * @note Set the Speed
sype 2:a2c141d922b3 71 * @note Set the Speed and Accel
sype 2:a2c141d922b3 72 * @note Set the Speed and Distance
sype 2:a2c141d922b3 73 * @note Set the Speed, Accel and Distance
sype 2:a2c141d922b3 74 * @note Set the Speed, Accel, Decceleration and Position
sype 2:a2c141d922b3 75 */
sype 9:0fc5514faed9 76 void SpeedM1(int32_t speed);
sype 9:0fc5514faed9 77 void SpeedM2(int32_t speed);
sype 9:0fc5514faed9 78 void SpeedAccelM1(int32_t accel, int32_t speed);
sype 9:0fc5514faed9 79 void SpeedAccelM2(int32_t accel, int32_t speed);
sype 10:f82bc24e3bd4 80 void SpeedAccelM1M2(int32_t accel, int32_t speed1, int32_t speed2);
sype 9:0fc5514faed9 81 void SpeedDistanceM1(int32_t speed, uint32_t distance, uint8_t buffer);
sype 9:0fc5514faed9 82 void SpeedDistanceM2(int32_t speed, uint32_t distance, uint8_t buffer);
sype 9:0fc5514faed9 83 void SpeedAccelDistanceM1(int32_t accel, int32_t speed, uint32_t distance, uint8_t buffer);
sype 9:0fc5514faed9 84 void SpeedAccelDistanceM2(int32_t accel, int32_t speed, uint32_t distance, uint8_t buffer);
sype 9:0fc5514faed9 85 void SpeedAccelDeccelPositionM1(uint32_t accel, int32_t speed, uint32_t deccel, int32_t position, uint8_t flag);
sype 9:0fc5514faed9 86 void SpeedAccelDeccelPositionM2(uint32_t accel, int32_t speed, uint32_t deccel, int32_t position, uint8_t flag);
sype 9:0fc5514faed9 87 void SpeedAccelDeccelPositionM1M2(uint32_t accel1,uint32_t speed1,uint32_t deccel1, int32_t position1,uint32_t accel2,uint32_t speed2,uint32_t deccel2, int32_t position2,uint8_t flag);
sype 0:af5cf35e1a25 88
sype 0:af5cf35e1a25 89 private:
anfontanelli 11:04d8899b5d82 90 UARTSerial_mio _roboclaw;
sype 2:a2c141d922b3 91 uint16_t crc;
sype 9:0fc5514faed9 92 uint8_t address;
sype 0:af5cf35e1a25 93 void crc_clear();
sype 2:a2c141d922b3 94 void crc_update(uint8_t data);
sype 2:a2c141d922b3 95 uint16_t crc_get();
sype 0:af5cf35e1a25 96
sype 2:a2c141d922b3 97 void write_n(uint8_t cnt, ...);
sype 9:0fc5514faed9 98 void write_(uint8_t command, uint8_t data, bool reading, bool crcon);
sype 0:af5cf35e1a25 99
sype 2:a2c141d922b3 100 uint16_t crc16(uint8_t *packet, int nBytes);
anfontanelli 11:04d8899b5d82 101 int16_t read_(void);
anfontanelli 11:04d8899b5d82 102 bool read_n(uint8_t address, uint8_t cmd, int n_byte, int32_t &value);
anfontanelli 11:04d8899b5d82 103 void flushSerialBuffer(void);
anfontanelli 11:04d8899b5d82 104 void Rx_interrupt();
anfontanelli 11:04d8899b5d82 105 // Circular buffers for serial TX and RX data - used by interrupt routines
anfontanelli 11:04d8899b5d82 106 const int buffer_size = 7;
anfontanelli 11:04d8899b5d82 107 // might need to increase buffer size for high baud rates
anfontanelli 11:04d8899b5d82 108 char rx_buffer[7];
anfontanelli 11:04d8899b5d82 109 // Circular buffer pointers
anfontanelli 11:04d8899b5d82 110 // volatile makes read-modify-write atomic
anfontanelli 12:b423b3cbec47 111 //Timer readTimer;
anfontanelli 12:b423b3cbec47 112 //double readTime;
anfontanelli 12:b423b3cbec47 113 //double readTimePrec;
anfontanelli 11:04d8899b5d82 114 volatile int rx_in;
anfontanelli 11:04d8899b5d82 115 double read_timeout;
anfontanelli 11:04d8899b5d82 116 // Line buffers for sprintf and sscanf
anfontanelli 11:04d8899b5d82 117
anfontanelli 11:04d8899b5d82 118 char rx_line[80];
sype 0:af5cf35e1a25 119 };
sype 0:af5cf35e1a25 120
sype 0:af5cf35e1a25 121 #endif