Tripple Controller for the TLE5206 H Bridge motor controller

Files at this revision

API Documentation at this revision

Comitter:
AjK
Date:
Tue Jul 05 13:50:45 2011 +0000
Parent:
1:e6f43157c7db
Child:
3:b7d951c6f551
Commit message:
0.4 Beta See ChangeLog.h for details

Changed in this revision

inc/ChangeLog.h Show annotated file Show diff for this revision Revisions of this file
inc/SimpleTLE5206.h Show annotated file Show diff for this revision Revisions of this file
inc/example1.h Show annotated file Show diff for this revision Revisions of this file
src/SimpleTLE5206.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/inc/ChangeLog.h	Tue Jul 05 13:50:45 2011 +0000
@@ -0,0 +1,9 @@
+/*
+
+0.4 Beta 5/Jul/2011
+    * Added this file, ChangeLog.h
+    * Added the ability to specify the duty cycle in Hz.
+    * Made example1.h a bit cleaner and more fun to experiment with.
+    * Added some more documentation.
+    * ToDo: proper doxygen comments on main class structures.
+*/
--- a/inc/SimpleTLE5206.h	Tue Jul 05 09:27:36 2011 +0000
+++ b/inc/SimpleTLE5206.h	Tue Jul 05 13:50:45 2011 +0000
@@ -43,14 +43,16 @@
     
     void setMRx(PwmCh ch, uint32_t value);
     
-    void init(void);
+    void init(uint32_t);
     
 public:
 
     SimpleTLE5206() { error("We require two pins!\n"); }
         
+    SimpleTLE5206(SimpleTLE5206Output *in1, SimpleTLE5206Output *in2, int duty_cycle_hz);
+
     SimpleTLE5206(SimpleTLE5206Output *in1, SimpleTLE5206Output *in2);
-
+    
     void setSpeed(double d);
     
 }; /* class SimpleTLE5206 ends. */
--- 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++;
     }
 }
 
--- a/src/SimpleTLE5206.cpp	Tue Jul 05 09:27:36 2011 +0000
+++ b/src/SimpleTLE5206.cpp	Tue Jul 05 13:50:45 2011 +0000
@@ -31,19 +31,36 @@
     // Start initially as GPIO and both on (no drive, no speed).
     _in1 = in1;
     _in2 = in2;
-    init();
+    init(0);
+}
+
+SimpleTLE5206::SimpleTLE5206(SimpleTLE5206Output *in1, SimpleTLE5206Output *in2, int duty_cycle_hz)
+{
+    // Start initially as GPIO and both on (no drive, no speed).
+    _in1 = in1;
+    _in2 = in2;
+    init((uint32_t)(12000000UL / duty_cycle_hz * 2));
 }
 
 void
-SimpleTLE5206::init()
+SimpleTLE5206::init(uint32_t duty)
 {
-    setDuty(240000UL);
+    if (duty != 0) setDuty(duty);
     setSpeed(0.0);
 }
 
 void
 SimpleTLE5206::setDuty(uint32_t duty)
 {
+
+    _duty = duty;
+    
+    // Skip the setup if teh duty has already been set
+    // by a previous instance of controller.
+    if (LPC_PWM1->MR0 == duty) {
+        return;
+    }
+    
     // Ensure powered up (default is 1)  
     LPC_SC->PCONP |= (1UL << 6);  
         
@@ -53,8 +70,6 @@
     // Reset.
     LPC_PWM1->TCR   = 2; 
     
-    _duty = duty;
-    
     LPC_PWM1->PR    = 0;
     LPC_PWM1->MR0   = _duty;
     LPC_PWM1->MR1   = 0;