Packetized interface with sabertooth motor controller

Dependents:   Camera_Gimbal_Tracking

Committer:
bediyap
Date:
Thu Feb 10 19:57:24 2011 +0000
Revision:
0:8ad928968f9e

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bediyap 0:8ad928968f9e 1 /**
bediyap 0:8ad928968f9e 2 * @author Birdy
bediyap 0:8ad928968f9e 3 *
bediyap 0:8ad928968f9e 4 * @section LICENSE
bediyap 0:8ad928968f9e 5 *
bediyap 0:8ad928968f9e 6 * Copyright (c) 2010 ARM Limited
bediyap 0:8ad928968f9e 7 *
bediyap 0:8ad928968f9e 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
bediyap 0:8ad928968f9e 9 * of this software and associated documentation files (the "Software"), to deal
bediyap 0:8ad928968f9e 10 * in the Software without restriction, including without limitation the rights
bediyap 0:8ad928968f9e 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
bediyap 0:8ad928968f9e 12 * copies of the Software, and to permit persons to whom the Software is
bediyap 0:8ad928968f9e 13 * furnished to do so, subject to the following conditions:
bediyap 0:8ad928968f9e 14 *
bediyap 0:8ad928968f9e 15 * The above copyright notice and this permission notice shall be included in
bediyap 0:8ad928968f9e 16 * all copies or substantial portions of the Software.
bediyap 0:8ad928968f9e 17 *
bediyap 0:8ad928968f9e 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
bediyap 0:8ad928968f9e 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
bediyap 0:8ad928968f9e 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
bediyap 0:8ad928968f9e 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
bediyap 0:8ad928968f9e 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
bediyap 0:8ad928968f9e 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
bediyap 0:8ad928968f9e 24 * THE SOFTWARE.
bediyap 0:8ad928968f9e 25 *
bediyap 0:8ad928968f9e 26 * @section DESCRIPTION
bediyap 0:8ad928968f9e 27 *
bediyap 0:8ad928968f9e 28 * Sabertooth motor drivers
bediyap 0:8ad928968f9e 29 *
bediyap 0:8ad928968f9e 30 * Website:
bediyap 0:8ad928968f9e 31 * http://www.dimensionengineering.com/
bediyap 0:8ad928968f9e 32 *
bediyap 0:8ad928968f9e 33 */
bediyap 0:8ad928968f9e 34
bediyap 0:8ad928968f9e 35 /**
bediyap 0:8ad928968f9e 36 * Includes
bediyap 0:8ad928968f9e 37 */
bediyap 0:8ad928968f9e 38 #include "Sabertooth.h"
bediyap 0:8ad928968f9e 39
bediyap 0:8ad928968f9e 40 Sabertooth::Sabertooth(PinName Tx, int address, int baudrate) : _sabertoothserial(Tx, NC) {
bediyap 0:8ad928968f9e 41
bediyap 0:8ad928968f9e 42 _Address = address;
bediyap 0:8ad928968f9e 43 _sabertoothserial.baud(baudrate);
bediyap 0:8ad928968f9e 44 // Initialize Current Speed
bediyap 0:8ad928968f9e 45 _CurrentSpeed[A] = 0;
bediyap 0:8ad928968f9e 46 _CurrentSpeed[B] = 0;
bediyap 0:8ad928968f9e 47
bediyap 0:8ad928968f9e 48 }
bediyap 0:8ad928968f9e 49
bediyap 0:8ad928968f9e 50 void Sabertooth::InitializeCom(void){
bediyap 0:8ad928968f9e 51 wait(2); // wait for sabertooth to initialized
bediyap 0:8ad928968f9e 52 _sabertoothserial.putc(START_CHARACTER);
bediyap 0:8ad928968f9e 53 }
bediyap 0:8ad928968f9e 54
bediyap 0:8ad928968f9e 55 // Valid speed is between -127 and +127
bediyap 0:8ad928968f9e 56 void Sabertooth::SetSpeedMotorA(int speed)
bediyap 0:8ad928968f9e 57 {
bediyap 0:8ad928968f9e 58 SetMotorSpeed(A, speed);
bediyap 0:8ad928968f9e 59 }
bediyap 0:8ad928968f9e 60
bediyap 0:8ad928968f9e 61 // Valid speed is between -127 and +127
bediyap 0:8ad928968f9e 62 void Sabertooth::SetSpeedMotorB(int speed)
bediyap 0:8ad928968f9e 63 {
bediyap 0:8ad928968f9e 64 SetMotorSpeed(B, speed);
bediyap 0:8ad928968f9e 65 }
bediyap 0:8ad928968f9e 66
bediyap 0:8ad928968f9e 67
bediyap 0:8ad928968f9e 68 void Sabertooth::SetMotorSpeed(int motor,int speed){
bediyap 0:8ad928968f9e 69
bediyap 0:8ad928968f9e 70 speed = ClipSpeed(speed);
bediyap 0:8ad928968f9e 71 _CurrentSpeed[motor] = speed;
bediyap 0:8ad928968f9e 72 int motorCommandStart = (motor == 0 ? 0 : 4);
bediyap 0:8ad928968f9e 73 if (speed >= 0)
bediyap 0:8ad928968f9e 74 {
bediyap 0:8ad928968f9e 75 // forward
bediyap 0:8ad928968f9e 76 Send(motorCommandStart + 0, speed);
bediyap 0:8ad928968f9e 77 }
bediyap 0:8ad928968f9e 78 else
bediyap 0:8ad928968f9e 79 {
bediyap 0:8ad928968f9e 80 // backwards
bediyap 0:8ad928968f9e 81 Send(motorCommandStart + 1, -speed);
bediyap 0:8ad928968f9e 82 }
bediyap 0:8ad928968f9e 83
bediyap 0:8ad928968f9e 84 }
bediyap 0:8ad928968f9e 85
bediyap 0:8ad928968f9e 86 // Valid speed is between -127 and +127
bediyap 0:8ad928968f9e 87 int Sabertooth::GetCommandedSpeedA()
bediyap 0:8ad928968f9e 88 {
bediyap 0:8ad928968f9e 89 return _CurrentSpeed[A];
bediyap 0:8ad928968f9e 90 }
bediyap 0:8ad928968f9e 91
bediyap 0:8ad928968f9e 92 // Valid speed is between -127 and +127
bediyap 0:8ad928968f9e 93 int Sabertooth::GetCommandedSpeedB()
bediyap 0:8ad928968f9e 94 {
bediyap 0:8ad928968f9e 95 return _CurrentSpeed[B];
bediyap 0:8ad928968f9e 96 }
bediyap 0:8ad928968f9e 97
bediyap 0:8ad928968f9e 98 void Sabertooth::ShutdownMotors(void){
bediyap 0:8ad928968f9e 99 _CurrentSpeed[A] = 0;
bediyap 0:8ad928968f9e 100 _CurrentSpeed[B] = 0;
bediyap 0:8ad928968f9e 101 SetSpeedMotorA(_CurrentSpeed[A]);
bediyap 0:8ad928968f9e 102 SetSpeedMotorB(_CurrentSpeed[B]);
bediyap 0:8ad928968f9e 103 }
bediyap 0:8ad928968f9e 104
bediyap 0:8ad928968f9e 105
bediyap 0:8ad928968f9e 106 int Sabertooth::ClipSpeed(int speed)
bediyap 0:8ad928968f9e 107 {
bediyap 0:8ad928968f9e 108 if (speed < -127)
bediyap 0:8ad928968f9e 109 {
bediyap 0:8ad928968f9e 110 return -127;
bediyap 0:8ad928968f9e 111 }
bediyap 0:8ad928968f9e 112 if (speed > 127)
bediyap 0:8ad928968f9e 113 {
bediyap 0:8ad928968f9e 114 return 127;
bediyap 0:8ad928968f9e 115 }
bediyap 0:8ad928968f9e 116 return speed;
bediyap 0:8ad928968f9e 117 }
bediyap 0:8ad928968f9e 118
bediyap 0:8ad928968f9e 119
bediyap 0:8ad928968f9e 120 void Sabertooth::Send(int command, int value)
bediyap 0:8ad928968f9e 121 {
bediyap 0:8ad928968f9e 122 int tmp;
bediyap 0:8ad928968f9e 123 _sabertoothserial.putc(_Address);
bediyap 0:8ad928968f9e 124 _sabertoothserial.putc(command);
bediyap 0:8ad928968f9e 125 _sabertoothserial.putc(value);
bediyap 0:8ad928968f9e 126 tmp = _Address+command+value;
bediyap 0:8ad928968f9e 127 tmp = tmp & 0x7F;
bediyap 0:8ad928968f9e 128 _sabertoothserial.putc(tmp);
bediyap 0:8ad928968f9e 129 }