This library is a library for controlling a motor by Sabertooth2x25(Packetized Serial Mode).

Dependents:   2doejemplo

Committer:
nucho
Date:
Fri Sep 23 13:06:59 2011 +0000
Revision:
1:b8f62e733e56
Parent:
0:3f784e34179c
Child:
2:6a3a047c1ce1

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nucho 0:3f784e34179c 1 #ifndef SABERTOOTH2X25_H
nucho 0:3f784e34179c 2 #define SABERTOOTH2X25_H
nucho 0:3f784e34179c 3
nucho 0:3f784e34179c 4 #include"mbed.h"
nucho 0:3f784e34179c 5
nucho 0:3f784e34179c 6 //command
nucho 0:3f784e34179c 7 #define DRIVE_FORWARD_MOTOR_ONE 0
nucho 0:3f784e34179c 8 #define DRIVE_BACKWARD_MOTOR_ONE 1
nucho 0:3f784e34179c 9 #define MIN_VOLTAGE 2
nucho 0:3f784e34179c 10 #define MAX_VOLTAGE 3
nucho 0:3f784e34179c 11 #define DRIVE_FORWARD_MOTOR_TWO 4
nucho 0:3f784e34179c 12 #define DRIVE_BACKWARD_MOTOR_TWO 5
nucho 0:3f784e34179c 13 #define DRIVE_MOTOR_ONE_7BIT 6
nucho 0:3f784e34179c 14 #define DRIVE_MOTOR_TWO_7BIT 7
nucho 0:3f784e34179c 15 //mixed mode command
nucho 0:3f784e34179c 16 #define DRIVE_FORWARD_MIXED_MODE 8
nucho 0:3f784e34179c 17 #define DRIVE_BACKWARD_MIXED_MODE 9
nucho 0:3f784e34179c 18 #define TURN_RIGHT_MIXED_MODE 10
nucho 0:3f784e34179c 19 #define DRIVE_TURN_LEFT_MIXED_MODE 11
nucho 0:3f784e34179c 20 #define DRIVE_FORWARD_BACK_7BIT 12
nucho 0:3f784e34179c 21 #define TURN_7BIT 13
nucho 0:3f784e34179c 22
nucho 0:3f784e34179c 23
nucho 0:3f784e34179c 24 class Sabertooth2x25 {
nucho 0:3f784e34179c 25 public:
nucho 0:3f784e34179c 26 Sabertooth2x25(PinName tx, int baudrate);
nucho 0:3f784e34179c 27
nucho 1:b8f62e733e56 28 /** This is used to command motor 1 to drive forward.
nucho 0:3f784e34179c 29 * This is used to command motor 1 to drive forward. Valid data is 0-127 for off to full forward drive.
nucho 0:3f784e34179c 30 * If a command of 0 is given, the Sabertooth will go into power save mode for motor 1 after approximately 4 seconds.
nucho 1:b8f62e733e56 31 *
nucho 1:b8f62e733e56 32 * @param device_address The set device address
nucho 1:b8f62e733e56 33 * @param speed motor spped(Valid data is 0-127)
nucho 0:3f784e34179c 34 */
nucho 0:3f784e34179c 35 void forward_motor_1(int device_address, int speed);
nucho 0:3f784e34179c 36
nucho 1:b8f62e733e56 37 /**This is used to command motor 1 to drive backwards.
nucho 0:3f784e34179c 38 * This is used to command motor 1 to drive backwards. Valid data is 0-127 for off to full reverse drive.
nucho 0:3f784e34179c 39 * If a command of 0 is given, Sabertooth will go into power save mode for motor 1 after approximately 4 seconds.
nucho 1:b8f62e733e56 40 *
nucho 1:b8f62e733e56 41 * @param device_address The set device address
nucho 1:b8f62e733e56 42 * @param speed motor spped(Valid data is 0-127)
nucho 0:3f784e34179c 43 */
nucho 0:3f784e34179c 44 void backwards_motor_1(int device_address, int speed);
nucho 0:3f784e34179c 45
nucho 1:b8f62e733e56 46 /** This is used to set a custom minimum voltage for the battery feeding the Sabertooth.
nucho 0:3f784e34179c 47 * This is used to set a custom minimum voltage for the battery feeding the Sabertooth. If the battery voltage drops below this value, the output will shut down.
nucho 0:3f784e34179c 48 * This value is cleared at startup, so much be set each run. The value is sent in .2 volt increments with a command of zero corresponding to 6v, which is the minimum.
nucho 0:3f784e34179c 49 * Valid data is from 0 to 120. The function for converting volts to command data is
nucho 1:b8f62e733e56 50 * Value = (desired volts-6) x 5
nucho 0:3f784e34179c 51 *
nucho 1:b8f62e733e56 52 * @param device_address The set device address
nucho 1:b8f62e733e56 53 * @param data Value = (desired volts-6) x 5 (Valid data is from 0 to 120)
nucho 0:3f784e34179c 54 */
nucho 0:3f784e34179c 55 void min_voltage(int device_address, int data);
nucho 0:3f784e34179c 56
nucho 1:b8f62e733e56 57 /** This is used to set a custom maximum voltage.
nucho 0:3f784e34179c 58 * This is used to set a custom maximum voltage. If you are using a power supply that cannot sink current such as an ATX supply,
nucho 0:3f784e34179c 59 * the input voltage will rise when the driver is regenerating (slowing down the motor) Many ATX type supplies will shut down if the output voltage on the 12v supply rises beyond 16v.
nucho 0:3f784e34179c 60 * If the driver detects an input voltage above the set limit, it will put the motor into a hard brake until the voltage drops below the set point again.
nucho 0:3f784e34179c 61 * This is inefficient, because the energy is heating the motor instead of recharging a battery, but may be necessary.
nucho 0:3f784e34179c 62 * The driver comes preset for a maximum voltage of 30V. The range for a custom maximum voltage is 0v-25v. The formula for setting a custom maximum voltage is
nucho 1:b8f62e733e56 63 * Value = Desired Volts*5.12
nucho 1:b8f62e733e56 64 * If you are using any sort of battery, then this is not a problem and the max voltage should be left at the startup default.
nucho 0:3f784e34179c 65 *
nucho 1:b8f62e733e56 66 * @param device_address The set device address
nucho 1:b8f62e733e56 67 * @param data Value = Desired Volts*5.12 (0-25v)
nucho 0:3f784e34179c 68 */
nucho 0:3f784e34179c 69 void max_voltage(int device_address, int data);
nucho 0:3f784e34179c 70
nucho 1:b8f62e733e56 71 /** This is used to command motor 2 to drive forward.
nucho 0:3f784e34179c 72 * This is used to command motor 2 to drive forward. Valid data is 0-127 for off to full forward drive.
nucho 0:3f784e34179c 73 * If a command of 0 is given, the Sabertooth will go into power save mode for motor 2 after approximately 4 seconds.
nucho 1:b8f62e733e56 74 *
nucho 1:b8f62e733e56 75 * @param device_address The set device address
nucho 1:b8f62e733e56 76 * @param spped motor speed(Valid data is 0-127)
nucho 0:3f784e34179c 77 */
nucho 0:3f784e34179c 78 void forward_motor_2(int device_address, int speed);
nucho 0:3f784e34179c 79
nucho 1:b8f62e733e56 80 /** This is used to command motor 2 to drive backwards.
nucho 0:3f784e34179c 81 * This is used to command motor 2 to drive backwards. Valid data is 0-127 for off to full reverse drive.
nucho 1:b8f62e733e56 82 * If a command of 0 is given, the Sabertooth will go into power save mode after approximately 4 seconds.
nucho 1:b8f62e733e56 83 *
nucho 1:b8f62e733e56 84 * @param device_address The set device address
nucho 1:b8f62e733e56 85 * @param data Value = Desired Volts*5.12 (0-25v
nucho 0:3f784e34179c 86 */
nucho 0:3f784e34179c 87 void backwards_motor_2(int device_address, int speed);
nucho 0:3f784e34179c 88
nucho 1:b8f62e733e56 89 /** This command is used to drive motor 1.
nucho 0:3f784e34179c 90 * This command is used to drive motor 1. Instead of the standard commands 0 and 1, this one command can be used to drive motor 1 forward or in reverse,
nucho 0:3f784e34179c 91 * at a cost of lower resolution. A command of 0 will correspond to full reverse, and a command of 127 will command the motor to drive full forward. A command of 64 will stop the motor.
nucho 1:b8f62e733e56 92 *
nucho 1:b8f62e733e56 93 * @param device_address The set device address
nucho 1:b8f62e733e56 94 * @param data A command of 0 will correspond to full reverse, and a command of 127 will command the motor to drive full forward.
nucho 0:3f784e34179c 95 */
nucho 0:3f784e34179c 96 void drive_motor_1(int device_address, int speed);
nucho 0:3f784e34179c 97
nucho 1:b8f62e733e56 98 /** This command is used to drive motor 2.
nucho 0:3f784e34179c 99 * This command is used to drive motor 2. Instead of the standard commands 4 and 5, this one command can be used to drive motor 1 forward or in reverse,
nucho 0:3f784e34179c 100 * at a cost of lower resolution. A command of 0 will correspond to full reverse, and a command of 127 will command the motor to drive full forward. A command of 64 will stop the motor.
nucho 1:b8f62e733e56 101 *
nucho 1:b8f62e733e56 102 * @param device_address The set device address
nucho 1:b8f62e733e56 103 * @param data A command of 0 will correspond to full reverse, and a command of 127 will command the motor to drive full forward.
nucho 0:3f784e34179c 104 */
nucho 0:3f784e34179c 105 void drive_motor_2(int device_address, int speed);
nucho 0:3f784e34179c 106
nucho 1:b8f62e733e56 107 /** This is used to command the vehicle to drive forward in mixed mode.
nucho 0:3f784e34179c 108 * This is used to command the vehicle to drive forward in mixed mode. Valid data is 0-127 for off to full forward drive.
nucho 1:b8f62e733e56 109 *
nucho 1:b8f62e733e56 110 * @param device_address The set device address
nucho 1:b8f62e733e56 111 * @param speed Valid data is 0-127 for off to full forward drive.
nucho 0:3f784e34179c 112 */
nucho 0:3f784e34179c 113 void drive_forward_mixed_mode(int device_address, int speed);
nucho 0:3f784e34179c 114
nucho 1:b8f62e733e56 115 /** This is used to command the vehicle to drive backwards in mixed mode.
nucho 0:3f784e34179c 116 * This is used to command the vehicle to drive backwards in mixed mode. Valid data is 0-127 for off to full reverse drive.
nucho 1:b8f62e733e56 117 *
nucho 1:b8f62e733e56 118 * @param device_address The set device address
nucho 1:b8f62e733e56 119 * @param Valid data is 0-127 for off to full reverse drive.
nucho 0:3f784e34179c 120 */
nucho 0:3f784e34179c 121 void drive_backwards_mixed_mode(int device_address, int speed);
nucho 0:3f784e34179c 122
nucho 1:b8f62e733e56 123 /** This is used to command the vehicle to turn right in mixed mode.
nucho 0:3f784e34179c 124 * This is used to command the vehicle to turn right in mixed mode. Valid data is 0-127 for zero to maximum turning speed.
nucho 1:b8f62e733e56 125 *
nucho 1:b8f62e733e56 126 * @param device_address The set device address
nucho 1:b8f62e733e56 127 * @param speed Valid data is 0-127 for zero to maximum turning speed.
nucho 0:3f784e34179c 128 */
nucho 0:3f784e34179c 129 void turn_right_mixed_mode(int device_address, int speed);
nucho 0:3f784e34179c 130
nucho 1:b8f62e733e56 131 /** This is used to command the vehicle to turn leftt in mixed mode.
nucho 0:3f784e34179c 132 * This is used to command the vehicle to turn leftt in mixed mode. Valid data is 0-127 for zero to maximum turning speed.
nucho 1:b8f62e733e56 133 *
nucho 1:b8f62e733e56 134 * @param device_address The set device address
nucho 1:b8f62e733e56 135 * @param speed Valid data is 0-127 for zero to maximum turning speed.
nucho 0:3f784e34179c 136 */
nucho 0:3f784e34179c 137 void drive_turn_left_mixed_mode(int device_address, int speed);
nucho 0:3f784e34179c 138
nucho 1:b8f62e733e56 139 /** This is used to command the vehicle to move forwards or backwards.
nucho 0:3f784e34179c 140 * This is used to command the vehicle to move forwards or backwards. A command of 0 will cause maximum reverse, 64 will cause the vehicle to stop, and 127 will command full forward.
nucho 1:b8f62e733e56 141 *
nucho 1:b8f62e733e56 142 * @param device_address The set device address
nucho 1:b8f62e733e56 143 * @param speed A command of 0 will cause maximum reverse, 64 will cause the vehicle to stop, and 127 will command full forward.
nucho 0:3f784e34179c 144 */
nucho 0:3f784e34179c 145 void drive_forwards_back(int device_address, int speed);
nucho 0:3f784e34179c 146
nucho 1:b8f62e733e56 147 /** This is used to command the vehicle turn right or left.
nucho 0:3f784e34179c 148 * This is used to command the vehicle turn right or left. A command of 0 will cause maximum left turn rate,
nucho 0:3f784e34179c 149 * 64 will cause the vehicle to stop turning , and 127 will command maximum right turn rate.
nucho 1:b8f62e733e56 150 *
nucho 1:b8f62e733e56 151 * @param device_address The set device address
nucho 1:b8f62e733e56 152 * @param spped . A command of 0 will cause maximum left turn rate,64 will cause the vehicle to stop turning , and 127 will command maximum right turn rate.
nucho 0:3f784e34179c 153 */
nucho 0:3f784e34179c 154 void turn(int device_address, int speed);
nucho 0:3f784e34179c 155
nucho 0:3f784e34179c 156
nucho 0:3f784e34179c 157 private:
nucho 0:3f784e34179c 158 void sendCommand(int address, int command, int data);
nucho 0:3f784e34179c 159 Serial device_;
nucho 1:b8f62e733e56 160 unsigned char checksum_;
nucho 0:3f784e34179c 161 };
nucho 0:3f784e34179c 162
nucho 0:3f784e34179c 163
nucho 0:3f784e34179c 164 #endif