Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
- Committer:
- dkp14
- Date:
- 2017-03-14
- Revision:
- 13:deb1e793f125
- Parent:
- 11:043a63c952a0
- Child:
- 15:b6025338e0eb
File content as of revision 13:deb1e793f125:
#include "mbed.h"
#include "rtos.h"
#include "definitions.h"
#include "motorControl.h"
#define kp 0.75f
#define ki 0.5f
#define kd 1.0f
#define dt 0.02f //given in ms, used to call a ticker
volatile uint8_t state = 0;
//volatile uint8_t orState = 0; //Motor rotor offset.
volatile float w3 = 0; //Angular velocity
volatile float duty = 0.40;
volatile int count_i3 = 0;
const float angularVelocities[17] = {0, 112.355598, 164.975998, 218.721725,
260.672943, 291.491364, 308.479126, 316.805908, 321.183929, 324.010712,
326.146759, 336.187103, 351.175629, 364.887604, 377.856659, 387.58432,
392.540314};
const float dutyCycles [17] = {0, 0.28, 0.33, 0.38, 0.42, 0.47, 0.52, 0.57,
0.63, 0.68, 0.73, 0.78, 0.83, 0.88, 0.93, 0.98, 1};
const float angle = 6.283; //2*pi for 1 revolution
Timer dt_I3;
Timer motorTimer;
Ticker controlTicker;
volatile float fi0 = 0; //number of revs done
volatile int goalRevs = 50;
volatile float fi = 2*3.1415*goalRevs;
volatile float goalW = 0; //desired angular velocity
volatile float accError = 0;
volatile float prevError = 0;
float getDuty(float w){
for (int i=0;i<16;i++) { //iterate through the angular velocities
if (w > angularVelocities[i] && w <= angularVelocities[i+1]) {
if (w-angularVelocities[i] < angularVelocities[i+1]-w ) {
return dutyCycles[i];
}
else {
return dutyCycles[i+1];
}
}
}
return 0;
}
void control(){
fi0 = 6.283 * count_i3; //fi0 = 2*pi*revs
float error = fi - fi0;
accError += error*dt;
float dError = (prevError - error)/dt;
goalW = kp*error + ki*accError + kd*dError;
prevError = error;
duty = getDuty(goalW);
}
void i3rise(){
state = updateState();
motorOut((state-orState+lead+6)%6, duty);
w3 = angle/dt_I3.read(); //Calc angular velocity
dt_I3.reset();
count_i3++;
}
void i_edge(){
state = updateState();
motorOut((state-orState+lead+6)%6, duty);
}
void CHA_rise(){
}
void CHA_fall(){
}
void CHB_rise(){
}
void CHB_fall(){
}
int main() {
motorHome(); //Initialise motor before any interrupt
dt_I3.start(); //Start the time counters for velocity
controlTicker.attach(&control, dt);
I1.rise(&i_edge); //Assign interrupt handlers for LEDs
I1.fall(&i_edge);
I2.rise(&i_edge);
I2.fall(&i_edge);
I3.rise(&i3rise);
I3.fall(&i_edge);
// CHA.rise(&CHA_rise);
// CHA.fall(&CHA_fall);
// CHB.rise(&CHB_rise);
// CHB.fall(&CHB_fall);
state = updateState();
motorTimer.start();
motorOut((state-orState+lead+6)%6, 0.5f); //Kickstart the motor
wait(60);
while (count_i3<=goalRevs) {
pc.printf("Speed: %f, duty cycle: %f, revs done: %d \n\r",w3, duty, count_i3);
/*
if(duty < 0.00f) {
stopMotor();
return 0;
}
*/
/*
if(motorTimer.read() >= 30) {
stopMotor();
return 0;
}
*/
}
stopMotor();
return 0;
}
