Motor steering program

Dependencies:   mbed

Committer:
johnyp1845
Date:
Fri Jun 02 20:41:24 2017 +0000
Revision:
1:797c7fc3a9b8
Parent:
0:26a4609c804e
Motor Steering

Who changed what in which revision?

UserRevisionLine numberNew contents of line
johnyp1845 0:26a4609c804e 1 #include "mbed.h"
johnyp1845 0:26a4609c804e 2
johnyp1845 0:26a4609c804e 3 int period = 18;//how long the cycles are, Can be changed to increase accuracy
johnyp1845 0:26a4609c804e 4 int cycles = 5;// Number of cycles sampled, More cycles = more accuracy but more latency
johnyp1845 1:797c7fc3a9b8 5 float cutOff = .268; //rms value for starting breaks
johnyp1845 0:26a4609c804e 6 float hysteresis = .05;// undershoot value
johnyp1845 0:26a4609c804e 7
johnyp1845 0:26a4609c804e 8
johnyp1845 0:26a4609c804e 9
johnyp1845 0:26a4609c804e 10
johnyp1845 0:26a4609c804e 11
johnyp1845 0:26a4609c804e 12
johnyp1845 0:26a4609c804e 13
johnyp1845 0:26a4609c804e 14
johnyp1845 0:26a4609c804e 15
johnyp1845 0:26a4609c804e 16
johnyp1845 0:26a4609c804e 17 DigitalOut clamp(D7);
johnyp1845 1:797c7fc3a9b8 18 AnalogIn voltage(A1);
johnyp1845 0:26a4609c804e 19 int count, countCutOff;
johnyp1845 0:26a4609c804e 20 float value;
johnyp1845 0:26a4609c804e 21 double squareValue, RMSvalue, sum, RMS, x;
johnyp1845 0:26a4609c804e 22
johnyp1845 0:26a4609c804e 23 double getRMS();
johnyp1845 0:26a4609c804e 24 int main()
johnyp1845 0:26a4609c804e 25 {
johnyp1845 0:26a4609c804e 26 while(1)
johnyp1845 0:26a4609c804e 27 {
johnyp1845 1:797c7fc3a9b8 28 RMS = getRMS(); // gets values
johnyp1845 1:797c7fc3a9b8 29 if(RMS>=cutOff) // checks to see above threshold
johnyp1845 0:26a4609c804e 30 {
johnyp1845 0:26a4609c804e 31 clamp = 1;
johnyp1845 0:26a4609c804e 32
johnyp1845 0:26a4609c804e 33 }
johnyp1845 0:26a4609c804e 34 else
johnyp1845 0:26a4609c804e 35 {
johnyp1845 0:26a4609c804e 36 clamp = 0;
johnyp1845 0:26a4609c804e 37 }
johnyp1845 0:26a4609c804e 38 while(clamp == 1)
johnyp1845 0:26a4609c804e 39 {
johnyp1845 0:26a4609c804e 40 RMS = getRMS();
johnyp1845 1:797c7fc3a9b8 41 x = cutOff * hysteresis;//calculates offset
johnyp1845 1:797c7fc3a9b8 42 x = cutOff - x;// sets offset
johnyp1845 1:797c7fc3a9b8 43 if(RMS <= x)//checks for undervalue
johnyp1845 0:26a4609c804e 44 {
johnyp1845 0:26a4609c804e 45 clamp = 0;
johnyp1845 0:26a4609c804e 46 }
johnyp1845 0:26a4609c804e 47 else
johnyp1845 0:26a4609c804e 48 {
johnyp1845 0:26a4609c804e 49 clamp = 1;
johnyp1845 0:26a4609c804e 50 }
johnyp1845 0:26a4609c804e 51 }
johnyp1845 0:26a4609c804e 52 }
johnyp1845 0:26a4609c804e 53
johnyp1845 0:26a4609c804e 54 }
johnyp1845 0:26a4609c804e 55 double getRMS()
johnyp1845 0:26a4609c804e 56 {
johnyp1845 0:26a4609c804e 57 sum = 0;//resets sum and count
johnyp1845 0:26a4609c804e 58 count = 0;
johnyp1845 0:26a4609c804e 59 countCutOff = period * cycles;
johnyp1845 0:26a4609c804e 60 countCutOff = countCutOff * 10;
johnyp1845 0:26a4609c804e 61 while(count<=countCutOff)
johnyp1845 0:26a4609c804e 62 {
johnyp1845 0:26a4609c804e 63 value = 3.3*voltage.read(); //reads voltage
johnyp1845 0:26a4609c804e 64 squareValue = value*value; // squares value
johnyp1845 0:26a4609c804e 65 sum = sum + squareValue;// sums value in preperation for RMS
johnyp1845 0:26a4609c804e 66 count++; //increase sample counter
johnyp1845 0:26a4609c804e 67 wait_us(100);// wait
johnyp1845 0:26a4609c804e 68 }
johnyp1845 0:26a4609c804e 69 sum = sum/countCutOff; // averages sum
johnyp1845 0:26a4609c804e 70 RMSvalue = sqrt(sum); // Creates RMS value
johnyp1845 0:26a4609c804e 71 return RMSvalue;
johnyp1845 0:26a4609c804e 72 }