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: mbed QEI HIDScope biquadFilter MODSERIAL FastPWM
main.cpp@3:4f281c336a8b, 2019-10-07 (annotated)
- Committer:
- paulstuiver
- Date:
- Mon Oct 07 13:02:09 2019 +0000
- Revision:
- 3:4f281c336a8b
- Parent:
- 2:75b2f713161c
- Child:
- 4:fb67a4284aaa
- Child:
- 5:2ae500da8fe1
WORKING motor control
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| RobertoO | 0:67c50348f842 | 1 | #include "mbed.h" |
| RobertoO | 1:b862262a9d14 | 2 | #include "MODSERIAL.h" |
| paulstuiver | 2:75b2f713161c | 3 | #include "FastPWM.h" |
| paulstuiver | 2:75b2f713161c | 4 | |
| paulstuiver | 2:75b2f713161c | 5 | DigitalIn button1(D12); |
| paulstuiver | 2:75b2f713161c | 6 | AnalogIn pot2(A0); |
| paulstuiver | 2:75b2f713161c | 7 | Ticker john; |
| paulstuiver | 3:4f281c336a8b | 8 | DigitalOut motor1Direction(D7); |
| paulstuiver | 3:4f281c336a8b | 9 | FastPWM motor1Velocity(D6); |
| paulstuiver | 2:75b2f713161c | 10 | Serial pc(USBTX, USBRX); |
| paulstuiver | 2:75b2f713161c | 11 | volatile double frequency = 17000.0; |
| paulstuiver | 2:75b2f713161c | 12 | volatile double period_signal = 1.0/frequency; |
| paulstuiver | 3:4f281c336a8b | 13 | float vel = 0.0f; |
| paulstuiver | 3:4f281c336a8b | 14 | float referencevelocity; |
| paulstuiver | 3:4f281c336a8b | 15 | float motorvalue2; |
| paulstuiver | 2:75b2f713161c | 16 | |
| paulstuiver | 2:75b2f713161c | 17 | //get the measured velocity |
| paulstuiver | 2:75b2f713161c | 18 | double getmeasuredvelocity() |
| paulstuiver | 2:75b2f713161c | 19 | { |
| paulstuiver | 2:75b2f713161c | 20 | float measuredvelocity; |
| paulstuiver | 2:75b2f713161c | 21 | measuredvelocity = pot2.read(); |
| paulstuiver | 2:75b2f713161c | 22 | return measuredvelocity; |
| paulstuiver | 2:75b2f713161c | 23 | } |
| paulstuiver | 2:75b2f713161c | 24 | |
| paulstuiver | 2:75b2f713161c | 25 | //get the reference of the velocity: positive or negative |
| paulstuiver | 2:75b2f713161c | 26 | double getreferencevelocity() |
| paulstuiver | 2:75b2f713161c | 27 | { |
| paulstuiver | 3:4f281c336a8b | 28 | referencevelocity = -1.0 + 2.0*pot2.read(); |
| paulstuiver | 2:75b2f713161c | 29 | return referencevelocity; |
| paulstuiver | 2:75b2f713161c | 30 | } |
| RobertoO | 0:67c50348f842 | 31 | |
| paulstuiver | 2:75b2f713161c | 32 | //send value to motor |
| paulstuiver | 2:75b2f713161c | 33 | void sendtomotor(float motorvalue) |
| paulstuiver | 2:75b2f713161c | 34 | { |
| paulstuiver | 3:4f281c336a8b | 35 | motorvalue2 = motorvalue; |
| paulstuiver | 2:75b2f713161c | 36 | vel = fabs(motorvalue); |
| paulstuiver | 3:4f281c336a8b | 37 | vel = vel > 1.0f ? 1.0f : vel; |
| paulstuiver | 2:75b2f713161c | 38 | motor1Velocity = vel; |
| RobertoO | 0:67c50348f842 | 39 | |
| paulstuiver | 2:75b2f713161c | 40 | motor1Direction = (motorvalue > 0.0f); |
| paulstuiver | 2:75b2f713161c | 41 | } |
| RobertoO | 0:67c50348f842 | 42 | |
| paulstuiver | 2:75b2f713161c | 43 | // function to call reference velocity, measured velocity and controls motor with feedback |
| paulstuiver | 2:75b2f713161c | 44 | void measureandcontrol(void) |
| paulstuiver | 2:75b2f713161c | 45 | { |
| paulstuiver | 2:75b2f713161c | 46 | float referencevelocity = getreferencevelocity(); |
| paulstuiver | 2:75b2f713161c | 47 | float measuredvelocity = getmeasuredvelocity(); |
| paulstuiver | 2:75b2f713161c | 48 | sendtomotor(referencevelocity); |
| paulstuiver | 2:75b2f713161c | 49 | } |
| RobertoO | 0:67c50348f842 | 50 | int main() |
| RobertoO | 0:67c50348f842 | 51 | { |
| RobertoO | 0:67c50348f842 | 52 | pc.baud(115200); |
| paulstuiver | 2:75b2f713161c | 53 | pc.printf("Starting...\r\n"); |
| paulstuiver | 2:75b2f713161c | 54 | motor1Velocity.period(period_signal); |
| paulstuiver | 2:75b2f713161c | 55 | john.attach(measureandcontrol, 0.001f); |
| paulstuiver | 2:75b2f713161c | 56 | while(true) |
| paulstuiver | 2:75b2f713161c | 57 | { |
| paulstuiver | 2:75b2f713161c | 58 | wait(0.5); |
| paulstuiver | 3:4f281c336a8b | 59 | pc.printf("velocity = %f\r\n", vel); |
| paulstuiver | 3:4f281c336a8b | 60 | pc.printf("motor1Direction = %i\r\n", (int)motor1Direction); |
| paulstuiver | 3:4f281c336a8b | 61 | pc.printf("motorvalue2 = %f\r\n", motorvalue2); |
| RobertoO | 0:67c50348f842 | 62 | } |
| paulstuiver | 2:75b2f713161c | 63 | } |