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@2:da51e13f4ddf, 2021-08-08 (annotated)
- Committer:
- juliusbernth
- Date:
- Sun Aug 08 12:29:22 2021 +0000
- Revision:
- 2:da51e13f4ddf
- Parent:
- 1:6e3f1776be82
- Child:
- 3:4f215646a42b
Base working version.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
juliusbernth | 0:64442857169c | 1 | /* mbed Microcontroller Library |
juliusbernth | 0:64442857169c | 2 | * Copyright (c) 2019 ARM Limited |
juliusbernth | 0:64442857169c | 3 | * SPDX-License-Identifier: Apache-2.0 |
juliusbernth | 0:64442857169c | 4 | */ |
juliusbernth | 0:64442857169c | 5 | |
juliusbernth | 0:64442857169c | 6 | #include "mbed.h" |
juliusbernth | 0:64442857169c | 7 | #include "platform/mbed_thread.h" |
juliusbernth | 0:64442857169c | 8 | #include "Settings.h" |
juliusbernth | 0:64442857169c | 9 | #include "QEI.h" |
juliusbernth | 0:64442857169c | 10 | //#include "LIS3DH.h" |
juliusbernth | 0:64442857169c | 11 | |
juliusbernth | 0:64442857169c | 12 | /* |
juliusbernth | 0:64442857169c | 13 | Open issues: |
juliusbernth | 0:64442857169c | 14 | - need to figure out how to handle transitions to error states |
juliusbernth | 0:64442857169c | 15 | - Need to test how breaking is performed. |
juliusbernth | 0:64442857169c | 16 | -Test onboard current limitation |
juliusbernth | 0:64442857169c | 17 | */ |
juliusbernth | 0:64442857169c | 18 | |
juliusbernth | 0:64442857169c | 19 | Timer encoderTimer; |
juliusbernth | 0:64442857169c | 20 | Timer buttonTimer; |
juliusbernth | 0:64442857169c | 21 | Timer testTimer; |
juliusbernth | 0:64442857169c | 22 | |
juliusbernth | 0:64442857169c | 23 | Timeout buttonTimeout; |
juliusbernth | 0:64442857169c | 24 | |
juliusbernth | 0:64442857169c | 25 | Thread PrintThread(osPriorityAboveNormal); |
juliusbernth | 0:64442857169c | 26 | Thread CentrifugeTestThread(osPriorityHigh); |
juliusbernth | 0:64442857169c | 27 | Thread ReadButtonThread(osPriorityNormal); |
juliusbernth | 0:64442857169c | 28 | Thread MotorControlThread(osPriorityRealtime); |
juliusbernth | 0:64442857169c | 29 | |
juliusbernth | 0:64442857169c | 30 | double SPIN_T[6]; //times for spin cycle |
juliusbernth | 0:64442857169c | 31 | |
juliusbernth | 0:64442857169c | 32 | QEI encoder(PA_14, PA_13, PA_15, 256, QEI::X4_ENCODING); |
juliusbernth | 0:64442857169c | 33 | |
juliusbernth | 0:64442857169c | 34 | //LIS3DH accel = LIS3DH(SPI_MOSI, SPI_MISO, SPI_SCLK, SPI_CS,LIS3DH_DR_NR_LP_400HZ, LIS3DH_FS_2G); |
juliusbernth | 0:64442857169c | 35 | |
juliusbernth | 0:64442857169c | 36 | DigitalOut EN_FAULTA (D2); |
juliusbernth | 0:64442857169c | 37 | DigitalOut STBY (D8); |
juliusbernth | 0:64442857169c | 38 | DigitalOut PHA (D3); |
juliusbernth | 0:64442857169c | 39 | DigitalOut PHB (D7); |
juliusbernth | 0:64442857169c | 40 | PwmOut PWMA (D5); |
juliusbernth | 0:64442857169c | 41 | PwmOut PWMB (D4); |
juliusbernth | 0:64442857169c | 42 | PwmOut REF_PWM_A (D15); |
juliusbernth | 0:64442857169c | 43 | PwmOut REF_PWM_B (D14); |
juliusbernth | 0:64442857169c | 44 | |
juliusbernth | 0:64442857169c | 45 | Serial pc(USBTX,USBRX); |
juliusbernth | 0:64442857169c | 46 | |
juliusbernth | 0:64442857169c | 47 | DigitalOut pinStatusLED (D13); |
juliusbernth | 0:64442857169c | 48 | DigitalIn pinButton (PC_13); |
juliusbernth | 0:64442857169c | 49 | |
juliusbernth | 0:64442857169c | 50 | /* Still to allocate |
juliusbernth | 0:64442857169c | 51 | |
juliusbernth | 0:64442857169c | 52 | DigitalOut pinBuzzer |
juliusbernth | 0:64442857169c | 53 | DigitalOut pinPowerLED |
juliusbernth | 0:64442857169c | 54 | |
juliusbernth | 0:64442857169c | 55 | */ |
juliusbernth | 0:64442857169c | 56 | |
juliusbernth | 0:64442857169c | 57 | //global variables |
juliusbernth | 0:64442857169c | 58 | double speedFilterConstant; |
juliusbernth | 0:64442857169c | 59 | char state; |
juliusbernth | 0:64442857169c | 60 | double currentPulses; |
juliusbernth | 0:64442857169c | 61 | double lastPulses; |
juliusbernth | 0:64442857169c | 62 | double lastSpeedRPM; |
juliusbernth | 0:64442857169c | 63 | double Tnow; |
juliusbernth | 0:64442857169c | 64 | double demandSpeed_RPM = 0.0; |
juliusbernth | 0:64442857169c | 65 | double currentSpeedRPM; |
juliusbernth | 0:64442857169c | 66 | double deltaError; |
juliusbernth | 0:64442857169c | 67 | double lastError; |
juliusbernth | 0:64442857169c | 68 | double integralTerm; |
juliusbernth | 0:64442857169c | 69 | double output; |
juliusbernth | 0:64442857169c | 70 | |
juliusbernth | 0:64442857169c | 71 | Ticker tickerMotorControl; |
juliusbernth | 0:64442857169c | 72 | Ticker tickerPrint; |
juliusbernth | 0:64442857169c | 73 | Ticker tickerReleaseButton; |
juliusbernth | 0:64442857169c | 74 | |
juliusbernth | 0:64442857169c | 75 | Semaphore semMotorControl(0); |
juliusbernth | 0:64442857169c | 76 | Semaphore semPrint(0); |
juliusbernth | 0:64442857169c | 77 | Semaphore semStartTest(0); |
juliusbernth | 0:64442857169c | 78 | Semaphore semButton(0); |
juliusbernth | 0:64442857169c | 79 | |
juliusbernth | 2:da51e13f4ddf | 80 | //Mutex mut1; |
juliusbernth | 0:64442857169c | 81 | |
juliusbernth | 0:64442857169c | 82 | double LimitDouble(double input, double min, double max){ |
juliusbernth | 0:64442857169c | 83 | double output; |
juliusbernth | 0:64442857169c | 84 | output = input; |
juliusbernth | 0:64442857169c | 85 | if (input > max){ |
juliusbernth | 0:64442857169c | 86 | output = max; |
juliusbernth | 0:64442857169c | 87 | } |
juliusbernth | 0:64442857169c | 88 | if (input < min){ |
juliusbernth | 0:64442857169c | 89 | output = min; |
juliusbernth | 0:64442857169c | 90 | } |
juliusbernth | 0:64442857169c | 91 | return output; |
juliusbernth | 0:64442857169c | 92 | } |
juliusbernth | 0:64442857169c | 93 | /* |
juliusbernth | 0:64442857169c | 94 | double GetSpeed(int pulses, int lastPulses, double currentSpeed){ |
juliusbernth | 0:64442857169c | 95 | double filteredSpeedRPM; |
juliusbernth | 0:64442857169c | 96 | |
juliusbernth | 0:64442857169c | 97 | //find change in pulses. |
juliusbernth | 0:64442857169c | 98 | double speedRPM = (double) (pulses - lastPulses); //get change in angle |
juliusbernth | 0:64442857169c | 99 | speedRPM /= encoderTimer.read_us(); //calculate speed in pulses/us |
juliusbernth | 0:64442857169c | 100 | encoderTimer.reset(); |
juliusbernth | 0:64442857169c | 101 | speedRPM *= 60000000.0/PULSES_PER_REV/4.0;//convert to RPM; |
juliusbernth | 0:64442857169c | 102 | |
juliusbernth | 0:64442857169c | 103 | //filter speed |
juliusbernth | 0:64442857169c | 104 | //filteredSpeedRPM = speedFilterConstant * speedRPM + currentSpeed - speedFilterConstant * currentSpeed; |
juliusbernth | 0:64442857169c | 105 | filteredSpeedRPM = speedRPM; |
juliusbernth | 0:64442857169c | 106 | return filteredSpeedRPM; |
juliusbernth | 0:64442857169c | 107 | }*/ |
juliusbernth | 0:64442857169c | 108 | |
juliusbernth | 0:64442857169c | 109 | |
juliusbernth | 0:64442857169c | 110 | void PIDControl(){ |
juliusbernth | 0:64442857169c | 111 | |
juliusbernth | 0:64442857169c | 112 | } |
juliusbernth | 0:64442857169c | 113 | |
juliusbernth | 0:64442857169c | 114 | void TestCompleteNotification(){ |
juliusbernth | 0:64442857169c | 115 | //send message |
juliusbernth | 0:64442857169c | 116 | printf("\r\nTest complete\r\n"); |
juliusbernth | 0:64442857169c | 117 | //sound buzzer and blink 3 times |
juliusbernth | 0:64442857169c | 118 | } |
juliusbernth | 0:64442857169c | 119 | |
juliusbernth | 0:64442857169c | 120 | void MotorControlRelease(){ |
juliusbernth | 0:64442857169c | 121 | semMotorControl.release(); |
juliusbernth | 0:64442857169c | 122 | } |
juliusbernth | 0:64442857169c | 123 | void PrintRelease(){ |
juliusbernth | 0:64442857169c | 124 | semPrint.release(); |
juliusbernth | 0:64442857169c | 125 | } |
juliusbernth | 0:64442857169c | 126 | |
juliusbernth | 0:64442857169c | 127 | void PrintTimeRemaining(){ |
juliusbernth | 0:64442857169c | 128 | while(1){ |
juliusbernth | 0:64442857169c | 129 | semPrint.wait(); |
juliusbernth | 0:64442857169c | 130 | if(state == STATE_RUNNING){ |
juliusbernth | 0:64442857169c | 131 | double timeElapsed = testTimer.read(); |
juliusbernth | 0:64442857169c | 132 | double timeRemaining = SPIN_T[5] - timeElapsed; |
juliusbernth | 0:64442857169c | 133 | int displayTime = int(timeRemaining)+1; |
juliusbernth | 0:64442857169c | 134 | //printf("Time remaining %d s\r\n", displayTime); |
juliusbernth | 2:da51e13f4ddf | 135 | // mut1.lock(); |
juliusbernth | 2:da51e13f4ddf | 136 | // double printDemandSpeed = demandSpeed_RPM; |
juliusbernth | 2:da51e13f4ddf | 137 | // double printCurrentSpeed = currentSpeedRPM; |
juliusbernth | 2:da51e13f4ddf | 138 | // mut1.unlock();//this is the first change |
juliusbernth | 0:64442857169c | 139 | printf("%f\t%f\r\n",demandSpeed_RPM, currentSpeedRPM); |
juliusbernth | 0:64442857169c | 140 | } |
juliusbernth | 0:64442857169c | 141 | } |
juliusbernth | 0:64442857169c | 142 | } |
juliusbernth | 0:64442857169c | 143 | |
juliusbernth | 0:64442857169c | 144 | void MotorControl(){ |
juliusbernth | 0:64442857169c | 145 | while(1){ |
juliusbernth | 0:64442857169c | 146 | semMotorControl.wait();//wait for a signal |
juliusbernth | 0:64442857169c | 147 | if(state == STATE_RUNNING){//need to check if this is the best condition to look for. |
juliusbernth | 0:64442857169c | 148 | |
juliusbernth | 0:64442857169c | 149 | Tnow = testTimer.read(); |
juliusbernth | 0:64442857169c | 150 | //check accel. If problem, change state to ERROR |
juliusbernth | 0:64442857169c | 151 | |
juliusbernth | 0:64442857169c | 152 | //int deltaT = encoderTimer.read();//read current time in seconds |
juliusbernth | 0:64442857169c | 153 | |
juliusbernth | 0:64442857169c | 154 | //calculate current demand |
juliusbernth | 0:64442857169c | 155 | if ( (SPIN_T[0] <= Tnow) && (Tnow < SPIN_T[1]) ) {//test is in warmup |
juliusbernth | 0:64442857169c | 156 | demandSpeed_RPM = 0.0; |
juliusbernth | 0:64442857169c | 157 | //printf("warm up %f\r\n", demandSpeed_RPM); |
juliusbernth | 0:64442857169c | 158 | //deactivate motor? |
juliusbernth | 0:64442857169c | 159 | } |
juliusbernth | 0:64442857169c | 160 | if ((SPIN_T[1] <= Tnow)&&(Tnow < SPIN_T[2])){//test is in ramp up |
juliusbernth | 0:64442857169c | 161 | |
juliusbernth | 0:64442857169c | 162 | double a = Tnow - SPIN_T[1]; |
juliusbernth | 0:64442857169c | 163 | demandSpeed_RPM = a * targetSpeed_RPM / T_RAMP; |
juliusbernth | 0:64442857169c | 164 | //printf("ramp up %f\r\n", demandSpeed_RPM); |
juliusbernth | 0:64442857169c | 165 | } |
juliusbernth | 0:64442857169c | 166 | if ((SPIN_T[2] <= Tnow)&&(Tnow < SPIN_T[3])){//test is in coast |
juliusbernth | 0:64442857169c | 167 | demandSpeed_RPM = targetSpeed_RPM; |
juliusbernth | 0:64442857169c | 168 | //printf("coast %f\r\n", demandSpeed_RPM); |
juliusbernth | 0:64442857169c | 169 | } |
juliusbernth | 0:64442857169c | 170 | if ((SPIN_T[3] <= Tnow)&&(Tnow < SPIN_T[4])){//test is in ramp down |
juliusbernth | 0:64442857169c | 171 | double a = Tnow - SPIN_T[3]; |
juliusbernth | 0:64442857169c | 172 | demandSpeed_RPM = targetSpeed_RPM - (a/T_RAMP*targetSpeed_RPM); |
juliusbernth | 0:64442857169c | 173 | //printf("ramp down %f\r\n", demandSpeed_RPM); |
juliusbernth | 0:64442857169c | 174 | } |
juliusbernth | 0:64442857169c | 175 | if ((SPIN_T[4] <= Tnow)&&(Tnow < SPIN_T[5])){//test is in cooldown |
juliusbernth | 0:64442857169c | 176 | demandSpeed_RPM = 0.0; |
juliusbernth | 0:64442857169c | 177 | //printf("cool down %f\r\n", demandSpeed_RPM); |
juliusbernth | 0:64442857169c | 178 | //deactivate motor? |
juliusbernth | 0:64442857169c | 179 | } |
juliusbernth | 0:64442857169c | 180 | |
juliusbernth | 0:64442857169c | 181 | demandSpeed_RPM = LimitDouble(demandSpeed_RPM,0.0,MAX_SPEED_RPM);//limit demand |
juliusbernth | 0:64442857169c | 182 | currentPulses = encoder.getPulses();//calculate current speed |
juliusbernth | 0:64442857169c | 183 | double deltaPulses = currentPulses - lastPulses; |
juliusbernth | 0:64442857169c | 184 | double speed_RPM = deltaPulses / SAMPLE_TIME_US * 1000000;//get in pulses per second |
juliusbernth | 0:64442857169c | 185 | speed_RPM *= 60/4/PULSES_PER_REV;//convert to RPM |
juliusbernth | 0:64442857169c | 186 | |
juliusbernth | 0:64442857169c | 187 | currentSpeedRPM = 0.3*speed_RPM + 0.7*lastSpeedRPM;//filter speed |
juliusbernth | 0:64442857169c | 188 | |
juliusbernth | 0:64442857169c | 189 | double error = demandSpeed_RPM - currentSpeedRPM;//calculate error |
juliusbernth | 0:64442857169c | 190 | deltaError = error - lastError; |
juliusbernth | 0:64442857169c | 191 | double errorDot = deltaError/SAMPLE_TIME_US*1000000.0; |
juliusbernth | 0:64442857169c | 192 | integralTerm += error*Ki*SAMPLE_TIME_US/1000000.0; |
juliusbernth | 0:64442857169c | 193 | integralTerm = LimitDouble(integralTerm,-0.8, 0.8); |
juliusbernth | 0:64442857169c | 194 | |
juliusbernth | 0:64442857169c | 195 | output = Kp * error; //calculate output |
juliusbernth | 0:64442857169c | 196 | output += integralTerm; |
juliusbernth | 0:64442857169c | 197 | output += Kd*errorDot; |
juliusbernth | 0:64442857169c | 198 | output = LimitDouble(output,-1.0,1.0); |
juliusbernth | 0:64442857169c | 199 | //printf("wd:%f\t w:%f\t e:%f\t o:%f \r\n",demandSpeed_RPM, currentSpeedRPM, error, output); |
juliusbernth | 0:64442857169c | 200 | |
juliusbernth | 0:64442857169c | 201 | if(output >=0){//Set direction |
juliusbernth | 0:64442857169c | 202 | PHA.write(0); |
juliusbernth | 0:64442857169c | 203 | }else{ |
juliusbernth | 0:64442857169c | 204 | PHA.write(1); |
juliusbernth | 0:64442857169c | 205 | output = -1*output; |
juliusbernth | 0:64442857169c | 206 | } |
juliusbernth | 0:64442857169c | 207 | PWMA.write(output);//write to motor |
juliusbernth | 0:64442857169c | 208 | |
juliusbernth | 0:64442857169c | 209 | lastPulses = currentPulses;//update |
juliusbernth | 0:64442857169c | 210 | lastError = error; |
juliusbernth | 0:64442857169c | 211 | lastSpeedRPM = currentSpeedRPM; |
juliusbernth | 0:64442857169c | 212 | |
juliusbernth | 0:64442857169c | 213 | //exit when test has completed |
juliusbernth | 0:64442857169c | 214 | if (Tnow >= SPIN_T[5]){ |
juliusbernth | 0:64442857169c | 215 | printf("Terminating Test %f \t %f\r\n", Tnow, SPIN_T[5]); |
juliusbernth | 0:64442857169c | 216 | state = STATE_READY;//change state |
juliusbernth | 0:64442857169c | 217 | testTimer.stop(); //stop and reset timers |
juliusbernth | 0:64442857169c | 218 | testTimer.reset(); |
juliusbernth | 0:64442857169c | 219 | Tnow = testTimer.read(); |
juliusbernth | 0:64442857169c | 220 | encoderTimer.stop(); |
juliusbernth | 0:64442857169c | 221 | encoderTimer.reset(); |
juliusbernth | 0:64442857169c | 222 | EN_FAULTA.write(0);//disable motor |
juliusbernth | 0:64442857169c | 223 | |
juliusbernth | 0:64442857169c | 224 | tickerMotorControl.detach(); //detach the semaphore release for motor control |
juliusbernth | 0:64442857169c | 225 | tickerPrint.detach(); |
juliusbernth | 0:64442857169c | 226 | |
juliusbernth | 0:64442857169c | 227 | printf("state = %d\r\n",state); |
juliusbernth | 0:64442857169c | 228 | TestCompleteNotification();//send notification |
juliusbernth | 0:64442857169c | 229 | //deactivate motor |
juliusbernth | 0:64442857169c | 230 | //PrintThread.terminate();//terminate print thread |
juliusbernth | 0:64442857169c | 231 | //CentrifugeTestThread.terminate();//terminate threads |
juliusbernth | 0:64442857169c | 232 | } |
juliusbernth | 0:64442857169c | 233 | } |
juliusbernth | 0:64442857169c | 234 | } |
juliusbernth | 0:64442857169c | 235 | } |
juliusbernth | 0:64442857169c | 236 | |
juliusbernth | 0:64442857169c | 237 | void CentrifugeTest(){ |
juliusbernth | 0:64442857169c | 238 | while(1){ |
juliusbernth | 0:64442857169c | 239 | semStartTest.wait(); |
juliusbernth | 0:64442857169c | 240 | printf("\r\n Test starting \r\n"); |
juliusbernth | 0:64442857169c | 241 | state = STATE_RUNNING; |
juliusbernth | 0:64442857169c | 242 | //set up test |
juliusbernth | 0:64442857169c | 243 | testTimer.reset(); |
juliusbernth | 0:64442857169c | 244 | testTimer.start();//start timer |
juliusbernth | 0:64442857169c | 245 | |
juliusbernth | 0:64442857169c | 246 | char spinState; |
juliusbernth | 0:64442857169c | 247 | |
juliusbernth | 0:64442857169c | 248 | //set up ticker to allow motor control thread to run periodically |
juliusbernth | 0:64442857169c | 249 | |
juliusbernth | 0:64442857169c | 250 | encoder.reset();//reset encoder |
juliusbernth | 0:64442857169c | 251 | lastPulses = 0;//reset previous encoder reading |
juliusbernth | 0:64442857169c | 252 | encoderTimer.start(); |
juliusbernth | 0:64442857169c | 253 | PrintThread.start(PrintTimeRemaining); |
juliusbernth | 0:64442857169c | 254 | printf("\r\n Test setup complete \r\n"); |
juliusbernth | 0:64442857169c | 255 | EN_FAULTA.write(1); |
juliusbernth | 0:64442857169c | 256 | if(state == STATE_RUNNING){ |
juliusbernth | 0:64442857169c | 257 | printf("\r\n running %d\r\n",state); |
juliusbernth | 0:64442857169c | 258 | } |
juliusbernth | 0:64442857169c | 259 | |
juliusbernth | 0:64442857169c | 260 | |
juliusbernth | 0:64442857169c | 261 | } |
juliusbernth | 0:64442857169c | 262 | } |
juliusbernth | 0:64442857169c | 263 | |
juliusbernth | 0:64442857169c | 264 | void ReleaseReadButton(){ |
juliusbernth | 0:64442857169c | 265 | semButton.release(); |
juliusbernth | 0:64442857169c | 266 | } |
juliusbernth | 0:64442857169c | 267 | |
juliusbernth | 0:64442857169c | 268 | void ReadButton() |
juliusbernth | 0:64442857169c | 269 | { |
juliusbernth | 0:64442857169c | 270 | int countThreashold = int(BUTTON_HOLD_TIME_S/BUTTON_READ_SAMPLETIME_S); |
juliusbernth | 0:64442857169c | 271 | while (1) |
juliusbernth | 0:64442857169c | 272 | { |
juliusbernth | 0:64442857169c | 273 | semButton.wait(); |
juliusbernth | 0:64442857169c | 274 | int count = 0; |
juliusbernth | 0:64442857169c | 275 | |
juliusbernth | 0:64442857169c | 276 | while(!pinButton.read()) |
juliusbernth | 0:64442857169c | 277 | { |
juliusbernth | 0:64442857169c | 278 | count++; |
juliusbernth | 0:64442857169c | 279 | if (count ==1){ |
juliusbernth | 0:64442857169c | 280 | printf("button pressed\r\n"); |
juliusbernth | 0:64442857169c | 281 | } |
juliusbernth | 0:64442857169c | 282 | ThisThread::sleep_for(10); |
juliusbernth | 0:64442857169c | 283 | } |
juliusbernth | 0:64442857169c | 284 | switch (state) |
juliusbernth | 0:64442857169c | 285 | { |
juliusbernth | 0:64442857169c | 286 | case STATE_READY: |
juliusbernth | 0:64442857169c | 287 | if(count > countThreashold) |
juliusbernth | 0:64442857169c | 288 | { |
juliusbernth | 0:64442857169c | 289 | printf("button released count = %d\r\n",count); |
juliusbernth | 0:64442857169c | 290 | count = 0; |
juliusbernth | 0:64442857169c | 291 | //CentrifugeTestThread.start(CentrifugeTest); |
juliusbernth | 0:64442857169c | 292 | semStartTest.release(); |
juliusbernth | 0:64442857169c | 293 | tickerMotorControl.attach_us(&MotorControlRelease, SAMPLE_TIME_US);//set up signal for control frequency |
juliusbernth | 0:64442857169c | 294 | tickerPrint.attach(&PrintRelease,PRINT_TIME_S); |
juliusbernth | 0:64442857169c | 295 | } |
juliusbernth | 0:64442857169c | 296 | break; |
juliusbernth | 0:64442857169c | 297 | case STATE_RUNNING: |
juliusbernth | 0:64442857169c | 298 | if(count >1){ |
juliusbernth | 0:64442857169c | 299 | EN_FAULTA.write(0); |
juliusbernth | 0:64442857169c | 300 | } |
juliusbernth | 0:64442857169c | 301 | break; |
juliusbernth | 0:64442857169c | 302 | |
juliusbernth | 0:64442857169c | 303 | case STATE_ERROR: |
juliusbernth | 0:64442857169c | 304 | |
juliusbernth | 0:64442857169c | 305 | break; |
juliusbernth | 0:64442857169c | 306 | default: |
juliusbernth | 0:64442857169c | 307 | break; |
juliusbernth | 0:64442857169c | 308 | } |
juliusbernth | 0:64442857169c | 309 | count = 0; |
juliusbernth | 0:64442857169c | 310 | ThisThread::sleep_for(100); |
juliusbernth | 0:64442857169c | 311 | } |
juliusbernth | 0:64442857169c | 312 | } |
juliusbernth | 0:64442857169c | 313 | |
juliusbernth | 0:64442857169c | 314 | int main(){ |
juliusbernth | 0:64442857169c | 315 | printf("\r\nSystem Online\r\n"); |
juliusbernth | 0:64442857169c | 316 | //set up system |
juliusbernth | 0:64442857169c | 317 | |
juliusbernth | 0:64442857169c | 318 | //set up motor |
juliusbernth | 0:64442857169c | 319 | //EN_FAULTA.mode(PullDown); |
juliusbernth | 0:64442857169c | 320 | //STBY.mode(PullDown); |
juliusbernth | 0:64442857169c | 321 | |
juliusbernth | 0:64442857169c | 322 | REF_PWM_A.period_us(100); |
juliusbernth | 0:64442857169c | 323 | PWMA.period_us(100); |
juliusbernth | 0:64442857169c | 324 | REF_PWM_A.write(0.5);//set current reference |
juliusbernth | 0:64442857169c | 325 | |
juliusbernth | 0:64442857169c | 326 | EN_FAULTA.write(0); |
juliusbernth | 0:64442857169c | 327 | STBY.write(1); |
juliusbernth | 0:64442857169c | 328 | //calculate filter constant |
juliusbernth | 0:64442857169c | 329 | speedFilterConstant = 2*3.141593*SAMPLE_TIME_US/1000000*FILTER_CUTOFF_FREQ; |
juliusbernth | 0:64442857169c | 330 | speedFilterConstant /= (1 + speedFilterConstant); |
juliusbernth | 0:64442857169c | 331 | |
juliusbernth | 0:64442857169c | 332 | SPIN_T[0] = 0.0; |
juliusbernth | 0:64442857169c | 333 | SPIN_T[1] = T_WARMUP; |
juliusbernth | 0:64442857169c | 334 | SPIN_T[2] = T_WARMUP + T_RAMP; |
juliusbernth | 0:64442857169c | 335 | SPIN_T[3] = T_WARMUP + T_RAMP + T_TEST; |
juliusbernth | 0:64442857169c | 336 | SPIN_T[4] = T_WARMUP + T_RAMP + T_TEST + T_RAMP; |
juliusbernth | 0:64442857169c | 337 | SPIN_T[5] = T_WARMUP + T_RAMP + T_TEST + T_RAMP + T_WARMUP; |
juliusbernth | 0:64442857169c | 338 | |
juliusbernth | 0:64442857169c | 339 | /* //set up button interrupts |
juliusbernth | 0:64442857169c | 340 | pinButton.rise(ISR_Button_Rise); |
juliusbernth | 0:64442857169c | 341 | pinButton.fall(ISR_Button_Fall); |
juliusbernth | 0:64442857169c | 342 | */ |
juliusbernth | 0:64442857169c | 343 | |
juliusbernth | 0:64442857169c | 344 | state = STATE_READY;//set state to READY |
juliusbernth | 0:64442857169c | 345 | //start all threads |
juliusbernth | 0:64442857169c | 346 | MotorControlThread.start(MotorControl); |
juliusbernth | 0:64442857169c | 347 | |
juliusbernth | 0:64442857169c | 348 | CentrifugeTestThread.start(CentrifugeTest); |
juliusbernth | 0:64442857169c | 349 | //start print thread. |
juliusbernth | 0:64442857169c | 350 | |
juliusbernth | 0:64442857169c | 351 | ReadButtonThread.start(ReadButton); |
juliusbernth | 0:64442857169c | 352 | tickerReleaseButton.attach(ReleaseReadButton,BUTTON_READ_SAMPLETIME_S); |
juliusbernth | 0:64442857169c | 353 | |
juliusbernth | 0:64442857169c | 354 | |
juliusbernth | 0:64442857169c | 355 | printf("\r\nSetup Complete\r\n"); |
juliusbernth | 0:64442857169c | 356 | while (true) { |
juliusbernth | 0:64442857169c | 357 | //check state of button |
juliusbernth | 0:64442857169c | 358 | ThisThread::sleep_for(osWaitForever); |
juliusbernth | 0:64442857169c | 359 | } |
juliusbernth | 0:64442857169c | 360 | } |