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

Dependents:   2doejemplo

Committer:
nucho
Date:
Wed Sep 21 16:55:39 2011 +0000
Revision:
0:3f784e34179c
Child:
1:b8f62e733e56

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nucho 0:3f784e34179c 1 /*
nucho 0:3f784e34179c 2 This library is a library for controlling a motor by Sabertooth2x25(Packetized Serial Mode).
nucho 0:3f784e34179c 3 */
nucho 0:3f784e34179c 4 #ifndef SABERTOOTH2X25_H
nucho 0:3f784e34179c 5 #define SABERTOOTH2X25_H
nucho 0:3f784e34179c 6
nucho 0:3f784e34179c 7 #include"mbed.h"
nucho 0:3f784e34179c 8
nucho 0:3f784e34179c 9 //command
nucho 0:3f784e34179c 10 #define DRIVE_FORWARD_MOTOR_ONE 0
nucho 0:3f784e34179c 11 #define DRIVE_BACKWARD_MOTOR_ONE 1
nucho 0:3f784e34179c 12 #define MIN_VOLTAGE 2
nucho 0:3f784e34179c 13 #define MAX_VOLTAGE 3
nucho 0:3f784e34179c 14 #define DRIVE_FORWARD_MOTOR_TWO 4
nucho 0:3f784e34179c 15 #define DRIVE_BACKWARD_MOTOR_TWO 5
nucho 0:3f784e34179c 16 #define DRIVE_MOTOR_ONE_7BIT 6
nucho 0:3f784e34179c 17 #define DRIVE_MOTOR_TWO_7BIT 7
nucho 0:3f784e34179c 18 //mixed mode command
nucho 0:3f784e34179c 19 #define DRIVE_FORWARD_MIXED_MODE 8
nucho 0:3f784e34179c 20 #define DRIVE_BACKWARD_MIXED_MODE 9
nucho 0:3f784e34179c 21 #define TURN_RIGHT_MIXED_MODE 10
nucho 0:3f784e34179c 22 #define DRIVE_TURN_LEFT_MIXED_MODE 11
nucho 0:3f784e34179c 23 #define DRIVE_FORWARD_BACK_7BIT 12
nucho 0:3f784e34179c 24 #define TURN_7BIT 13
nucho 0:3f784e34179c 25
nucho 0:3f784e34179c 26
nucho 0:3f784e34179c 27 class Sabertooth2x25 {
nucho 0:3f784e34179c 28 public:
nucho 0:3f784e34179c 29 Sabertooth2x25(PinName tx, int baudrate);
nucho 0:3f784e34179c 30
nucho 0:3f784e34179c 31 /**
nucho 0:3f784e34179c 32 * This is used to command motor 1 to drive forward. Valid data is 0-127 for off to full forward drive.
nucho 0:3f784e34179c 33 * If a command of 0 is given, the Sabertooth will go into power save mode for motor 1 after approximately 4 seconds.
nucho 0:3f784e34179c 34 */
nucho 0:3f784e34179c 35 void forward_motor_1(int device_address, int speed);
nucho 0:3f784e34179c 36
nucho 0:3f784e34179c 37 /**
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 0:3f784e34179c 40 */
nucho 0:3f784e34179c 41 void backwards_motor_1(int device_address, int speed);
nucho 0:3f784e34179c 42
nucho 0:3f784e34179c 43 /**
nucho 0:3f784e34179c 44 * 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 45 * 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 46 * Valid data is from 0 to 120. The function for converting volts to command data is
nucho 0:3f784e34179c 47 *
nucho 0:3f784e34179c 48 * Value = (desired volts-6) x 5
nucho 0:3f784e34179c 49 */
nucho 0:3f784e34179c 50 void min_voltage(int device_address, int data);
nucho 0:3f784e34179c 51
nucho 0:3f784e34179c 52 /**
nucho 0:3f784e34179c 53 * 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 54 * 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 55 * 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 56 * This is inefficient, because the energy is heating the motor instead of recharging a battery, but may be necessary.
nucho 0:3f784e34179c 57 * 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 0:3f784e34179c 58 *
nucho 0:3f784e34179c 59 * Value = Desired Volts*5.12
nucho 0:3f784e34179c 60 *
nucho 0:3f784e34179c 61 * 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 62 */
nucho 0:3f784e34179c 63 void max_voltage(int device_address, int data);
nucho 0:3f784e34179c 64
nucho 0:3f784e34179c 65 /**
nucho 0:3f784e34179c 66 * This is used to command motor 2 to drive forward. Valid data is 0-127 for off to full forward drive.
nucho 0:3f784e34179c 67 * If a command of 0 is given, the Sabertooth will go into power save mode for motor 2 after approximately 4 seconds.
nucho 0:3f784e34179c 68 */
nucho 0:3f784e34179c 69 void forward_motor_2(int device_address, int speed);
nucho 0:3f784e34179c 70
nucho 0:3f784e34179c 71 /**
nucho 0:3f784e34179c 72 * This is used to command motor 2 to drive backwards. Valid data is 0-127 for off to full reverse drive.
nucho 0:3f784e34179c 73 * If a command of 0 is given, the Sabertooth will go into power save mode after approximately 4 seconds.
nucho 0:3f784e34179c 74 */
nucho 0:3f784e34179c 75 void backwards_motor_2(int device_address, int speed);
nucho 0:3f784e34179c 76
nucho 0:3f784e34179c 77 /**
nucho 0:3f784e34179c 78 * 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 79 * 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 0:3f784e34179c 80 */
nucho 0:3f784e34179c 81 void drive_motor_1(int device_address, int speed);
nucho 0:3f784e34179c 82
nucho 0:3f784e34179c 83 /**
nucho 0:3f784e34179c 84 * 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 85 * 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 0:3f784e34179c 86 */
nucho 0:3f784e34179c 87 void drive_motor_2(int device_address, int speed);
nucho 0:3f784e34179c 88
nucho 0:3f784e34179c 89 /**
nucho 0:3f784e34179c 90 * 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 0:3f784e34179c 91 */
nucho 0:3f784e34179c 92 void drive_forward_mixed_mode(int device_address, int speed);
nucho 0:3f784e34179c 93
nucho 0:3f784e34179c 94 /**
nucho 0:3f784e34179c 95 * 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 0:3f784e34179c 96 */
nucho 0:3f784e34179c 97 void drive_backwards_mixed_mode(int device_address, int speed);
nucho 0:3f784e34179c 98
nucho 0:3f784e34179c 99 /**
nucho 0:3f784e34179c 100 * 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 0:3f784e34179c 101 */
nucho 0:3f784e34179c 102 void turn_right_mixed_mode(int device_address, int speed);
nucho 0:3f784e34179c 103
nucho 0:3f784e34179c 104 /**
nucho 0:3f784e34179c 105 * 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 0:3f784e34179c 106 */
nucho 0:3f784e34179c 107 void drive_turn_left_mixed_mode(int device_address, int speed);
nucho 0:3f784e34179c 108
nucho 0:3f784e34179c 109 /**
nucho 0:3f784e34179c 110 * 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 0:3f784e34179c 111 */
nucho 0:3f784e34179c 112 void drive_forwards_back(int device_address, int speed);
nucho 0:3f784e34179c 113
nucho 0:3f784e34179c 114 /**
nucho 0:3f784e34179c 115 * This is used to command the vehicle turn right or left. A command of 0 will cause maximum left turn rate,
nucho 0:3f784e34179c 116 * 64 will cause the vehicle to stop turning , and 127 will command maximum right turn rate.
nucho 0:3f784e34179c 117 */
nucho 0:3f784e34179c 118 void turn(int device_address, int speed);
nucho 0:3f784e34179c 119
nucho 0:3f784e34179c 120
nucho 0:3f784e34179c 121 private:
nucho 0:3f784e34179c 122 void sendCommand(int address, int command, int data);
nucho 0:3f784e34179c 123 Serial device_;
nucho 0:3f784e34179c 124 unsigned char tx_buf[4];
nucho 0:3f784e34179c 125 };
nucho 0:3f784e34179c 126
nucho 0:3f784e34179c 127
nucho 0:3f784e34179c 128 #endif