first_library

Dependents:   2019_tourobo_upper minirobo_upper_reserve minirobo_under_reserve serial_RTX_NUCLEA

Committer:
sink
Date:
Mon Feb 04 04:46:37 2019 +0000
Revision:
1:9dd052490f0a
ok

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sink 1:9dd052490f0a 1 #ifndef ROBOCLAW_H
sink 1:9dd052490f0a 2 #define ROBOCLAW_H
sink 1:9dd052490f0a 3 #include "mbed.h"
sink 1:9dd052490f0a 4 #include "registers.h"
sink 1:9dd052490f0a 5
sink 1:9dd052490f0a 6 /*
sink 1:9dd052490f0a 7 * 一般公開されているライブラリのマイナーチェンジである
sink 1:9dd052490f0a 8 * Serial -> RawSerial
sink 1:9dd052490f0a 9 * 指令時にアドレスを指定できるものを利用した
sink 1:9dd052490f0a 10 *
sink 1:9dd052490f0a 11 */
sink 1:9dd052490f0a 12
sink 1:9dd052490f0a 13 /** The RoboClaw class, yo ucan use the library with : http://www.ionmc.com/RoboClaw-2x15A-Motor-Controller_p_10.html
sink 1:9dd052490f0a 14 * Used to control one or two motors with (or not) encoders
sink 1:9dd052490f0a 15 * @code
sink 1:9dd052490f0a 16 * #include "mbed.h"
sink 1:9dd052490f0a 17 * #include "RoboClaw.h"
sink 1:9dd052490f0a 18 *
sink 1:9dd052490f0a 19 * RoboClaw roboclaw(115200, PA_11, PA_12);
sink 1:9dd052490f0a 20 *
sink 1:9dd052490f0a 21 * int main() {
sink 1:9dd052490f0a 22 * roboclaw.ForwardM1(ADR, 127);
sink 1:9dd052490f0a 23 * while(1);
sink 1:9dd052490f0a 24 * }
sink 1:9dd052490f0a 25 * @endcode
sink 1:9dd052490f0a 26 */
sink 1:9dd052490f0a 27
sink 1:9dd052490f0a 28 class RoboClaw
sink 1:9dd052490f0a 29 {
sink 1:9dd052490f0a 30 public:
sink 1:9dd052490f0a 31 /** Create RoboClaw instance
sink 1:9dd052490f0a 32 */
sink 1:9dd052490f0a 33 RoboClaw(int baudrate, PinName rx, PinName tx);
sink 1:9dd052490f0a 34
sink 1:9dd052490f0a 35 /** Forward and Backward functions
sink 1:9dd052490f0a 36 * @param address address of the device
sink 1:9dd052490f0a 37 * @param speed speed of the motor (between 0 and 127)
sink 1:9dd052490f0a 38 * @note Forward and Backward functions
sink 1:9dd052490f0a 39 */
sink 1:9dd052490f0a 40 void ForwardM1(uint8_t address, int speed);
sink 1:9dd052490f0a 41 void BackwardM1(uint8_t address, int speed);
sink 1:9dd052490f0a 42 void ForwardM2(uint8_t address, int speed);
sink 1:9dd052490f0a 43 void BackwardM2(uint8_t address, int speed);
sink 1:9dd052490f0a 44
sink 1:9dd052490f0a 45 /** Forward and Backward functions
sink 1:9dd052490f0a 46 * @param address address of the device
sink 1:9dd052490f0a 47 * @param speed speed of the motor (between 0 and 127)
sink 1:9dd052490f0a 48 * @note Forward and Backward functions, it turns the two motors
sink 1:9dd052490f0a 49 */
sink 1:9dd052490f0a 50 void Forward(uint8_t address, int speed);
sink 1:9dd052490f0a 51 void Backward(uint8_t address, int speed);
sink 1:9dd052490f0a 52
sink 1:9dd052490f0a 53 /** Read the Firmware
sink 1:9dd052490f0a 54 * @param address address of the device
sink 1:9dd052490f0a 55 */
sink 1:9dd052490f0a 56 void ReadFirm(uint8_t address);
sink 1:9dd052490f0a 57
sink 1:9dd052490f0a 58 /** Read encoder and speed of M1 or M2
sink 1:9dd052490f0a 59 * @param address address of the device
sink 1:9dd052490f0a 60 * @note Read encoder in ticks
sink 1:9dd052490f0a 61 * @note Read speed in ticks per second
sink 1:9dd052490f0a 62 */
sink 1:9dd052490f0a 63 int32_t ReadEncM1(uint8_t address);
sink 1:9dd052490f0a 64 int32_t ReadEncM2(uint8_t address);
sink 1:9dd052490f0a 65 int32_t ReadSpeedM1(uint8_t address);
sink 1:9dd052490f0a 66 int32_t ReadSpeedM2(uint8_t address);
sink 1:9dd052490f0a 67
sink 1:9dd052490f0a 68 /** Set both encoders to zero
sink 1:9dd052490f0a 69 * @param address address of the device
sink 1:9dd052490f0a 70 */
sink 1:9dd052490f0a 71 void ResetEnc(uint8_t address);
sink 1:9dd052490f0a 72
sink 1:9dd052490f0a 73 /** Set speed of Motor with different parameter (only in ticks)
sink 1:9dd052490f0a 74 * @param address address of the device
sink 1:9dd052490f0a 75 * @note Set the Speed
sink 1:9dd052490f0a 76 * @note Set the Speed and Accel
sink 1:9dd052490f0a 77 * @note Set the Speed and Distance
sink 1:9dd052490f0a 78 * @note Set the Speed, Accel and Distance
sink 1:9dd052490f0a 79 * @note Set the Speed, Accel, Decceleration and Position
sink 1:9dd052490f0a 80 */
sink 1:9dd052490f0a 81 void SpeedM1(uint8_t address, int32_t speed);
sink 1:9dd052490f0a 82 void SpeedM2(uint8_t address, int32_t speed);
sink 1:9dd052490f0a 83 void SpeedAccelM1(uint8_t address, int32_t accel, int32_t speed);
sink 1:9dd052490f0a 84 void SpeedAccelM2(uint8_t address, int32_t accel, int32_t speed);
sink 1:9dd052490f0a 85 void SpeedAccelM1M2(uint8_t address, int32_t accel1, int32_t speed1, int32_t accel2, int32_t speed2);
sink 1:9dd052490f0a 86 void SpeedDistanceM1(uint8_t address, int32_t speed, uint32_t distance, uint8_t buffer);
sink 1:9dd052490f0a 87 void SpeedDistanceM2(uint8_t address, int32_t speed, uint32_t distance, uint8_t buffer);
sink 1:9dd052490f0a 88 void SpeedAccelDistanceM1(uint8_t address, int32_t accel, int32_t speed, uint32_t distance, uint8_t buffer);
sink 1:9dd052490f0a 89 void SpeedAccelDistanceM2(uint8_t address, int32_t accel, int32_t speed, uint32_t distance, uint8_t buffer);
sink 1:9dd052490f0a 90 void SpeedAccelDeccelPositionM1(uint8_t address, uint32_t accel, int32_t speed, uint32_t deccel, int32_t position, uint8_t flag);
sink 1:9dd052490f0a 91 void SpeedAccelDeccelPositionM2(uint8_t address, uint32_t accel, int32_t speed, uint32_t deccel, int32_t position, uint8_t flag);
sink 1:9dd052490f0a 92 void SpeedAccelDeccelPositionM1M2(uint8_t address,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);
sink 1:9dd052490f0a 93
sink 1:9dd052490f0a 94 private:
sink 1:9dd052490f0a 95 RawSerial _roboclaw;
sink 1:9dd052490f0a 96 uint16_t crc;
sink 1:9dd052490f0a 97 void crc_clear();
sink 1:9dd052490f0a 98 void crc_update(uint8_t data);
sink 1:9dd052490f0a 99 uint16_t crc_get();
sink 1:9dd052490f0a 100
sink 1:9dd052490f0a 101 void write_n(uint8_t cnt, ...);
sink 1:9dd052490f0a 102 void write_(uint8_t address, uint8_t command, uint8_t data, bool reading, bool crcon);
sink 1:9dd052490f0a 103
sink 1:9dd052490f0a 104 uint16_t crc16(uint8_t *packet, int nBytes);
sink 1:9dd052490f0a 105 uint8_t read_(void);
sink 1:9dd052490f0a 106 };
sink 1:9dd052490f0a 107
sink 1:9dd052490f0a 108 #endif