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@8:7dab565a208e, 2019-10-14 (annotated)
- Committer:
- Floris_Hoek
- Date:
- Mon Oct 14 14:00:10 2019 +0000
- Revision:
- 8:7dab565a208e
- Parent:
- 5:2ae500da8fe1
- Child:
- 9:e8cc37a94fec
program split into different files to keep program organised
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| RobertoO | 0:67c50348f842 | 1 | #include "mbed.h" |
| Floris_Hoek | 8:7dab565a208e | 2 | #include "HIDScope.h" |
| Floris_Hoek | 8:7dab565a208e | 3 | #include "BiQuad.h" |
| RobertoO | 1:b862262a9d14 | 4 | #include "MODSERIAL.h" |
| paulstuiver | 2:75b2f713161c | 5 | #include "FastPWM.h" |
| paulstuiver | 5:2ae500da8fe1 | 6 | #include "QEI.h" |
| paulstuiver | 5:2ae500da8fe1 | 7 | #include <math.h> |
| paulstuiver | 2:75b2f713161c | 8 | |
| Floris_Hoek | 8:7dab565a208e | 9 | #include "Motor_Control.h" |
| Floris_Hoek | 8:7dab565a208e | 10 | |
| paulstuiver | 2:75b2f713161c | 11 | DigitalIn button1(D12); |
| paulstuiver | 2:75b2f713161c | 12 | AnalogIn pot2(A0); |
| Floris_Hoek | 8:7dab565a208e | 13 | Ticker measurecontrol; |
| paulstuiver | 3:4f281c336a8b | 14 | DigitalOut motor1Direction(D7); |
| paulstuiver | 3:4f281c336a8b | 15 | FastPWM motor1Velocity(D6); |
| Floris_Hoek | 8:7dab565a208e | 16 | MODSERIAL pc(USBTX, USBRX); |
| paulstuiver | 5:2ae500da8fe1 | 17 | QEI Encoder(D8,D9,NC,8400); |
| Floris_Hoek | 8:7dab565a208e | 18 | |
| Floris_Hoek | 8:7dab565a208e | 19 | float PI = 3.1415926535897932384626433832795028841971693993; |
| paulstuiver | 5:2ae500da8fe1 | 20 | float timeinterval = 0.001; |
| paulstuiver | 5:2ae500da8fe1 | 21 | |
| paulstuiver | 5:2ae500da8fe1 | 22 | // README |
| paulstuiver | 5:2ae500da8fe1 | 23 | |
| paulstuiver | 5:2ae500da8fe1 | 24 | |
| paulstuiver | 5:2ae500da8fe1 | 25 | //P control implementation (behaves like a spring) |
| paulstuiver | 5:2ae500da8fe1 | 26 | double P_controller(double error) |
| paulstuiver | 5:2ae500da8fe1 | 27 | { |
| Floris_Hoek | 8:7dab565a208e | 28 | double Kp = 17.5; |
| paulstuiver | 5:2ae500da8fe1 | 29 | //Proportional part: |
| paulstuiver | 5:2ae500da8fe1 | 30 | double u_k = Kp * error; |
| paulstuiver | 5:2ae500da8fe1 | 31 | |
| paulstuiver | 5:2ae500da8fe1 | 32 | //sum all parts and return it |
| paulstuiver | 5:2ae500da8fe1 | 33 | return u_k; |
| paulstuiver | 5:2ae500da8fe1 | 34 | } |
| paulstuiver | 2:75b2f713161c | 35 | |
| Floris_Hoek | 8:7dab565a208e | 36 | void nothing(){// Do nothing |
| paulstuiver | 2:75b2f713161c | 37 | } |
| paulstuiver | 2:75b2f713161c | 38 | |
| RobertoO | 0:67c50348f842 | 39 | int main() |
| RobertoO | 0:67c50348f842 | 40 | { |
| Floris_Hoek | 8:7dab565a208e | 41 | double frequency = 17000.0; |
| Floris_Hoek | 8:7dab565a208e | 42 | double period_signal = 1.0/frequency; |
| RobertoO | 0:67c50348f842 | 43 | pc.baud(115200); |
| Floris_Hoek | 8:7dab565a208e | 44 | pc.printf("Starting...\r\n\r\n"); |
| paulstuiver | 2:75b2f713161c | 45 | motor1Velocity.period(period_signal); |
| Floris_Hoek | 8:7dab565a208e | 46 | measurecontrol.attach(measureandcontrol, timeinterval); |
| Floris_Hoek | 8:7dab565a208e | 47 | bool aa = true; |
| Floris_Hoek | 8:7dab565a208e | 48 | while(aa) |
| paulstuiver | 2:75b2f713161c | 49 | { |
| Floris_Hoek | 8:7dab565a208e | 50 | char c = pc.getcNb(); |
| Floris_Hoek | 8:7dab565a208e | 51 | aa = c == 'c' ? false : true; |
| RobertoO | 0:67c50348f842 | 52 | } |
| Floris_Hoek | 8:7dab565a208e | 53 | pc.printf("Programm stops running\r\n"); |
| Floris_Hoek | 8:7dab565a208e | 54 | sendtomotor(0); |
| Floris_Hoek | 8:7dab565a208e | 55 | measurecontrol.attach(nothing,10000); |
| Floris_Hoek | 8:7dab565a208e | 56 | return 0; |
| Floris_Hoek | 8:7dab565a208e | 57 | } |