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.
Diff: main.cpp
- Revision:
- 12:cbea987a3ec4
- Parent:
- 11:74eeb8871fe6
- Child:
- 13:da9d3fbbe407
diff -r 74eeb8871fe6 -r cbea987a3ec4 main.cpp
--- a/main.cpp Mon Dec 17 08:37:21 2018 +0000
+++ b/main.cpp Thu Jan 03 15:35:32 2019 +0000
@@ -23,11 +23,11 @@
Timer t; //timer used in RPM
PwmOut Phase1 (p21); //Pin and LED set up - originally standard pins but changed to PWM to enable speed control
-PwmOut Phase2 (p23);
-PwmOut Phase3 (p24);
-PwmOut Phase4 (p25);
+PwmOut Phase2 (p22);
+PwmOut Phase3 (p23);
+PwmOut Phase4 (p24);
-AnalogOut Aout(p18); //Used with multimeter to give a speed indicator 1mV = 1RPM
+//AnalogOut Aout(p18); //Used with multimeter to give a speed indicator 1mV = 1RPM
//DigitalIn Button1 (p11); //not used
//DigitalIn Button2 (p12); //not used
@@ -36,7 +36,8 @@
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
-
+DigitalOut UnUsedPhase1 (p25);
+DigitalOut UnUsedPhase2 (p26);
DigitalOut SerialClock (p12); //ReadKType
DigitalIn DOut (p13); //ReadKType
DigitalOut cs1 (p14); //ReadKType
@@ -44,19 +45,19 @@
int StateA = 0; //State for first 2 revolutions (calibration of the index)
int StateB = 0; //All state machines after calibration use this state
//int StateC = 0;
-int AdjCW = 0; //CW offset to adjust phase firing to give the fastest speed = used to calculate stateB
-int AdjACW = 3; //ACW offset to adjust phase firing to give the fastest speed = used to calculate stateB
+int AdjCW = 2; //CW offset to adjust phase firing to give the fastest speed = used to calculate stateB
+int AdjACW = 5; //ACW offset to adjust phase firing to give the fastest speed = used to calculate stateB
int TimePerClick = 0; //for calc of RPM
int TimePerRev = 0; //for calc of RPM
-int RPS = 0; //for calc of RPMl;
+int RPS = 0; //for calc of RPM
int rpm = 0; //for calc of RPM
int SetPoint = 1000; //for adjusting the speed
-int z = 80; //TimePerRev = TimePerClick * (800/z); 800 pulses per rev, PulseCount2_==80 for wheel.getwhoop_ flag. i.e. 10 points per reoluition for RPM calc.
+int z = 800; //TimePerRev = TimePerClick * (800/z); 800 pulses per rev, PulseCount2_==800 for wheel.getwhoop_ flag. i.e. 10 points per reoluition for RPM calc.
int T = 80; //Motor temp limit
char c; //keyboard cotrol GetChar
-float duty = 1; //velocity loop: 1 = fastest, 0.96 = slowest. Below 0.96 the motor will not operate.
+float duty = 0.5; //velocity loop: 1 = fastest, 0.96 = slowest. Below 0.96 the motor will not operate.
float diff = 0.0; //Velocity loop: diff = SetPoint - rpm;
float x=0.1; //x=time of square wave when 1 phase energised,
float y=0.04; //y=time of square wave when 2 phases energised
@@ -73,8 +74,8 @@
Initialisation();
wait(0.1);
t.start();
- SerialClock = 0;
-
+ SerialClock = 0;
+
while(wheel.getRevolutions()<2) //Index Calibration
{
switch(StateA)
@@ -113,7 +114,7 @@
{
while((led1 == 0) && (led2 == 0)) //If no command to operate
{
- Aout = 0;
+ //Aout = 0;
rpm = 0;
Phase1.write(0);
Phase2.write(0);
@@ -155,7 +156,7 @@
default:break;
}
- if(wheel.getWhoop()==1) //PulseCount2_==80, whoop_=1;
+ if(wheel.getWhoop()==1) //PulseCount2_==800, whoop_=1;
{
RPM();
VelocityLoop();
@@ -190,7 +191,7 @@
default:break;
}
- if(wheel.getWhoop()==1) //PulseCount2_==80, whoop_=1;
+ if(wheel.getWhoop()==1) //PulseCount2_==800, whoop_=1;
{
RPM();
VelocityLoop();
@@ -224,7 +225,7 @@
default:break;
}
- if(wheel.getWhoop()==1) //PulseCount2_==80, whoop_=1;
+ if(wheel.getWhoop()==1) //PulseCount2_==800, whoop_=1;
{
RPM();
VelocityLoop();
@@ -256,7 +257,7 @@
case 0:Ph1();break;
default:break;
}
- if(wheel.getWhoop()==1) //PulseCount2_==80, whoop_=1;
+ if(wheel.getWhoop()==1) //PulseCount2_==800, whoop_=1;
{
RPM();
VelocityLoop();
@@ -385,6 +386,8 @@
led2 = 0;
led3 = 0;
led4 = 0;
+ UnUsedPhase1=0;
+ UnUsedPhase2=0;
wheel.ResetYay();
}
@@ -466,21 +469,21 @@
}
if(c=='t')
{
- pc.printf("%f C\n\r",temp);
+ pc.printf("%.0f C\n\r",temp);
}
}
}
void RPM (void)
{
- wheel.ResetWhoop(); //PulseCount2_==80, whoop_=1;
+ wheel.ResetWhoop(); //PulseCount2_==800, whoop_=1;
TimePerClick = (t.read_us()); //read timer in microseconds
t.reset(); //reset timer
- TimePerRev = TimePerClick * (800/z); //z = 80 (PulseCount2_==80)
+ TimePerRev = TimePerClick * (800/z); //z = 800 (PulseCount2_==800)
TimePerRev = TimePerRev / 1000; //
RPS = 10000000 / TimePerRev;
rpm = (RPS * 60)/10000;
- Aout=((0.30303*rpm)/1000); // for 500 rpm (0.30303*500/1000)*3.3V = 0.500V
+ //Aout=((0.30303*rpm)/1000); // for 500 rpm (0.30303*500/1000)*3.3V = 0.500V
ReadKType();
//if(rpm < 800)
//{
@@ -505,7 +508,7 @@
void VelocityLoop (void)
{
diff = SetPoint - rpm; //difference between setpoint and the RPM measurement
- duty = duty + (diff*0.00001); //duty is adjusted to speed up or slow down until difference = 0
+ duty = duty + (diff*0.0001); //duty is adjusted to speed up or slow down until difference = 0
if (duty > 1) //limits for duty. Motor will not operate below 0.96. 1 = max
{
duty = 1;
@@ -526,25 +529,25 @@
//SerialClock = 0; //set clock to 0
//wait_ms(1);
SerialClock = 1; //clock once to set to the 13 bit temp data
- wait_ms(0.000001);
+ wait_ms(0.1);
SerialClock = 0;
- wait_ms(0.000001);
+ wait_ms(0.1);
for(i = 13; i > -1; i = i-1) // now data is temp data where MSB is first and each count is worth 0.25 degrees
{
if(DOut == 1) //check data, store results in readout
{
- Readout |= (1<<i);
+ Readout |= (1<<i);
}
else
{
Readout |= (0<<i);
}
-
+
SerialClock = 1; //clock to the next bit
- wait_ms(0.000001);
+ wait_ms(0.1);
SerialClock = 0;
- wait_ms(0.000001);
+ wait_ms(0.1);
}
temp = Readout * 0.125; //get the real temp value which is a float
//pc.printf("%f\n\r",temp);