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: Servoaansturing mbed QEI HIDScope biquadFilter MODSERIAL FastPWM
main.cpp@35:113a4015d408, 2019-11-04 (annotated)
- Committer:
- Renate
- Date:
- Mon Nov 04 09:59:52 2019 +0000
- Revision:
- 35:113a4015d408
- Parent:
- 32:d651c23bbb77
Script voor encoders
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RobertoO | 0:67c50348f842 | 1 | #include "mbed.h" |
Rosalie | 3:6ee0b20c23b0 | 2 | #include "HIDScope.h" |
Rosalie | 3:6ee0b20c23b0 | 3 | #include "QEI.h" |
RobertoO | 1:b862262a9d14 | 4 | #include "MODSERIAL.h" |
Rosalie | 3:6ee0b20c23b0 | 5 | #include "BiQuad.h" |
Rosalie | 3:6ee0b20c23b0 | 6 | #include "FastPWM.h" |
Renate | 32:d651c23bbb77 | 7 | #define M_PI 3.14159265358979323846 /* pi */ |
WiesjeRoskamp | 2:aee655d11b6d | 8 | #include <math.h> |
Rosalie | 5:9f1260408ef2 | 9 | #include "Servo.h" |
Renate | 21:456acc79726c | 10 | #include <cmath> |
Renate | 29:8e0a7c33e4e7 | 11 | #include <complex> |
RobertoO | 0:67c50348f842 | 12 | |
WiesjeRoskamp | 2:aee655d11b6d | 13 | Serial pc(USBTX, USBRX); |
Rosalie | 3:6ee0b20c23b0 | 14 | |
Renate | 28:7c7508bdb21f | 15 | PwmOut motor1(D6); // Misschien moeten we hiervoor DigitalOut gebruiken, moet |
Renate | 23:4572750a5c59 | 16 | PwmOut motor2(D5); // samen kunnen gaan met de servo motor |
Renate | 21:456acc79726c | 17 | |
Renate | 23:4572750a5c59 | 18 | DigitalOut motor1_dir(D7); |
Renate | 23:4572750a5c59 | 19 | DigitalOut motor2_dir(D4); |
Renate | 23:4572750a5c59 | 20 | |
Renate | 35:113a4015d408 | 21 | QEI Encoder1(D13, D12, NC, 64); |
Renate | 35:113a4015d408 | 22 | QEI Encoder2(D10, D9, NC, 64); |
Renate | 35:113a4015d408 | 23 | |
Renate | 23:4572750a5c59 | 24 | // VARIABELEN VOOR ENCODER, MOTORHOEK ETC. |
Renate | 35:113a4015d408 | 25 | double pulses_M1; |
Renate | 35:113a4015d408 | 26 | double pulses_M2; |
Renate | 35:113a4015d408 | 27 | double counts1; |
Renate | 35:113a4015d408 | 28 | double counts2; |
Renate | 32:d651c23bbb77 | 29 | const double conversion_factor = (2.0*M_PI)/(64.0*131.25*2.0); |
Renate | 21:456acc79726c | 30 | double theta_h_1_rad; |
Renate | 21:456acc79726c | 31 | double theta_h_2_rad; |
Renate | 35:113a4015d408 | 32 | double offset1 = 0.0; |
Renate | 35:113a4015d408 | 33 | double offset2 = 0.0; |
Renate | 23:4572750a5c59 | 34 | |
Renate | 35:113a4015d408 | 35 | HIDScope scope(1); |
Renate | 30:0a328a9a4788 | 36 | |
Renate | 35:113a4015d408 | 37 | Ticker loop; |
Renate | 31:967b455bc328 | 38 | |
Renate | 35:113a4015d408 | 39 | void motors() |
Renate | 31:967b455bc328 | 40 | { |
Renate | 35:113a4015d408 | 41 | motor1.write(0.6); |
Renate | 35:113a4015d408 | 42 | motor1_dir.write(1); |
Renate | 35:113a4015d408 | 43 | motor2.write(0.6); |
Renate | 35:113a4015d408 | 44 | motor2_dir.write(1); |
Renate | 35:113a4015d408 | 45 | |
Renate | 23:4572750a5c59 | 46 | // Berekenen van de motorhoeken (in radialen) |
Renate | 32:d651c23bbb77 | 47 | pulses_M1 = Encoder1.getPulses(); |
Renate | 32:d651c23bbb77 | 48 | counts1 = pulses_M1*2; |
Renate | 35:113a4015d408 | 49 | theta_h_1_rad = conversion_factor*(counts1); |
Renate | 35:113a4015d408 | 50 | |
Renate | 35:113a4015d408 | 51 | scope.set(0, theta_h_1_rad); |
Renate | 23:4572750a5c59 | 52 | scope.send(); |
Renate | 11:4bc0304978e2 | 53 | } |
WiesjeRoskamp | 2:aee655d11b6d | 54 | |
Renate | 8:c7d3b67346db | 55 | int main(void) |
Renate | 28:7c7508bdb21f | 56 | { |
Renate | 28:7c7508bdb21f | 57 | pc.printf("Opstarten\r\n"); |
Renate | 35:113a4015d408 | 58 | motor1.period_us(56); |
Renate | 35:113a4015d408 | 59 | motor2.period_us(56); |
Renate | 35:113a4015d408 | 60 | loop.attach(&motors, 0.002f); |
Renate | 35:113a4015d408 | 61 | while(true) |
Renate | 35:113a4015d408 | 62 | { |
Renate | 28:7c7508bdb21f | 63 | } |
Renate | 28:7c7508bdb21f | 64 | } |