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.
Dependencies: MODSERIAL QEI mbed HIDScope
Fork of prog_pract3 by
Diff: main.cpp
- Revision:
- 7:9f4e35d977af
- Parent:
- 6:84a01494d836
--- a/main.cpp Mon Oct 10 15:03:42 2016 +0000 +++ b/main.cpp Mon Oct 10 15:26:27 2016 +0000 @@ -18,9 +18,9 @@ HIDScope scope(2); float referenceVelocity = 0; -int counts; -double DerivativeCounts; -int countsPrev = 0; +volatile int counts; +volatile float DerivativeCounts; +volatile int countsPrev = 0; volatile bool MeasureTicker_go=false, TimeTracker_go=false, sampleT_go=false; @@ -28,19 +28,6 @@ void TimeTracker_act(){TimeTracker_go=true;}; void sampleT_act(){sampleT_go=true;}; -// encoder in HIDScope setten -void sample() -{ - QEI Encoder(D12, D13, NC, 32); - counts = Encoder.getPulses(); // gives position - scope.set(0, counts); - DerivativeCounts = (counts-countsPrev)/0.001; - scope.set(1, DerivativeCounts); - countsPrev = counts; - scope.send(); -} - - float GetReferenceVelocity() { // Returns reference velocity in rad/s. @@ -65,7 +52,7 @@ { // very simple linear feed-forward control const float MotorGain=8.4; // unit: (rad/s) / PWM - float motorValue = referenceVelocity / MotorGain; + volatile float motorValue = referenceVelocity / MotorGain; return motorValue; } @@ -81,23 +68,36 @@ else motor1MagnitudePin = fabs(motorValue); } +void EncoderHIDScope() // encoder in HIDScope zetten +{ + QEI Encoder(D12, D13, NC, 32); + counts = Encoder.getPulses(); // gives position + scope.set(0, counts); + DerivativeCounts = (counts-countsPrev)/0.001; + scope.set(1, DerivativeCounts); + countsPrev = counts; + scope.send(); +} + void MeasureAndControl(void) { // This function measures the potmeter position, extracts a // reference velocity from it, and controls the motor with // a simple FeedForward controller. Call this from a Ticker. - float referenceVelocity = GetReferenceVelocity(); - float motorValue = FeedForwardControl(referenceVelocity); + volatile float referenceVelocity = GetReferenceVelocity(); + volatile float motorValue = FeedForwardControl(referenceVelocity); SetMotor1(motorValue); + EncoderHIDScope(); //pc.printf("MotorValue: %f rad/s \r\n", motorValue); } -//void TimeTrackerF(){ - //wait(1); - //float Potmeter = potMeterIn.read(); - //pc.printf("Reference velocity: %f rad/s \r\n", referenceVelocity); - //pc.printf("Potmeter: %f rad/s \r\n", Potmeter); - +void TimeTrackerF(){ + wait(1); + float Potmeter = potMeterIn.read(); + pc.printf("Reference velocity: %f rad/s \r\n", referenceVelocity); + pc.printf("Potmeter: %f rad/s \r\n", Potmeter); + //pc.printf("MotorValue: %f rad/s \r\n", motorValue); + pc.printf("Encoder counts: %i \r\n", countsPrev); } int main() @@ -105,7 +105,7 @@ //Initialize led1=0; led2=0; - int counts; + counts; //float Potmeter = potMeterIn.read(); MeasureTicker.attach(&MeasureTicker_act, 0.01f); TimeTracker.attach(&TimeTracker_act, 0.3f); @@ -121,18 +121,16 @@ if (MeasureTicker_go){ MeasureTicker_go=false; MeasureAndControl(); - counts = Encoder.getPulses(); // gives position + //counts = Encoder.getPulses(); // gives position //pc.printf("Encoder counts: %i \r\n", counts); } - /*if (TimeTracker_go){ + if (TimeTracker_go){ TimeTracker_go=false; TimeTrackerF(); - }*/ + } if (sampleT_go){ sampleT_go=false; - sample(); + EncoderHIDScope(); } } } - -