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:
- 11:74eeb8871fe6
- Parent:
- 10:808cb9052f14
- Child:
- 12:cbea987a3ec4
diff -r 808cb9052f14 -r 74eeb8871fe6 main.cpp
--- a/main.cpp Fri Dec 14 11:00:47 2018 +0000
+++ b/main.cpp Mon Dec 17 08:37:21 2018 +0000
@@ -14,6 +14,7 @@
void GetChar (void);
void RPM (void);
void VelocityLoop (void);
+void ReadKType(void);
Serial pc(USBTX, USBRX); // tx, rx - set up the Terraterm input from mbed
@@ -21,10 +22,10 @@
Timer t; //timer used in RPM
-PwmOut Phase1 (p23); //Pin and LED set up - originally standard pins but changed to PWM to enable speed control
-PwmOut Phase2 (p24);
-PwmOut Phase3 (p25);
-PwmOut Phase4 (p26);
+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);
AnalogOut Aout(p18); //Used with multimeter to give a speed indicator 1mV = 1RPM
@@ -51,12 +52,11 @@
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 i = 0; //ReadKType
-int Readout = 0; //ReadKType
-
+int T = 80; //Motor temp limit
+
char c; //keyboard cotrol GetChar
-float duty = 0.5; //velocity loop: 1 = fastest, 0.96 = slowest. Below 0.96 the motor will not operate.
+float duty = 1; //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,6 +73,7 @@
Initialisation();
wait(0.1);
t.start();
+ SerialClock = 0;
while(wheel.getRevolutions()<2) //Index Calibration
{
@@ -119,12 +120,13 @@
Phase3.write(0);
Phase4.write(0);
GetChar();
+ ReadKType();
//StateB = wheel.getPulses()%16;
//StateC = (800+wheel.getPulses()+StateA+AdjCW)%16;
//pc.printf("StateA= %i, StateB= %i, StateC= %i, Pulses = %i\n\r", StateA, StateB, StateC, wheel.getPulses());
//pc.printf("0 StateB= %i, Pulses= %i, Revs= %i\r", StateB,wheel.getPulses(),wheel.getRevolutions());
}
- while((wheel.getRevolutions()>1) && (wheel.getPulses()>0) && (led1==1)) //After Calibration, Prev CW movement, CW command
+ while((wheel.getRevolutions()>1) && (wheel.getPulses()>0) && (led1==1) && (temp<T)) //After Calibration, Prev CW movement, CW command
{
GetChar();
StateB = (wheel.getPulses()+StateA+AdjCW)%16;
@@ -157,10 +159,10 @@
{
RPM();
VelocityLoop();
- }
+ }
}
- while(wheel.getRevolutions()>1 && wheel.getPulses()<1 && (led1==1)) //After Calibration, Prev ACW movement, CW command
+ while(wheel.getRevolutions()>1 && wheel.getPulses()<1 && (led1==1) && (temp<T)) //After Calibration, Prev ACW movement, CW command
{
GetChar();
StateB = (800+wheel.getPulses()+StateA+AdjCW)%16;
@@ -192,9 +194,9 @@
{
RPM();
VelocityLoop();
- }
+ }
}
- while((wheel.getRevolutions()>1) && (wheel.getPulses()>0) && (led2==1)) //After Calibration, Prev CW movement, ACW command
+ while((wheel.getRevolutions()>1) && (wheel.getPulses()>0) && (led2==1) && (temp<T)) //After Calibration, Prev CW movement, ACW command
{
GetChar();
//StateB = (800+wheel.getPulses())%16;
@@ -228,7 +230,7 @@
VelocityLoop();
}
}
- while((wheel.getRevolutions()>1) && (wheel.getPulses()<1) && (led2==1)) //After Calibration, Prev ACW movement, ACW command
+ while((wheel.getRevolutions()>1) && (wheel.getPulses()<1) && (led2==1) &&(temp<T)) //After Calibration, Prev ACW movement, ACW command
{
GetChar();
StateB = (800+wheel.getPulses()+StateA+AdjACW)%16;
@@ -258,8 +260,24 @@
{
RPM();
VelocityLoop();
- }
+ }
}
+ while(temp>(T-1))
+ {
+ Initialisation();
+ pc.printf("Motor Over Temp\n\r");
+ while(1)
+ {
+ ReadKType();
+ pc.printf("%f\r",temp);
+ wait(1);
+ if(temp<T-20)
+ {
+ pc.printf("Motor Back Online\n\r");
+ break;
+ }
+ }
+ }
}
}
void StepCW(void) //Square wave switching
@@ -446,6 +464,10 @@
{
pc.printf("%i, %.5f, %i, %i, %i \n\r", SetPoint, duty, AdjCW, AdjACW, rpm);
}
+ if(c=='t')
+ {
+ pc.printf("%f C\n\r",temp);
+ }
}
}
@@ -459,6 +481,7 @@
RPS = 10000000 / TimePerRev;
rpm = (RPS * 60)/10000;
Aout=((0.30303*rpm)/1000); // for 500 rpm (0.30303*500/1000)*3.3V = 0.500V
+ ReadKType();
//if(rpm < 800)
//{
// AdjCW = 0;
@@ -492,4 +515,39 @@
duty = 0.01;
}
//pc.printf("%i, %.5f, %i, %i, %i \r", SetPoint, duty, AdjCW, AdjACW, rpm); //SetPoint = %i, rpm = %i\n\r", duty, SetPoint, rpm);
+}
+
+void ReadKType(void)
+{
+ int i = 0;
+ int Readout = 0;
+
+ cs1 = 0;
+ //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);
+ SerialClock = 0;
+ wait_ms(0.000001);
+
+ 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);
+ }
+ else
+ {
+ Readout |= (0<<i);
+ }
+
+ SerialClock = 1; //clock to the next bit
+ wait_ms(0.000001);
+ SerialClock = 0;
+ wait_ms(0.000001);
+ }
+ temp = Readout * 0.125; //get the real temp value which is a float
+ //pc.printf("%f\n\r",temp);
+ Readout = 0;
+ cs1 = 1;
}
\ No newline at end of file