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

Dependents:   2doejemplo

Committer:
nucho
Date:
Sun Nov 13 02:20:48 2011 +0000
Revision:
3:482383d9a5de
Parent:
2:6a3a047c1ce1

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nucho 2:6a3a047c1ce1 1 #ifndef SABERTOOTH2X25_H
nucho 2:6a3a047c1ce1 2 #define SABERTOOTH2X25_H
nucho 2:6a3a047c1ce1 3
nucho 2:6a3a047c1ce1 4 #include"mbed.h"
nucho 2:6a3a047c1ce1 5
nucho 2:6a3a047c1ce1 6 //command
nucho 2:6a3a047c1ce1 7 #define DRIVE_FORWARD_MOTOR_ONE 0
nucho 2:6a3a047c1ce1 8 #define DRIVE_BACKWARD_MOTOR_ONE 1
nucho 2:6a3a047c1ce1 9 #define MIN_VOLTAGE 2
nucho 2:6a3a047c1ce1 10 #define MAX_VOLTAGE 3
nucho 2:6a3a047c1ce1 11 #define DRIVE_FORWARD_MOTOR_TWO 4
nucho 2:6a3a047c1ce1 12 #define DRIVE_BACKWARD_MOTOR_TWO 5
nucho 2:6a3a047c1ce1 13 #define DRIVE_MOTOR_ONE_7BIT 6
nucho 2:6a3a047c1ce1 14 #define DRIVE_MOTOR_TWO_7BIT 7
nucho 2:6a3a047c1ce1 15 //mixed mode command
nucho 2:6a3a047c1ce1 16 #define DRIVE_FORWARD_MIXED_MODE 8
nucho 2:6a3a047c1ce1 17 #define DRIVE_BACKWARD_MIXED_MODE 9
nucho 2:6a3a047c1ce1 18 #define TURN_RIGHT_MIXED_MODE 10
nucho 2:6a3a047c1ce1 19 #define DRIVE_TURN_LEFT_MIXED_MODE 11
nucho 2:6a3a047c1ce1 20 #define DRIVE_FORWARD_BACK_7BIT 12
nucho 2:6a3a047c1ce1 21 #define TURN_7BIT 13
nucho 2:6a3a047c1ce1 22
nucho 2:6a3a047c1ce1 23 /** Sabertooth2x25 class
nucho 2:6a3a047c1ce1 24 *
nucho 2:6a3a047c1ce1 25 * This class is a class for controlling a motor by Sabertooth2x25(Packetized Serial Mode).
nucho 2:6a3a047c1ce1 26 */
nucho 2:6a3a047c1ce1 27
nucho 2:6a3a047c1ce1 28 class Sabertooth2x25 {
nucho 2:6a3a047c1ce1 29 public:
nucho 2:6a3a047c1ce1 30 Sabertooth2x25(PinName tx, int baudrate);
nucho 2:6a3a047c1ce1 31
nucho 2:6a3a047c1ce1 32 /** This is used to command motor 1 to drive forward.
nucho 2:6a3a047c1ce1 33 * This is used to command motor 1 to drive forward. Valid data is 0-127 for off to full forward drive.
nucho 2:6a3a047c1ce1 34 * If a command of 0 is given, the Sabertooth will go into power save mode for motor 1 after approximately 4 seconds.
nucho 2:6a3a047c1ce1 35 *
nucho 2:6a3a047c1ce1 36 * @param device_address The set device address
nucho 2:6a3a047c1ce1 37 * @param speed motor spped(Valid data is 0-127)
nucho 2:6a3a047c1ce1 38 */
nucho 2:6a3a047c1ce1 39 void forward_motor_1(int device_address, int speed);
nucho 2:6a3a047c1ce1 40
nucho 3:482383d9a5de 41 /**This is used to command motor 1 to drive backward.
nucho 3:482383d9a5de 42 * This is used to command motor 1 to drive backward. Valid data is 0-127 for off to full reverse drive.
nucho 2:6a3a047c1ce1 43 * If a command of 0 is given, Sabertooth will go into power save mode for motor 1 after approximately 4 seconds.
nucho 2:6a3a047c1ce1 44 *
nucho 2:6a3a047c1ce1 45 * @param device_address The set device address
nucho 2:6a3a047c1ce1 46 * @param speed motor spped(Valid data is 0-127)
nucho 2:6a3a047c1ce1 47 */
nucho 3:482383d9a5de 48 void backward_motor_1(int device_address, int speed);
nucho 2:6a3a047c1ce1 49
nucho 2:6a3a047c1ce1 50 /** This is used to set a custom minimum voltage for the battery feeding the Sabertooth.
nucho 2:6a3a047c1ce1 51 * 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 2:6a3a047c1ce1 52 * 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 2:6a3a047c1ce1 53 * Valid data is from 0 to 120. The function for converting volts to command data is
nucho 2:6a3a047c1ce1 54 * Value = (desired volts-6) x 5
nucho 2:6a3a047c1ce1 55 *
nucho 2:6a3a047c1ce1 56 * @param device_address The set device address
nucho 2:6a3a047c1ce1 57 * @param data Value = (desired volts-6) x 5 (Valid data is from 0 to 120)
nucho 2:6a3a047c1ce1 58 */
nucho 2:6a3a047c1ce1 59 void min_voltage(int device_address, int data);
nucho 2:6a3a047c1ce1 60
nucho 2:6a3a047c1ce1 61 /** This is used to set a custom maximum voltage.
nucho 2:6a3a047c1ce1 62 * 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 2:6a3a047c1ce1 63 * 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 2:6a3a047c1ce1 64 * 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 2:6a3a047c1ce1 65 * This is inefficient, because the energy is heating the motor instead of recharging a battery, but may be necessary.
nucho 2:6a3a047c1ce1 66 * 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 2:6a3a047c1ce1 67 * Value = Desired Volts*5.12
nucho 2:6a3a047c1ce1 68 * 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 2:6a3a047c1ce1 69 *
nucho 2:6a3a047c1ce1 70 * @param device_address The set device address
nucho 2:6a3a047c1ce1 71 * @param data Value = Desired Volts*5.12 (0-25v)
nucho 2:6a3a047c1ce1 72 */
nucho 2:6a3a047c1ce1 73 void max_voltage(int device_address, int data);
nucho 2:6a3a047c1ce1 74
nucho 2:6a3a047c1ce1 75 /** This is used to command motor 2 to drive forward.
nucho 2:6a3a047c1ce1 76 * This is used to command motor 2 to drive forward. Valid data is 0-127 for off to full forward drive.
nucho 2:6a3a047c1ce1 77 * If a command of 0 is given, the Sabertooth will go into power save mode for motor 2 after approximately 4 seconds.
nucho 2:6a3a047c1ce1 78 *
nucho 2:6a3a047c1ce1 79 * @param device_address The set device address
nucho 2:6a3a047c1ce1 80 * @param spped motor speed(Valid data is 0-127)
nucho 2:6a3a047c1ce1 81 */
nucho 2:6a3a047c1ce1 82 void forward_motor_2(int device_address, int speed);
nucho 2:6a3a047c1ce1 83
nucho 3:482383d9a5de 84 /** This is used to command motor 2 to drive backward.
nucho 3:482383d9a5de 85 * This is used to command motor 2 to drive backward. Valid data is 0-127 for off to full reverse drive.
nucho 2:6a3a047c1ce1 86 * If a command of 0 is given, the Sabertooth will go into power save mode after approximately 4 seconds.
nucho 2:6a3a047c1ce1 87 *
nucho 2:6a3a047c1ce1 88 * @param device_address The set device address
nucho 2:6a3a047c1ce1 89 * @param data Value = Desired Volts*5.12 (0-25v
nucho 2:6a3a047c1ce1 90 */
nucho 3:482383d9a5de 91 void backward_motor_2(int device_address, int speed);
nucho 2:6a3a047c1ce1 92
nucho 2:6a3a047c1ce1 93 /** This command is used to drive motor 1.
nucho 2:6a3a047c1ce1 94 * 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 2:6a3a047c1ce1 95 * 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 2:6a3a047c1ce1 96 *
nucho 2:6a3a047c1ce1 97 * @param device_address The set device address
nucho 2:6a3a047c1ce1 98 * @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 2:6a3a047c1ce1 99 */
nucho 2:6a3a047c1ce1 100 void drive_motor_1(int device_address, int speed);
nucho 2:6a3a047c1ce1 101
nucho 2:6a3a047c1ce1 102 /** This command is used to drive motor 2.
nucho 2:6a3a047c1ce1 103 * 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 2:6a3a047c1ce1 104 * 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 2:6a3a047c1ce1 105 *
nucho 2:6a3a047c1ce1 106 * @param device_address The set device address
nucho 2:6a3a047c1ce1 107 * @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 2:6a3a047c1ce1 108 */
nucho 2:6a3a047c1ce1 109 void drive_motor_2(int device_address, int speed);
nucho 2:6a3a047c1ce1 110
nucho 2:6a3a047c1ce1 111 /** This is used to command the vehicle to drive forward in mixed mode.
nucho 2:6a3a047c1ce1 112 * 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 2:6a3a047c1ce1 113 *
nucho 2:6a3a047c1ce1 114 * @param device_address The set device address
nucho 2:6a3a047c1ce1 115 * @param speed Valid data is 0-127 for off to full forward drive.
nucho 2:6a3a047c1ce1 116 */
nucho 2:6a3a047c1ce1 117 void drive_forward_mixed_mode(int device_address, int speed);
nucho 2:6a3a047c1ce1 118
nucho 2:6a3a047c1ce1 119 /** This is used to command the vehicle to drive backwards in mixed mode.
nucho 2:6a3a047c1ce1 120 * 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 2:6a3a047c1ce1 121 *
nucho 2:6a3a047c1ce1 122 * @param device_address The set device address
nucho 2:6a3a047c1ce1 123 * @param Valid data is 0-127 for off to full reverse drive.
nucho 2:6a3a047c1ce1 124 */
nucho 3:482383d9a5de 125 void drive_backward_mixed_mode(int device_address, int speed);
nucho 2:6a3a047c1ce1 126
nucho 2:6a3a047c1ce1 127 /** This is used to command the vehicle to turn right in mixed mode.
nucho 2:6a3a047c1ce1 128 * 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 2:6a3a047c1ce1 129 *
nucho 2:6a3a047c1ce1 130 * @param device_address The set device address
nucho 2:6a3a047c1ce1 131 * @param speed Valid data is 0-127 for zero to maximum turning speed.
nucho 2:6a3a047c1ce1 132 */
nucho 2:6a3a047c1ce1 133 void turn_right_mixed_mode(int device_address, int speed);
nucho 2:6a3a047c1ce1 134
nucho 2:6a3a047c1ce1 135 /** This is used to command the vehicle to turn leftt in mixed mode.
nucho 2:6a3a047c1ce1 136 * 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 2:6a3a047c1ce1 137 *
nucho 2:6a3a047c1ce1 138 * @param device_address The set device address
nucho 2:6a3a047c1ce1 139 * @param speed Valid data is 0-127 for zero to maximum turning speed.
nucho 2:6a3a047c1ce1 140 */
nucho 2:6a3a047c1ce1 141 void drive_turn_left_mixed_mode(int device_address, int speed);
nucho 2:6a3a047c1ce1 142
nucho 2:6a3a047c1ce1 143 /** This is used to command the vehicle to move forwards or backwards.
nucho 2:6a3a047c1ce1 144 * 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 2:6a3a047c1ce1 145 *
nucho 2:6a3a047c1ce1 146 * @param device_address The set device address
nucho 2:6a3a047c1ce1 147 * @param speed A command of 0 will cause maximum reverse, 64 will cause the vehicle to stop, and 127 will command full forward.
nucho 2:6a3a047c1ce1 148 */
nucho 2:6a3a047c1ce1 149 void drive_forwards_back(int device_address, int speed);
nucho 2:6a3a047c1ce1 150
nucho 2:6a3a047c1ce1 151 /** This is used to command the vehicle turn right or left.
nucho 2:6a3a047c1ce1 152 * This is used to command the vehicle turn right or left. A command of 0 will cause maximum left turn rate,
nucho 2:6a3a047c1ce1 153 * 64 will cause the vehicle to stop turning , and 127 will command maximum right turn rate.
nucho 2:6a3a047c1ce1 154 *
nucho 2:6a3a047c1ce1 155 * @param device_address The set device address
nucho 2:6a3a047c1ce1 156 * @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 2:6a3a047c1ce1 157 */
nucho 2:6a3a047c1ce1 158 void turn(int device_address, int speed);
nucho 2:6a3a047c1ce1 159
nucho 2:6a3a047c1ce1 160
nucho 2:6a3a047c1ce1 161 private:
nucho 2:6a3a047c1ce1 162 void sendCommand(int address, int command, int data);
nucho 2:6a3a047c1ce1 163 Serial device_;
nucho 2:6a3a047c1ce1 164 unsigned char checksum_;
nucho 2:6a3a047c1ce1 165 };
nucho 2:6a3a047c1ce1 166
nucho 2:6a3a047c1ce1 167
nucho 0:3f784e34179c 168 #endif