DC motor control program using TA7291P type driver and rotary encoder with A, B phase.

Dependencies:   QEI mbed-rtos mbed

Revision:
16:759d6f647c83
Parent:
15:744a81d5b7ac
Child:
17:10be89ff33e8
--- a/main.cpp	Sat Dec 08 05:01:53 2012 +0000
+++ b/main.cpp	Sat Dec 08 05:27:35 2012 +0000
@@ -1,5 +1,5 @@
 //  DC motor control program using H-bridge driver (ex. TA7291P) and 360 resolution rotary encoder with A, B phase.
-//      ver. 121208a by Kosaka lab.
+//      ver. 121208b by Kosaka lab.
 #include "mbed.h"
 #include "rtos.h"
 #include "QEI.h"
@@ -339,18 +339,6 @@
 #ifdef  GOOD_DATA
     int i;
     RtosTimer RtosTimerTS1(th_controller);  // RtosTimer priority is osPriorityAboveNormal, just one above main()
-//BUG(unstable!!)    Thread startTimerTS2(timerTS2,NULL,osPriorityNormal);
-    Thread ThreadTimerTS3(timerTS3,NULL,osPriorityBelowNormal);
-    Thread ThreadTimerTS4(timerTS4,NULL,osPriorityLow);
-// Priority of Thread (RtosTimer is osPriorityAboveNormal)
-//  osPriorityIdle          = -3,          ///< priority: idle (lowest)--> then, mbed ERROR!!
-//  osPriorityLow           = -2,          ///< priority: low
-//  osPriorityBelowNormal   = -1,          ///< priority: below normal
-//  osPriorityNormal        =  0,          ///< priority: normal (default)
-//  osPriorityAboveNormal   = +1,          ///< priority: above normal
-//  osPriorityHigh          = +2,          ///< priority: high 
-//  osPriorityRealtime      = +3,          ///< priority: realtime (highest)
-//  osPriorityError         =  0x84        ///< system cannot determine priority or thread has illegal priority
 
     count3=0;
     _count_data=0;
@@ -390,6 +378,18 @@
 }
 
 int main() {
+//BUG(unstable!!)    Thread startTimerTS2(timerTS2,NULL,osPriorityNormal);
+    Thread ThreadTimerTS3(timerTS3,NULL,osPriorityBelowNormal);
+    Thread ThreadTimerTS4(timerTS4,NULL,osPriorityLow);
+// Priority of Thread (RtosTimer is osPriorityAboveNormal)
+//  osPriorityIdle          = -3,          ///< priority: idle (lowest)--> then, mbed ERROR!!
+//  osPriorityLow           = -2,          ///< priority: low
+//  osPriorityBelowNormal   = -1,          ///< priority: below normal
+//  osPriorityNormal        =  0,          ///< priority: normal (default)
+//  osPriorityAboveNormal   = +1,          ///< priority: above normal
+//  osPriorityHigh          = +2,          ///< priority: high 
+//  osPriorityRealtime      = +3,          ///< priority: realtime (highest)
+//  osPriorityError         =  0x84        ///< system cannot determine priority or thread has illegal priority
 #if CONTROL_MODE==0     // PID control
     char    f;
     float   val;
@@ -420,12 +420,14 @@
         pc.printf("Control start!!\r\n");
         motor_control();    // motor control ON for TMAX seconds.
         pc.printf("Control completed!!\r\n\r\n");
+        ThreadTimerTS3.terminate();
+        ThreadTimerTS4.terminate();
 
         // Change parameters using tera term
 #if CONTROL_MODE>=1     // frequency response, or Step response
         pc.printf("Input u(t) Frequency[Hz]? (if 9, reset mbed)...");
         pc.scanf("%f",&_freq_u);
-        pc.printf("%8.3f[Hz]\r\n", _freq_u);  // print to tera term
+        pc.printf("%8.3f[Hz]\r\n", &_freq_u);  // print to tera term
         if(_freq_u==9){    mbed_reset();}
 #else                   // PID control
 //  #ifdef R_SIN