Tripple Controller for the TLE5206 H Bridge motor controller

Committer:
AjK
Date:
Tue Jul 05 14:27:56 2011 +0000
Revision:
3:b7d951c6f551
Parent:
2:c5fbe0cb8a97
Child:
4:d69f22061c03
0.5 Beta See ChangeLog.h

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 2:c5fbe0cb8a97 14 * All PWM outputs use a common duty cycle. Therefore the third arg
AjK 2:c5fbe0cb8a97 15 * to the constructor must be the same for all TLE5206 devices. To
AjK 2:c5fbe0cb8a97 16 * ensure this, we use a #define DUTY_CYCLE_IN_HERTZ and supply it
AjK 2:c5fbe0cb8a97 17 * to all instances of controllers created.
AjK 0:e2433ca2ce59 18 *
AjK 0:e2433ca2ce59 19 * Additionally you can use LED1, LED2, LED3 and.or LED4 as mimics.
AjK 0:e2433ca2ce59 20 * However, if using:-
AjK 0:e2433ca2ce59 21 * LED1 you cannot use p26 to drive an in to TLE5206
AjK 0:e2433ca2ce59 22 * LED2 you cannot use p25 to drive an in to TLE5206
AjK 0:e2433ca2ce59 23 * LED3 you cannot use p24 to drive an in to TLE5206
AjK 0:e2433ca2ce59 24 * LED4 you cannot use p23 to drive an in to TLE5206
AjK 0:e2433ca2ce59 25 *
AjK 0:e2433ca2ce59 26 * The function SimpleTLE5206::setSpeed() takes a single arg, a double,
AjK 0:e2433ca2ce59 27 * and should be in the range +1.0 to -1.0 where +1.0 is full speed in
AjK 0:e2433ca2ce59 28 * a CW direction, -1.0 is full speed in a CCW direction and 0 is stopped.
AjK 0:e2433ca2ce59 29 */
AjK 0:e2433ca2ce59 30
AjK 2:c5fbe0cb8a97 31 #define DUTY_CYCLE_IN_HERTZ 50
AjK 2:c5fbe0cb8a97 32
AjK 3:b7d951c6f551 33 // Create a motor "A", driven by a TLE5206 on pins 21 and 22 (attach scope first, not a motor!)
AjK 2:c5fbe0cb8a97 34 SimpleTLE5206Output Ain1(p21); // TLE5206 In1 is connected to p21
AjK 2:c5fbe0cb8a97 35 SimpleTLE5206Output Ain2(p22); // TLE5206 In2 is connected to p22
AjK 2:c5fbe0cb8a97 36 SimpleTLE5206 motorA(&Ain1, &Ain2, DUTY_CYCLE_IN_HERTZ); // Create the TLE5206 controller.
AjK 0:e2433ca2ce59 37
AjK 2:c5fbe0cb8a97 38 // Create a motor "B", driven by a TLE5206 but on LEDs as a mimic.
AjK 2:c5fbe0cb8a97 39 SimpleTLE5206Output Bin1(LED1); // TLE5206 In1 is connected to LED1
AjK 2:c5fbe0cb8a97 40 SimpleTLE5206Output Bin2(LED2); // TLE5206 In2 is connected to LED2
AjK 2:c5fbe0cb8a97 41 SimpleTLE5206 motorB(&Bin1, &Bin2, DUTY_CYCLE_IN_HERTZ); // Create the TLE5206 controller.
AjK 0:e2433ca2ce59 42
AjK 2:c5fbe0cb8a97 43 // Create a motor "C", driven by a TLE5206 but on LEDs as a mimic.
AjK 2:c5fbe0cb8a97 44 SimpleTLE5206Output Cin1(LED3); // TLE5206 In1 is connected to LED3
AjK 2:c5fbe0cb8a97 45 SimpleTLE5206Output Cin2(LED4); // TLE5206 In2 is connected to LED4
AjK 2:c5fbe0cb8a97 46 SimpleTLE5206 motorC(&Cin1, &Cin2, DUTY_CYCLE_IN_HERTZ); // Create the TLE5206 controller.
AjK 0:e2433ca2ce59 47
AjK 2:c5fbe0cb8a97 48 Ticker A, B, C;
AjK 2:c5fbe0cb8a97 49
AjK 2:c5fbe0cb8a97 50 volatile double demand[3];
AjK 2:c5fbe0cb8a97 51 volatile double speed[3];
AjK 0:e2433ca2ce59 52
AjK 1:e6f43157c7db 53 #define PI 3.14159265
AjK 1:e6f43157c7db 54
AjK 2:c5fbe0cb8a97 55 void Acallback(void) {
AjK 2:c5fbe0cb8a97 56 speed[0] = sin(demand[0] * PI / 180.0);
AjK 2:c5fbe0cb8a97 57 if (++demand[0] >= 360.0) demand[0] = 0.0;
AjK 2:c5fbe0cb8a97 58 motorA.setSpeed(speed[0]);
AjK 2:c5fbe0cb8a97 59 }
AjK 2:c5fbe0cb8a97 60
AjK 2:c5fbe0cb8a97 61 void Bcallback(void) {
AjK 2:c5fbe0cb8a97 62 speed[1] = sin(demand[1] * PI / 180.0);
AjK 2:c5fbe0cb8a97 63 if (++demand[1] >= 360.0) demand[1] = 0.0;
AjK 2:c5fbe0cb8a97 64 motorB.setSpeed(speed[1]);
AjK 2:c5fbe0cb8a97 65 }
AjK 2:c5fbe0cb8a97 66
AjK 2:c5fbe0cb8a97 67 void Ccallback(void) {
AjK 2:c5fbe0cb8a97 68 speed[2] = sin(demand[2] * PI / 180.0);
AjK 2:c5fbe0cb8a97 69 if (++demand[2] >= 360.0) demand[2] = 0.0;
AjK 2:c5fbe0cb8a97 70 motorC.setSpeed(speed[2]);
AjK 2:c5fbe0cb8a97 71 }
AjK 2:c5fbe0cb8a97 72
AjK 0:e2433ca2ce59 73 int main() {
AjK 2:c5fbe0cb8a97 74
AjK 2:c5fbe0cb8a97 75 volatile int trash = 0;
AjK 0:e2433ca2ce59 76
AjK 0:e2433ca2ce59 77 pc.baud(115200);
AjK 0:e2433ca2ce59 78
AjK 1:e6f43157c7db 79 motorA.setSpeed(0);
AjK 1:e6f43157c7db 80 motorB.setSpeed(0);
AjK 2:c5fbe0cb8a97 81 motorC.setSpeed(0);
AjK 2:c5fbe0cb8a97 82
AjK 2:c5fbe0cb8a97 83 // Init the global variables.
AjK 2:c5fbe0cb8a97 84 for (int i = 0; i < 3; i++) {
AjK 2:c5fbe0cb8a97 85 demand[i] = speed[i] = 0.0;
AjK 2:c5fbe0cb8a97 86 }
AjK 2:c5fbe0cb8a97 87
AjK 2:c5fbe0cb8a97 88 // Note, you probably wouldn't want to move the speed of
AjK 2:c5fbe0cb8a97 89 // a motor at this rate, may break it. This example uses
AjK 2:c5fbe0cb8a97 90 // a high update rate (0.025) assuming you are attaching
AjK 2:c5fbe0cb8a97 91 // an oscilloscope just for testing. It goes without saying
AjK 2:c5fbe0cb8a97 92 // that the update rates for B and C are way to big, I just
AjK 2:c5fbe0cb8a97 93 // choose these (0.005 and 0.0025) because it looks nice
AjK 2:c5fbe0cb8a97 94 // when used on LEDs!
AjK 2:c5fbe0cb8a97 95 // Always use appropriate accel/decel rates when handling
AjK 2:c5fbe0cb8a97 96 // motors/external hardware that moves.
AjK 2:c5fbe0cb8a97 97
AjK 2:c5fbe0cb8a97 98 A.attach(Acallback, 0.025);
AjK 2:c5fbe0cb8a97 99 B.attach(Bcallback, 0.005);
AjK 2:c5fbe0cb8a97 100 C.attach(Ccallback, 0.0025);
AjK 2:c5fbe0cb8a97 101
AjK 0:e2433ca2ce59 102 while(1) {
AjK 3:b7d951c6f551 103 /* The main loop has little to do as the Ticker callbacks
AjK 2:c5fbe0cb8a97 104 set-up the speed changes for the example. So give it something
AjK 2:c5fbe0cb8a97 105 to do. Maybe change this and use the spare time to calculate PI
AjK 2:c5fbe0cb8a97 106 more accurately? Lol, just kidding. */
AjK 2:c5fbe0cb8a97 107 trash++;
AjK 0:e2433ca2ce59 108 }
AjK 0:e2433ca2ce59 109 }
AjK 0:e2433ca2ce59 110
AjK 0:e2433ca2ce59 111