Tripple Controller for the TLE5206 H Bridge motor controller

Committer:
AjK
Date:
Tue Jul 05 07:43:28 2011 +0000
Revision:
0:e2433ca2ce59
Child:
1:e6f43157c7db
0.2 Beta

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AjK 0:e2433ca2ce59 1
AjK 0:e2433ca2ce59 2 #include "mbed.h"
AjK 0:e2433ca2ce59 3 #include "SimpleTLE5206.h"
AjK 0:e2433ca2ce59 4
AjK 0:e2433ca2ce59 5 Serial pc(USBTX, USBRX);
AjK 0:e2433ca2ce59 6
AjK 0:e2433ca2ce59 7 /* NOTE! The SimpleTLE5206 library supports the TLE5206 in
AjK 0:e2433ca2ce59 8 * Mode 2 Sign/Magnitude Control using two PWM outputs to
AjK 0:e2433ca2ce59 9 * control speed and direction.
AjK 0:e2433ca2ce59 10 *
AjK 0:e2433ca2ce59 11 * Pins that be be used are p21, p22, p23, p24, p25 and/or p26
AjK 0:e2433ca2ce59 12 * in pairs. So the library supports upto 3 TLE5206 devices/motors.
AjK 0:e2433ca2ce59 13 *
AjK 0:e2433ca2ce59 14 * All PWM outputs use a common duty cycle which defaults to 100Hz/10ms.
AjK 0:e2433ca2ce59 15 * There is no way to have differing duty cycles for different output
AjK 0:e2433ca2ce59 16 * pin pairs as the hardware only supports a common duty cycle in single
AjK 0:e2433ca2ce59 17 * ended mode. It may be possible to refactor the library to use a different
AjK 0:e2433ca2ce59 18 * duty cycle system (paired outputs). But single/common duty cycle was the
AjK 0:e2433ca2ce59 19 * easiest and simplest way to get the library done in a short period of time.
AjK 0:e2433ca2ce59 20 *
AjK 0:e2433ca2ce59 21 * Additionally you can use LED1, LED2, LED3 and.or LED4 as mimics.
AjK 0:e2433ca2ce59 22 * However, if using:-
AjK 0:e2433ca2ce59 23 * LED1 you cannot use p26 to drive an in to TLE5206
AjK 0:e2433ca2ce59 24 * LED2 you cannot use p25 to drive an in to TLE5206
AjK 0:e2433ca2ce59 25 * LED3 you cannot use p24 to drive an in to TLE5206
AjK 0:e2433ca2ce59 26 * LED4 you cannot use p23 to drive an in to TLE5206
AjK 0:e2433ca2ce59 27 *
AjK 0:e2433ca2ce59 28 * The function SimpleTLE5206::setSpeed() takes a single arg, a double,
AjK 0:e2433ca2ce59 29 * and should be in the range +1.0 to -1.0 where +1.0 is full speed in
AjK 0:e2433ca2ce59 30 * a CW direction, -1.0 is full speed in a CCW direction and 0 is stopped.
AjK 0:e2433ca2ce59 31 */
AjK 0:e2433ca2ce59 32
AjK 0:e2433ca2ce59 33 // Create a motor "A" driven by a TLE5206 on pins 21 and 22.
AjK 0:e2433ca2ce59 34 SimpleTLE5206Output Ain1(p21); // TLE5206 In1 is connected to p21
AjK 0:e2433ca2ce59 35 SimpleTLE5206Output Ain2(p22); // TLE5206 In2 is connected to p22
AjK 0:e2433ca2ce59 36 SimpleTLE5206 motorA(&Ain1, &Ain2); // Create the TLE5206 controller using these pins.
AjK 0:e2433ca2ce59 37
AjK 0:e2433ca2ce59 38 // Create a motor "B" driven by a TLE5206 but on LEDs as a mimic.
AjK 0:e2433ca2ce59 39 SimpleTLE5206Output Bin1(LED3); // TLE5206 In1 is connected to LED3
AjK 0:e2433ca2ce59 40 SimpleTLE5206Output Bin2(LED4); // TLE5206 In2 is connected to LED4
AjK 0:e2433ca2ce59 41 SimpleTLE5206 motorB(&Bin1, &Bin2); // Create the TLE5206 controller using these pins.
AjK 0:e2433ca2ce59 42
AjK 0:e2433ca2ce59 43 DigitalOut myled(LED1);
AjK 0:e2433ca2ce59 44
AjK 0:e2433ca2ce59 45 Ticker myLedFlasher;
AjK 0:e2433ca2ce59 46
AjK 0:e2433ca2ce59 47 void myLedFlasherCallback(void) {
AjK 0:e2433ca2ce59 48 myled = !myled;
AjK 0:e2433ca2ce59 49 }
AjK 0:e2433ca2ce59 50
AjK 0:e2433ca2ce59 51 int main() {
AjK 0:e2433ca2ce59 52 double speed;
AjK 0:e2433ca2ce59 53
AjK 0:e2433ca2ce59 54 pc.baud(115200);
AjK 0:e2433ca2ce59 55
AjK 0:e2433ca2ce59 56 // Just flashes LED1 similar to a normal initial Mbed program.
AjK 0:e2433ca2ce59 57 myLedFlasher.attach(myLedFlasherCallback, 0.2);
AjK 0:e2433ca2ce59 58
AjK 0:e2433ca2ce59 59 // The main loop just demos a linear ramp up/ramp down repeatedly.
AjK 0:e2433ca2ce59 60 while(1) {
AjK 0:e2433ca2ce59 61 motorA.setSpeed(0);
AjK 0:e2433ca2ce59 62 motorB.setSpeed(0);
AjK 0:e2433ca2ce59 63
AjK 0:e2433ca2ce59 64 wait(2);
AjK 0:e2433ca2ce59 65
AjK 0:e2433ca2ce59 66 // Go from zero to +1.0 (+1.0 is full speed)
AjK 0:e2433ca2ce59 67 pc.printf("Going from zero to full CW speed...");
AjK 0:e2433ca2ce59 68 for(speed = 0.0; speed <= 1.0; speed += 0.005) {
AjK 0:e2433ca2ce59 69 if (speed > 1.0) speed = 1.0; // Never let the speed go over 1!
AjK 0:e2433ca2ce59 70 if (speed < -1.0) speed = -1.0; // Never let the speed go below -1!
AjK 0:e2433ca2ce59 71 motorA.setSpeed(speed);
AjK 0:e2433ca2ce59 72 motorB.setSpeed(speed);
AjK 0:e2433ca2ce59 73 wait(0.05);
AjK 0:e2433ca2ce59 74 }
AjK 0:e2433ca2ce59 75 pc.printf(" done.\n");
AjK 0:e2433ca2ce59 76
AjK 0:e2433ca2ce59 77 // Hold the motor at full speed for 5seconds.
AjK 0:e2433ca2ce59 78 wait(2);
AjK 0:e2433ca2ce59 79
AjK 0:e2433ca2ce59 80 // Now slow the motor down to zero and then reverse direction to -1.0
AjK 0:e2433ca2ce59 81 pc.printf("Going from full CW speed to full CCW speed...");
AjK 0:e2433ca2ce59 82 for( ; speed >= -1.0; speed -= 0.005) {
AjK 0:e2433ca2ce59 83 if (speed > 1.0) speed = 1.0; // Never let the speed go over 1!
AjK 0:e2433ca2ce59 84 if (speed < -1.0) speed = -1.0; // Never let the speed go below -1!
AjK 0:e2433ca2ce59 85 motorA.setSpeed(speed);
AjK 0:e2433ca2ce59 86 motorB.setSpeed(speed);
AjK 0:e2433ca2ce59 87 wait(0.05);
AjK 0:e2433ca2ce59 88 }
AjK 0:e2433ca2ce59 89 pc.printf(" done.\n");
AjK 0:e2433ca2ce59 90
AjK 0:e2433ca2ce59 91 // Hold the motor at full speed in ccw for 5seconds.
AjK 0:e2433ca2ce59 92 wait(2);
AjK 0:e2433ca2ce59 93
AjK 0:e2433ca2ce59 94 // Now bring the motor to a stop.
AjK 0:e2433ca2ce59 95 pc.printf("Bringing the motor to a halt...");
AjK 0:e2433ca2ce59 96 for( ; speed <= 0.0; speed += 0.005) {
AjK 0:e2433ca2ce59 97 if (speed > 1.0) speed = 1.0; // Never let the speed go over 1!
AjK 0:e2433ca2ce59 98 if (speed < -1.0) speed = -1.0; // Never let the speed go below -1!
AjK 0:e2433ca2ce59 99 motorA.setSpeed(speed);
AjK 0:e2433ca2ce59 100 motorB.setSpeed(speed);
AjK 0:e2433ca2ce59 101 wait(0.05);
AjK 0:e2433ca2ce59 102 }
AjK 0:e2433ca2ce59 103 pc.printf(" done.\n");
AjK 0:e2433ca2ce59 104 }
AjK 0:e2433ca2ce59 105 }
AjK 0:e2433ca2ce59 106
AjK 0:e2433ca2ce59 107