David Pasztor / Mbed 2 deprecated Motor_control

Dependencies:   mbed-rtos mbed

main.cpp

Committer:
davidanderle
Date:
2017-03-13
Revision:
2:fe637a5f3387
Parent:
0:74a5723d604a
Child:
4:dc705df93090

File content as of revision 2:fe637a5f3387:

#include "mbed.h"
#include "rtos.h"
#include "definitions.h"
#include "motorControl.h"

volatile uint8_t state = 0;
volatile uint8_t orState = 0;                   //Motor rotor offset.
volatile float w [3] = {0, 0, 0};               //Angular velocities
volatile float avgW = 0;
volatile int duty = 0;

const uint16_t angle = 6283;                    //2*pi*1000 for 1 revolution
Timer dt_I1;
Timer dt_I2;
Timer dt_I3;
Timer motorTimer;

void i1rise(){
    state = updateState();
    motorOut((state-orState+lead+6)%6, duty);
    
    dt_I1.stop();
    w[0] = angle/dt_I1.read_ms();                //Calc angular velocity
    
    dt_I1.reset();
    dt_I1.start();                       
}

void i2rise(){
    state = updateState();
    motorOut((state-orState+lead+6)%6, duty);
    
    dt_I2.stop();
    w[1] = angle/dt_I2.read_ms();
    
    dt_I2.reset();
    dt_I2.start();                       
}

void i3rise(){
    state = updateState();
    motorOut((state-orState+lead+6)%6, duty);
    
    dt_I3.stop();
    w[2] = angle/dt_I3.read_ms();
    
    dt_I3.reset();
    dt_I3.start();                       
}

void i_fall(){
   state = updateState();
   motorOut((state-orState+lead+6)%6, duty);
}  

void CHA_rise(){
}
void CHA_fall(){
}
void CHB_rise(){
}
void CHB_fall(){
}

int main() {
    //Probably measure orState from hardware and make it a const?
    orState = motorHome();  //Initialise motor before any interrupt
    
    dt_I1.start();          //Start the time counters for velocity
    dt_I2.start();          //Probably put these in an init function?
    dt_I3.start();
    
    motorOut(4, 100);            //Kickstart the motor
    motorTimer.start();
    
    I1.rise(&i1rise);       //Assign interrupt handlers for LEDs
    I1.fall(&i_fall);
    I2.rise(&i2rise);
    I2.fall(&i_fall);
    I3.rise(&i3rise);
    I3.fall(&i_fall);
//    CHA.rise(&CHA_rise);
//    CHA.fall(&CHA_fall);
//    CHB.rise(&CHB_rise);
//    CHB.fall(&CHB_fall);

    while (1) {
        duty += 5;
        wait(2);
        if(duty == 100) {
            stopMotor();
            return 0;    
        }
        avgW = (w[0] + w[1] + w[2])/3;    //average speeds for better prediction
        //if(motorTimer.read_ms() >= 10000) {
        //    stopMotor();
        //    return 0;    
        //}
    }
}