Library for Sabertooth motor driver ported from the oficial Arduino library. Packetized Serial Mode
I ported the official arduino library for Sabertooth dual motor drivers. In the porting process I did not used const methods as the original library does, but the functionality stays the same. I also added some methods that I find useful. According to the documentation of the driver this code should be compatible with SyRen drivers.
Sabertooth.h@0:70797f033fe0, 2017-02-20 (annotated)
- Committer:
- Elefantul_umilit
- Date:
- Mon Feb 20 11:10:08 2017 +0000
- Revision:
- 0:70797f033fe0
Library for Sabertooth ported from the oficial Arduino library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Elefantul_umilit | 0:70797f033fe0 | 1 | /* |
Elefantul_umilit | 0:70797f033fe0 | 2 | Arduino Library for SyRen/Sabertooth Packet Serial |
Elefantul_umilit | 0:70797f033fe0 | 3 | Copyright (c) 2012-2013 Dimension Engineering LLC |
Elefantul_umilit | 0:70797f033fe0 | 4 | http://www.dimensionengineering.com/arduino |
Elefantul_umilit | 0:70797f033fe0 | 5 | |
Elefantul_umilit | 0:70797f033fe0 | 6 | Permission to use, copy, modify, and/or distribute this software for any |
Elefantul_umilit | 0:70797f033fe0 | 7 | purpose with or without fee is hereby granted, provided that the above |
Elefantul_umilit | 0:70797f033fe0 | 8 | copyright notice and this permission notice appear in all copies. |
Elefantul_umilit | 0:70797f033fe0 | 9 | |
Elefantul_umilit | 0:70797f033fe0 | 10 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
Elefantul_umilit | 0:70797f033fe0 | 11 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
Elefantul_umilit | 0:70797f033fe0 | 12 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
Elefantul_umilit | 0:70797f033fe0 | 13 | SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
Elefantul_umilit | 0:70797f033fe0 | 14 | RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
Elefantul_umilit | 0:70797f033fe0 | 15 | NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE |
Elefantul_umilit | 0:70797f033fe0 | 16 | USE OR PERFORMANCE OF THIS SOFTWARE. |
Elefantul_umilit | 0:70797f033fe0 | 17 | */ |
Elefantul_umilit | 0:70797f033fe0 | 18 | |
Elefantul_umilit | 0:70797f033fe0 | 19 | /* |
Elefantul_umilit | 0:70797f033fe0 | 20 | * Ported to mbed by Litan Virgil, 2017 |
Elefantul_umilit | 0:70797f033fe0 | 21 | * I added some features that I found useful. |
Elefantul_umilit | 0:70797f033fe0 | 22 | * Also I removed "const" methods |
Elefantul_umilit | 0:70797f033fe0 | 23 | */ |
Elefantul_umilit | 0:70797f033fe0 | 24 | |
Elefantul_umilit | 0:70797f033fe0 | 25 | #ifndef _SABERTOOTH_H_ |
Elefantul_umilit | 0:70797f033fe0 | 26 | #define _SABERTOOTH_H_ |
Elefantul_umilit | 0:70797f033fe0 | 27 | |
Elefantul_umilit | 0:70797f033fe0 | 28 | #include "mbed.h" |
Elefantul_umilit | 0:70797f033fe0 | 29 | |
Elefantul_umilit | 0:70797f033fe0 | 30 | #define START_CHARACTER 0xAA |
Elefantul_umilit | 0:70797f033fe0 | 31 | |
Elefantul_umilit | 0:70797f033fe0 | 32 | class Sabertooth { |
Elefantul_umilit | 0:70797f033fe0 | 33 | |
Elefantul_umilit | 0:70797f033fe0 | 34 | public: |
Elefantul_umilit | 0:70797f033fe0 | 35 | /*! |
Elefantul_umilit | 0:70797f033fe0 | 36 | Initializes a new instance of the Sabertooth class. |
Elefantul_umilit | 0:70797f033fe0 | 37 | The driver address and the TX serial pin used is set to the value given, and the baudrate |
Elefantul_umilit | 0:70797f033fe0 | 38 | \param tx The TX pin used. |
Elefantul_umilit | 0:70797f033fe0 | 39 | \param address The driver address. |
Elefantul_umilit | 0:70797f033fe0 | 40 | \param baudrate The baurate of the serial port. |
Elefantul_umilit | 0:70797f033fe0 | 41 | */ |
Elefantul_umilit | 0:70797f033fe0 | 42 | Sabertooth(PinName tx, uint8_t address, int baudrate); |
Elefantul_umilit | 0:70797f033fe0 | 43 | |
Elefantul_umilit | 0:70797f033fe0 | 44 | /* |
Elefantul_umilit | 0:70797f033fe0 | 45 | Initializes the comunication eith the driver |
Elefantul_umilit | 0:70797f033fe0 | 46 | */ |
Elefantul_umilit | 0:70797f033fe0 | 47 | void initializeComunication(void); |
Elefantul_umilit | 0:70797f033fe0 | 48 | /*! |
Elefantul_umilit | 0:70797f033fe0 | 49 | Sets the power of motor 1. |
Elefantul_umilit | 0:70797f033fe0 | 50 | \param power The power, between -127 and 127. |
Elefantul_umilit | 0:70797f033fe0 | 51 | */ |
Elefantul_umilit | 0:70797f033fe0 | 52 | void motor(int power); |
Elefantul_umilit | 0:70797f033fe0 | 53 | /*! |
Elefantul_umilit | 0:70797f033fe0 | 54 | Sets the power of the specified motor. |
Elefantul_umilit | 0:70797f033fe0 | 55 | \param motor The motor number, 1 or 2. |
Elefantul_umilit | 0:70797f033fe0 | 56 | \param power The power, between -127 and 127. |
Elefantul_umilit | 0:70797f033fe0 | 57 | */ |
Elefantul_umilit | 0:70797f033fe0 | 58 | void motor(uint8_t motor, int power); |
Elefantul_umilit | 0:70797f033fe0 | 59 | /*! |
Elefantul_umilit | 0:70797f033fe0 | 60 | Sets the driving power. |
Elefantul_umilit | 0:70797f033fe0 | 61 | \param power The power, between -127 and 127. |
Elefantul_umilit | 0:70797f033fe0 | 62 | */ |
Elefantul_umilit | 0:70797f033fe0 | 63 | void drive(int power); |
Elefantul_umilit | 0:70797f033fe0 | 64 | /*! |
Elefantul_umilit | 0:70797f033fe0 | 65 | Sets the turning power. |
Elefantul_umilit | 0:70797f033fe0 | 66 | \param power The power, between -127 and 127. |
Elefantul_umilit | 0:70797f033fe0 | 67 | */ |
Elefantul_umilit | 0:70797f033fe0 | 68 | void turn(int power); |
Elefantul_umilit | 0:70797f033fe0 | 69 | /*! |
Elefantul_umilit | 0:70797f033fe0 | 70 | Stops. |
Elefantul_umilit | 0:70797f033fe0 | 71 | */ |
Elefantul_umilit | 0:70797f033fe0 | 72 | void stop(); |
Elefantul_umilit | 0:70797f033fe0 | 73 | /*! |
Elefantul_umilit | 0:70797f033fe0 | 74 | sets the speed of each motor |
Elefantul_umilit | 0:70797f033fe0 | 75 | \param aPower The power for motor a |
Elefantul_umilit | 0:70797f033fe0 | 76 | \param bPower The power of motor b |
Elefantul_umilit | 0:70797f033fe0 | 77 | */ |
Elefantul_umilit | 0:70797f033fe0 | 78 | void go(int aPower, int bPower); |
Elefantul_umilit | 0:70797f033fe0 | 79 | /*! |
Elefantul_umilit | 0:70797f033fe0 | 80 | sets the drive and the turn power |
Elefantul_umilit | 0:70797f033fe0 | 81 | the driver must receive both the drive and turn commands in order to |
Elefantul_umilit | 0:70797f033fe0 | 82 | apply them to the motors |
Elefantul_umilit | 0:70797f033fe0 | 83 | \param drivePower The power for driving |
Elefantul_umilit | 0:70797f033fe0 | 84 | \param turnPower The power for turning |
Elefantul_umilit | 0:70797f033fe0 | 85 | */ |
Elefantul_umilit | 0:70797f033fe0 | 86 | void goDT(int drivePower, int turnPower); |
Elefantul_umilit | 0:70797f033fe0 | 87 | /*! |
Elefantul_umilit | 0:70797f033fe0 | 88 | sets the speed of each motor for a limitet amount of time |
Elefantul_umilit | 0:70797f033fe0 | 89 | \param aPower The power for motor a |
Elefantul_umilit | 0:70797f033fe0 | 90 | \param bPower The power of motor b |
Elefantul_umilit | 0:70797f033fe0 | 91 | \param secs The amount of time after which the motors Stops |
Elefantul_umilit | 0:70797f033fe0 | 92 | */ |
Elefantul_umilit | 0:70797f033fe0 | 93 | void go(int aSpeed, int bSpeed, float secs); |
Elefantul_umilit | 0:70797f033fe0 | 94 | /*! |
Elefantul_umilit | 0:70797f033fe0 | 95 | sets the drive and the turn power for a limited amount of time |
Elefantul_umilit | 0:70797f033fe0 | 96 | the driver must receive both the drive and turn commands in order to |
Elefantul_umilit | 0:70797f033fe0 | 97 | apply them to the motors |
Elefantul_umilit | 0:70797f033fe0 | 98 | \param drivePower The power for driving |
Elefantul_umilit | 0:70797f033fe0 | 99 | \param turnPower The power for turning |
Elefantul_umilit | 0:70797f033fe0 | 100 | \param secs The amount of time after which the motors Stops |
Elefantul_umilit | 0:70797f033fe0 | 101 | */ |
Elefantul_umilit | 0:70797f033fe0 | 102 | void goDT(int drivePower, int turnPower, float secs); |
Elefantul_umilit | 0:70797f033fe0 | 103 | /*! |
Elefantul_umilit | 0:70797f033fe0 | 104 | Sets the minimum voltage. |
Elefantul_umilit | 0:70797f033fe0 | 105 | \param value The voltage. The units of this value are driver-specific and are specified in the Packet Serial chapter of the driver's user manual. |
Elefantul_umilit | 0:70797f033fe0 | 106 | */ |
Elefantul_umilit | 0:70797f033fe0 | 107 | void setMinVoltage(uint8_t value); |
Elefantul_umilit | 0:70797f033fe0 | 108 | /*! |
Elefantul_umilit | 0:70797f033fe0 | 109 | Sets the maximum voltage. |
Elefantul_umilit | 0:70797f033fe0 | 110 | Maximum voltage is stored in EEPROM, so changes persist between power cycles. |
Elefantul_umilit | 0:70797f033fe0 | 111 | \param value The voltage. The units of this value are driver-specific and are specified in the Packet Serial chapter of the driver's user manual. |
Elefantul_umilit | 0:70797f033fe0 | 112 | */ |
Elefantul_umilit | 0:70797f033fe0 | 113 | void setMaxVoltage(uint8_t value); |
Elefantul_umilit | 0:70797f033fe0 | 114 | /*! |
Elefantul_umilit | 0:70797f033fe0 | 115 | Sets the deadband. |
Elefantul_umilit | 0:70797f033fe0 | 116 | Deadband is stored in EEPROM, so changes persist between power cycles. |
Elefantul_umilit | 0:70797f033fe0 | 117 | \param value The deadband value. |
Elefantul_umilit | 0:70797f033fe0 | 118 | Motor powers in the range [-deadband, deadband] will be considered in the deadband, and will |
Elefantul_umilit | 0:70797f033fe0 | 119 | not prevent the driver from entering nor cause the driver to leave an idle brake state. |
Elefantul_umilit | 0:70797f033fe0 | 120 | 0 resets to the default, which is 3. |
Elefantul_umilit | 0:70797f033fe0 | 121 | */ |
Elefantul_umilit | 0:70797f033fe0 | 122 | void setDeadband(uint8_t value); |
Elefantul_umilit | 0:70797f033fe0 | 123 | /*! |
Elefantul_umilit | 0:70797f033fe0 | 124 | Sets the ramping. |
Elefantul_umilit | 0:70797f033fe0 | 125 | Ramping is stored in EEPROM, so changes persist between power cycles. |
Elefantul_umilit | 0:70797f033fe0 | 126 | \param value The ramping value. Consult the user manual for possible values. |
Elefantul_umilit | 0:70797f033fe0 | 127 | */ |
Elefantul_umilit | 0:70797f033fe0 | 128 | void setRamping(uint8_t value); |
Elefantul_umilit | 0:70797f033fe0 | 129 | /*! |
Elefantul_umilit | 0:70797f033fe0 | 130 | Sets the serial timeout. |
Elefantul_umilit | 0:70797f033fe0 | 131 | \param milliseconds The maximum time in milliseconds between packets. If this time is exceeded, |
Elefantul_umilit | 0:70797f033fe0 | 132 | the driver will stop the motors. This value is rounded up to the nearest 100 milliseconds. |
Elefantul_umilit | 0:70797f033fe0 | 133 | This library assumes the command value is in units of 100 milliseconds. This is true for |
Elefantul_umilit | 0:70797f033fe0 | 134 | most drivers, but not all. Check the packet serial chapter of the driver's user manual |
Elefantul_umilit | 0:70797f033fe0 | 135 | to make sure. |
Elefantul_umilit | 0:70797f033fe0 | 136 | */ |
Elefantul_umilit | 0:70797f033fe0 | 137 | void setTimeout(int milliseconds); |
Elefantul_umilit | 0:70797f033fe0 | 138 | /*! |
Elefantul_umilit | 0:70797f033fe0 | 139 | Sets the baud rate. |
Elefantul_umilit | 0:70797f033fe0 | 140 | Baud rate is stored in EEPROM, so changes persist between power cycles. |
Elefantul_umilit | 0:70797f033fe0 | 141 | \param baudRate The baud rate. This can be 2400, 9600, 19200, 38400, or on some drivers 115200. |
Elefantul_umilit | 0:70797f033fe0 | 142 | */ |
Elefantul_umilit | 0:70797f033fe0 | 143 | void setBaudRate(int baudRate); |
Elefantul_umilit | 0:70797f033fe0 | 144 | private: |
Elefantul_umilit | 0:70797f033fe0 | 145 | /*! |
Elefantul_umilit | 0:70797f033fe0 | 146 | Sends a packet serial command to the motor driver. |
Elefantul_umilit | 0:70797f033fe0 | 147 | \param command The number of the command. |
Elefantul_umilit | 0:70797f033fe0 | 148 | \param value The command's value. |
Elefantul_umilit | 0:70797f033fe0 | 149 | */ |
Elefantul_umilit | 0:70797f033fe0 | 150 | void command(uint8_t command, uint8_t value); |
Elefantul_umilit | 0:70797f033fe0 | 151 | void throttleCommand(uint8_t command, int power); |
Elefantul_umilit | 0:70797f033fe0 | 152 | uint8_t _address; |
Elefantul_umilit | 0:70797f033fe0 | 153 | Serial _sabertoothserial; |
Elefantul_umilit | 0:70797f033fe0 | 154 | Timeout timeOff; |
Elefantul_umilit | 0:70797f033fe0 | 155 | }; |
Elefantul_umilit | 0:70797f033fe0 | 156 | |
Elefantul_umilit | 0:70797f033fe0 | 157 | #endif |
Elefantul_umilit | 0:70797f033fe0 | 158 |