For flywheel rig. Interfaces with matlab flywheel interface. 2.007 MIT Biomimetics Robotics Lab. Sangbae Kim. Ben Katz.
Diff: main.cpp
- Revision:
- 11:ad41446b0edb
- Parent:
- 10:ae139f40acc0
- Child:
- 12:f9f08c7551f4
--- a/main.cpp Thu Feb 16 23:27:49 2017 +0000 +++ b/main.cpp Fri Apr 14 17:35:05 2017 +0000 @@ -19,6 +19,9 @@ const int CPR = 900*4; // Encoder counts per revolution. Change to match your encoder. x4 for quadrature! const double VREF = 3; //Microcontroller reference voltage const float currentSensorOutputRatio = 0.185; // Volts/Amp. Divide Voltage by cSenseOutput to get current +const float PSupply_Voltage = 12.0; +const float Output_Voltage = 2.0; +const float pwm_flywheel = Output_Voltage/PSupply_Voltage; void EncoderInitialise(void) { // configure GPIO PA0 & PA1 as inputs for Encoder @@ -91,39 +94,32 @@ float currentSensorOffset = 0; int i; for(i=1;i<301;i++){ currentSensorOffset += currentSense.read(); } currentSensorOffset = currentSensorOffset*VREF/300; - EncoderInitialise(); fflush(pc); - /*** wait here for matlab information like voltage before starting? ***/ - + while(1) { -// currentSensorOffset = 0; i = 0; -// for(i=1;i<201;i++){ currentSensorOffset += currentSense.read(); } //TODO: watch out this could take some time, but this should make things better! -// currentSensorOffset = currentSensorOffset*VREF/300; while(1) { green = true; - if(pc.readable()) - { + if(pc.readable()) { char charIn = pc.getc(); if(charIn == 'g'){ - fflush(pc); // TODO: this was recently changed check to see it causes no bugs + fflush(pc); absoluteStart = clock(); - end = clock(); /* TODO: fix clock things */ + end = clock(); ZeroEncoder(); velocity = 0; startcount = 0; endcount = 0; currentSensed = 0; + pwm.write(pwm_flywheel); break; - } else if(isdigit(charIn)) { - double abrahamsCommand = (double)(charIn - '0'); - pwm.write(abrahamsCommand/10.0); } } + wait(0.05); } @@ -147,17 +143,14 @@ { char charIn = pc.getc(); if(charIn == 'r'){ - fflush(pc); /* TODO: purge much better than this! */ - pwm.write(0.0); /* eliminates the need to write r and 0 commands in matlab which mbed couldn't read fast enough */ + fflush(pc); + pwm.write(0.0); break; - } else if(isdigit(charIn)) { - double abrahamsCommand = (double)(charIn - '0'); - pwm.write(abrahamsCommand/10.0); } } if(publishCounter == samplesPerPublish) { - printf("%+9.5f,%+8.4f,%+9.5f,%+8.2f\n", currentSensed/currentSensorOutputRatio, pwm.read(), velocity, ((double)(end-absoluteStart)/CLOCKS_PER_SEC)); - publishCounter = 1; // good for 1000 seconds. and room to grow one power of 10 on all other inputs. + printf("%+9.5f,%+8.4f,%+9.4f,%+8.2f\n", currentSensed/currentSensorOutputRatio, pwm.read(), -velocity, ((double)(end-absoluteStart)/CLOCKS_PER_SEC)); + publishCounter = 1; // good for 1000 seconds. and room to grow one power of 10 on all other inputs. //just changed velocity to 4 precision to give it extra room for no load? bug maybe? } publishCounter++;