unit test for brushless motors using a potentiometer and a castle creations esc with 0.5 center duty cycle

Dependencies:   ESC Servo mbed

Fork of brushlessmotor by jetfishteam

Files at this revision

API Documentation at this revision

Comitter:
rkk
Date:
Fri Feb 26 14:35:02 2016 +0000
Parent:
2:040b8c8f4f92
Commit message:
brushless test program

Changed in this revision

MainController.cpp Show annotated file Show diff for this revision Revisions of this file
MainController.h Show annotated file Show diff for this revision Revisions of this file
PwmIn.cpp Show diff for this revision Revisions of this file
PwmIn.h Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 040b8c8f4f92 -r 605f216167f6 MainController.cpp
--- a/MainController.cpp	Thu Jul 30 20:36:00 2015 +0000
+++ b/MainController.cpp	Fri Feb 26 14:35:02 2016 +0000
@@ -1,137 +1,32 @@
 #include "MainController.h"
 
 MainController::MainController()
-    :ch1(p17,0.0636,0.1075), // yaw
-     ch2(p15,0.0623,0.1075), // pitch
-     ch3(p18,0.0614,0.1071), // amplitude
-     //ch4(p30,0.055,0.092), // adj
-     ch6(p16,0.0625,0.1055), // frequency
-     esc1(p25)
-     //ap(p25, p26)//,
-     //leftservo(p21),
-     //rightservo(p23)
-
+    :esc1(p25),
+    myled(LED1),
+    mypotentiometer(p20)
 {
     wait_ms(50);
     amp = 0.0;
-    frq = 1.0;
-    frqCmd = frq;
-    yaw = 0.5;
     frqMin = 0.8; //hardcoded
     frqMax = 1.8; //hardcoded
-    fullCycle = true;
-    raiser = 1.0;
-    alPi = 0.2/(0.2+0.001);//tf/(tf+Ts);
-
-    throttle_var = 0.5; //neutral position
-    esc1 = throttle_var;
+    dutyCycle = 0.5;
+    esc1 = dutyCycle;
     esc1();
     wait_ms(3000); //to arm brushless motor
-    //leftservo.calibrate(0.0008, 45);
-    //rightservo.calibrate(-0.0001, 45);
 
 }
 
 void MainController::control()
 {
+    amp = mypotentiometer.read();
+    myled = amp;
+        
     curTime = timer1.read();
-
-    // check every half cycle
-    if(curTime > (1/(2*frqCmd)-TOFF) ) {
-
-        if(curTime < 1/(2*frqCmd) ) {
-            ampNew = 0.0;
-        } else {
-            // read new yaw value every half cycle
-            yaw = this->calculateYaw(); // a value from -1 to 1
-
-            if(yaw < 0.1 && yaw > -0.1) { // eliminating noise around 0.0 +-0.1
-                yaw =0.0;
-            }
-
-            // Read volume and frequency only every full cycle
-            if( fullCycle ) {
-                //read other new inputs only at upward portion of full cycle
-                amp = this->calculateAmplitude(); // a value from 0 to 1
-                frq = this->calculateFrequency(); // a value from frqmin to frqmax
-
-                ampNew = amp;
-
-                if(yaw < 0.0) {
-                    ampNew = (1.0+0.7*yaw)*amp;
-                }
-
-                fullCycle = false;
-
-            } else {
-                // reverse for the downward slope
-                amp = -amp;
-
-                ampNew = amp;
-
-                if(yaw > 0.0) {
-                    ampNew = (1.0-0.7*yaw)*amp;
-                }
-
-                // use amp and frq from last cycle in order to make sure it is equalized
-                fullCycle = true;
-            }
-            // update the frequency that actually needs to be commanded
-            frqCmd = frq;
-
-            // read new yaw value every half cycle
-            //adj = this->calculateAdj(); // a value from 0 to 1
-
-            // for keeping track, calculate current volume storage, positive means on side is fuller, negative means other side is fuller
-            //volume = volChg + volume;
-            // rudder value used to define the trapezoid shape
-            raiser = 1.5; // varied from 1 to 5
-
-            //reset timer
-            timer1.reset();
-            curTime = 0.0;
-        }
-    }
-
-
-//    //Set Servo Values
-    pitch = this->calculatePitch();
-//    leftservo = pitch+0.03;
-//    rightservo = 1.0 - pitch;
-//    if (rightservo<0.03){
-//        rightservo = 0.03;
-//    }
-
-    dutyCycle = 0.5 + ampNew * saturate(raiser * sin( 2.0 * MATH_PI * frqCmd * curTime )); // add factor 4.0 to get a cut off sinus
+    
     //set brushless motor speed
-    throttle_var = dutyCycle;
-    esc1 = throttle_var;
-    esc1();
-}
-
-float MainController::calculateFrequency()
-{
-    return ((frqMax - frqMin) * ch6.dutycyclescaledup() + frqMin);
-    //return (ch6.dutycyclescaledup());
-}
-
-float MainController::calculateAmplitude()
-{
-    return (ch3.dutycyclescaledup() / 2.0);
-}
-
-float MainController::calculateYaw()
-{
-    return (2.0*ch1.dutycyclescaledup() - 1.0); //maps from 0.0-1.0 to -1.0 - 1.0
-    //return (ch1.dutycyclescaledup());
-}
-
-float MainController::calculatePitch()
-{
-    //float pitAvg = alPi * pitAvg+ (1.0 - alPi)*(ch2.dutycyclescaledup());
-    float pitAvg = (ch2.dutycyclescaledup());
-    return pitAvg;
-    
+    dutyCycle = 0.5 + (amp/2); 
+    esc1.setThrottle(dutyCycle);
+    esc1.pulse();
 }
 
 //float MainController::calculateAdj()
