Tripple Controller for the TLE5206 H Bridge motor controller
Diff: inc/example1.h
- Revision:
- 2:c5fbe0cb8a97
- Parent:
- 1:e6f43157c7db
- Child:
- 3:b7d951c6f551
--- a/inc/example1.h Tue Jul 05 09:27:36 2011 +0000 +++ b/inc/example1.h Tue Jul 05 13:50:45 2011 +0000 @@ -11,12 +11,10 @@ * Pins that be be used are p21, p22, p23, p24, p25 and/or p26 * in pairs. So the library supports upto 3 TLE5206 devices/motors. * - * All PWM outputs use a common duty cycle which defaults to 100Hz/10ms. - * There is no way to have differing duty cycles for different output - * pin pairs as the hardware only supports a common duty cycle in single - * ended mode. It may be possible to refactor the library to use a different - * duty cycle system (paired outputs). But single/common duty cycle was the - * easiest and simplest way to get the library done in a short period of time. + * All PWM outputs use a common duty cycle. Therefore the third arg + * to the constructor must be the same for all TLE5206 devices. To + * ensure this, we use a #define DUTY_CYCLE_IN_HERTZ and supply it + * to all instances of controllers created. * * Additionally you can use LED1, LED2, LED3 and.or LED4 as mimics. * However, if using:- @@ -30,44 +28,83 @@ * a CW direction, -1.0 is full speed in a CCW direction and 0 is stopped. */ -// Create a motor "A" driven by a TLE5206 on pins 21 and 22. -SimpleTLE5206Output Ain1(p21); // TLE5206 In1 is connected to p21 -SimpleTLE5206Output Ain2(p22); // TLE5206 In2 is connected to p22 -SimpleTLE5206 motorA(&Ain1, &Ain2); // Create the TLE5206 controller using these pins. +#define DUTY_CYCLE_IN_HERTZ 50 + +// Create a motor "A", driven by a TLE5206 on pins 21 and 22. +SimpleTLE5206Output Ain1(p21); // TLE5206 In1 is connected to p21 +SimpleTLE5206Output Ain2(p22); // TLE5206 In2 is connected to p22 +SimpleTLE5206 motorA(&Ain1, &Ain2, DUTY_CYCLE_IN_HERTZ); // Create the TLE5206 controller. -// Create a motor "B" driven by a TLE5206 but on LEDs as a mimic. -SimpleTLE5206Output Bin1(LED3); // TLE5206 In1 is connected to LED3 -SimpleTLE5206Output Bin2(LED4); // TLE5206 In2 is connected to LED4 -SimpleTLE5206 motorB(&Bin1, &Bin2); // Create the TLE5206 controller using these pins. +// Create a motor "B", driven by a TLE5206 but on LEDs as a mimic. +SimpleTLE5206Output Bin1(LED1); // TLE5206 In1 is connected to LED1 +SimpleTLE5206Output Bin2(LED2); // TLE5206 In2 is connected to LED2 +SimpleTLE5206 motorB(&Bin1, &Bin2, DUTY_CYCLE_IN_HERTZ); // Create the TLE5206 controller. -DigitalOut myled(LED1); - -Ticker myLedFlasher; +// Create a motor "C", driven by a TLE5206 but on LEDs as a mimic. +SimpleTLE5206Output Cin1(LED3); // TLE5206 In1 is connected to LED3 +SimpleTLE5206Output Cin2(LED4); // TLE5206 In2 is connected to LED4 +SimpleTLE5206 motorC(&Cin1, &Cin2, DUTY_CYCLE_IN_HERTZ); // Create the TLE5206 controller. -void myLedFlasherCallback(void) { - myled = !myled; -} +Ticker A, B, C; + +volatile double demand[3]; +volatile double speed[3]; #define PI 3.14159265 +void Acallback(void) { + speed[0] = sin(demand[0] * PI / 180.0); + if (++demand[0] >= 360.0) demand[0] = 0.0; + motorA.setSpeed(speed[0]); +} + +void Bcallback(void) { + speed[1] = sin(demand[1] * PI / 180.0); + if (++demand[1] >= 360.0) demand[1] = 0.0; + motorB.setSpeed(speed[1]); +} + +void Ccallback(void) { + speed[2] = sin(demand[2] * PI / 180.0); + if (++demand[2] >= 360.0) demand[2] = 0.0; + motorC.setSpeed(speed[2]); +} + int main() { - double speed; + + volatile int trash = 0; pc.baud(115200); - // Just flashes LED1 similar to a normal initial Mbed program. - myLedFlasher.attach(myLedFlasherCallback, 0.2); - motorA.setSpeed(0); motorB.setSpeed(0); - + motorC.setSpeed(0); + + // Init the global variables. + for (int i = 0; i < 3; i++) { + demand[i] = speed[i] = 0.0; + } + + // Note, you probably wouldn't want to move the speed of + // a motor at this rate, may break it. This example uses + // a high update rate (0.025) assuming you are attaching + // an oscilloscope just for testing. It goes without saying + // that the update rates for B and C are way to big, I just + // choose these (0.005 and 0.0025) because it looks nice + // when used on LEDs! + // Always use appropriate accel/decel rates when handling + // motors/external hardware that moves. + + A.attach(Acallback, 0.025); + B.attach(Bcallback, 0.005); + C.attach(Ccallback, 0.0025); + while(1) { - for (double i = 0; i < 360; i++) { - speed = sin(i * PI / 180.0); - motorA.setSpeed(speed); - motorB.setSpeed(speed); - wait(0.05); - } + /* The main loop has little to do as the Ticker cakkbacks + set-up the speed changes for the example. So give it something + to do. Maybe change this and use the spare time to calculate PI + more accurately? Lol, just kidding. */ + trash++; } }