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