@@ -144,19 +39,14 @@
     timer1.start();
 
     ticker1.attach(this, &MainController::control,0.001);
-    //Autopilot guardian
-    //ap.calibrate();
-    //ap.set2D();
-    //ap.setoff();
-
 }
 
 void MainController::stop()
 {
     timer1.stop();
     ticker1.detach();
-    throttle_var = 0.5;
-    esc1 = throttle_var;
+    dutyCycle = 0.5;
+    esc1 = dutyCycle;
     esc1();
 }
 
@@ -170,22 +60,6 @@
     return amp;
 }
 
-
-float MainController::getFrequency()
-{
-    return frq;
-}
-
-float MainController::getYaw()
-{
-    return yaw;
-}
-
-float MainController::getPitch()
-{
-    return pitch;
-}
-
 //signum function
 float MainController::signum(float input) //gives back the sign
 {
diff -r 040b8c8f4f92 -r 605f216167f6 MainController.h
--- a/MainController.h	Thu Jul 30 20:36:00 2015 +0000
+++ b/MainController.h	Fri Feb 26 14:35:02 2016 +0000
@@ -2,7 +2,6 @@
 #define MBED_MAINCONTROLLER_H
  
 #include "mbed.h"
-#include "PwmIn.h"
 #include "esc.h"
 
 #define MATH_PI 3.14159265359
@@ -20,12 +19,7 @@
      */
     void start();
     float getDutyCycle();
-    float getFrequency();
     float getAmplitude();
-    float getYaw();
-    float getPitch();
-    float getTimeAdd();
-    float getAdj();
 
     /** Stop the main controller
      * @returns 
@@ -34,41 +28,23 @@
     
 protected:        
     void control();
-    float calculateFrequency();
-    float calculateAmplitude();
-    float calculateYaw();
-    float calculatePitch();
     //float calculateAdj();
     float signum(float input);
     float saturate(float input);
     
 private:
-    PwmIn ch1; //yaw
-    PwmIn ch2; //pitch
-    PwmIn ch3; //amp
-    PwmIn ch6; //freq
     ESC esc1;
-    //Servo leftservo;
-    //Servo rightservo;
     
     Timer timer1;
     Ticker ticker1;
     float amp;
-    float ampNew;
-    float frq;
     float dutyCycle;
     float curTime;
     float frqMin;
-    float frqMax;
-    float yaw;
-    float pitch;
-    float alPi;
-    //float adj;  
-    bool fullCycle;
-    float frqCmd;
-    float raiser;
+    float frqMax;    
+    PwmOut myled;
+    AnalogIn mypotentiometer;
     
-    float throttle_var;
 };
  
 #endif
\ No newline at end of file
diff -r 040b8c8f4f92 -r 605f216167f6 PwmIn.cpp
--- a/PwmIn.cpp	Thu Jul 30 20:36:00 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,55 +0,0 @@
-#include "PwmIn.h"
- 
-PwmIn::PwmIn(PinName p, float dutyMin, float dutyMax) 
-    : _p(p) {
-    _p.rise(this, &PwmIn::rise);
-    _p.fall(this, &PwmIn::fall);
-    _period = 20000;
-    _pulsewidth = 0;
-    _t.start();
-    _risen = false;
-    _dutyMin = dutyMin;
-    _dutyMax = dutyMax;
-    _dutyDelta = dutyMax - dutyMin;
-
-}
- 
-float PwmIn::period() {
-    return float(_period)/1000000;
-}
- 
-float PwmIn::pulsewidth() {
-    return float(_pulsewidth)/1000000;
-}
- 
-float PwmIn::dutycycle() {
-    return float(_pulsewidth) / float(_period);
-}
-
-float PwmIn::dutycyclescaledup() {
-    float duty = float(_pulsewidth) / float(_period);
-    float dutyAdjusted = (duty > _dutyMin) ? ((duty-_dutyMin)/_dutyDelta) : 0.0;
-    return (dutyAdjusted < 1.0) ? dutyAdjusted : 1.0;
-     
-}
- 
-void PwmIn::rise()
-{
-    _tmp = _t.read_us();
-    if(_risen == false) {
-        _period = _tmp;
-        _risen = true;
-        _t.reset();
-    }
-}
-
-void PwmIn::fall()
-{
-    _tmp = _t.read_us();
-    if(_risen == true) {
-        _pulsewidth = _tmp;
-        _risen = false;
-    }
-}
-
-    
\ No newline at end of file
diff -r 040b8c8f4f92 -r 605f216167f6 PwmIn.h
--- a/PwmIn.h	Thu Jul 30 20:36:00 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,55 +0,0 @@
-
-#ifndef MBED_PWMIN_H
-#define MBED_PWMIN_H
- 
-#include "mbed.h"
- 
-/** PwmIn class to read PWM inputs
- * 
- * Uses InterruptIn to measure the changes on the input
- * and record the time they occur
- *
- * @note uses InterruptIn, so not available on p19/p20
- */
-class PwmIn {
-public:
-    /** Create a PwmIn
-     *
-     * @param p The pwm input pin (must support InterruptIn)
-     */ 
-    PwmIn(PinName p, float dutyMin, float dutyMax) ;
-    
-    /** Read the current period
-     *
-     * @returns the period in seconds
-     */
-    float period();
-    
-    /** Read the current pulsewidth
-     *
-     * @returns the pulsewidth in seconds
-     */
-    float pulsewidth();
-    
-    /** Read the current dutycycle
-     *
-     * @returns the dutycycle as a percentage, represented between 0.0-1.0
-     */
-    float dutycycle();
-    
-    float dutycyclescaledup();
- 
-protected:        
-    void rise();
-    void fall();
-    
-    InterruptIn _p;
-    Timer _t;
-    int _pulsewidth, _period;
-    int _tmp;
-    float _dutyMin, _dutyMax, _dutyDelta;
-    
-    bool _risen;
-};
- 
-#endif
\ No newline at end of file
diff -r 040b8c8f4f92 -r 605f216167f6 main.cpp
--- a/main.cpp	Thu Jul 30 20:36:00 2015 +0000
+++ b/main.cpp	Fri Feb 26 14:35:02 2016 +0000
@@ -45,17 +45,17 @@
 {
     t.start();
     
-    MainController mainCtrl;
+    MainController mC;
        
-    mainCtrl.start();
+    mC.start();
 
     while(true) {
   
-        pc.printf("frq: %.4f, amp: %.4f, yaw: %.4f, pit: %.4f, dut: %.4f, t: %.4f\n",
-         mainCtrl.getFrequency(), mainCtrl.getAmplitude(), mainCtrl.getYaw(), mainCtrl.getPitch(), mainCtrl.getDutyCycle(), t.read());
+        pc.printf("amp: %.4f, dutyCycle %.4f, t: %.4f\n",
+         mC.getAmplitude(), mC.getDutyCycle(), t.read());
 
         wait_ms(900);
     }
     //t.stop();
-    //mainCtrl.stop();
+    //mC.stop();
 }
diff -r 040b8c8f4f92 -r 605f216167f6 mbed.bld
--- a/mbed.bld	Thu Jul 30 20:36:00 2015 +0000
+++ b/mbed.bld	Fri Feb 26 14:35:02 2016 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/7cff1c4259d7
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/bad568076d81
\ No newline at end of file