motor voor het eerst aan de praat. Met dit scriptje moet motor 2 links om draaien rechts om draaien en sneller en langzamer draaien met behulp van de 2e potmeter.
Dependencies: HIDScope MODSERIAL mbed
main.cpp@5:6fac8f640b50, 2017-09-25 (annotated)
- Committer:
- Miriam
- Date:
- Mon Sep 25 15:24:06 2017 +0000
- Revision:
- 5:6fac8f640b50
- Parent:
- 4:4afa30c72c57
motor2 draaien, harder zachter, klok mee klok tegen
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Miriam | 0:7af8b67c64f8 | 1 | #include "mbed.h" |
Miriam | 0:7af8b67c64f8 | 2 | #include "HIDScope.h" |
Miriam | 0:7af8b67c64f8 | 3 | |
Miriam | 0:7af8b67c64f8 | 4 | HIDScope scope(2); // We're going to send 2 chennels of data |
Miriam | 0:7af8b67c64f8 | 5 | Ticker AInTicker; |
Miriam | 4:4afa30c72c57 | 6 | //AnalogIn aIn1(A0); |
Miriam | 4:4afa30c72c57 | 7 | |
Miriam | 5:6fac8f640b50 | 8 | AnalogIn potmeter2(A1); //Analog input of the potmeter |
Miriam | 5:6fac8f640b50 | 9 | AnalogIn but2(A2); |
Miriam | 5:6fac8f640b50 | 10 | PwmOut E1(D6); //Biorobotics Motor 1 PWM control speed |
Miriam | 5:6fac8f640b50 | 11 | PwmOut E2(D5); //Biorobotics Motor 2 PWM control speed |
Miriam | 5:6fac8f640b50 | 12 | DigitalOut M2(D4); //Biorobotics Motor 2 direction control (bool) |
Miriam | 5:6fac8f640b50 | 13 | DigitalOut M1(D7); //Biorobotics Motor 1 direction control (bool) |
Miriam | 4:4afa30c72c57 | 14 | |
Miriam | 4:4afa30c72c57 | 15 | float PwmPeriod = 1.0/5000.0; //PWM period (5000 Hz) |
Miriam | 0:7af8b67c64f8 | 16 | |
Miriam | 0:7af8b67c64f8 | 17 | volatile float x; |
Miriam | 0:7af8b67c64f8 | 18 | volatile float x_prev=0; |
Miriam | 0:7af8b67c64f8 | 19 | volatile float y; //filtered 'output' of ReadAnalogInAndFilter |
Miriam | 0:7af8b67c64f8 | 20 | |
Miriam | 0:7af8b67c64f8 | 21 | void ReadAnalogInAndFilter() |
Miriam | 0:7af8b67c64f8 | 22 | { |
Miriam | 5:6fac8f640b50 | 23 | x=potmeter2; // Capture data |
Miriam | 0:7af8b67c64f8 | 24 | scope.set(0,x); //store data in first element of scope memory |
Miriam | 0:7af8b67c64f8 | 25 | y=(x_prev +x)/2.0; //averaging filter |
Miriam | 0:7af8b67c64f8 | 26 | scope.set(1,y); // stor data in second element of scope memory |
Miriam | 0:7af8b67c64f8 | 27 | x_prev=x; //Prepare for next round |
Miriam | 0:7af8b67c64f8 | 28 | |
Miriam | 0:7af8b67c64f8 | 29 | scope.send(); // send what's in the scope memory to PC |
Miriam | 0:7af8b67c64f8 | 30 | |
Miriam | 0:7af8b67c64f8 | 31 | } |
Miriam | 0:7af8b67c64f8 | 32 | |
Miriam | 0:7af8b67c64f8 | 33 | int main () |
Miriam | 0:7af8b67c64f8 | 34 | { |
Miriam | 0:7af8b67c64f8 | 35 | AInTicker.attach(&ReadAnalogInAndFilter, 0.01); |
Miriam | 4:4afa30c72c57 | 36 | |
Miriam | 5:6fac8f640b50 | 37 | E2.period(PwmPeriod); //Set PWM period at 5000 Hz |
Miriam | 5:6fac8f640b50 | 38 | M2 = false; // true is met de klok mee evenals false? |
Miriam | 2:30f6f422c00d | 39 | |
Miriam | 5:6fac8f640b50 | 40 | while(true) |
Miriam | 5:6fac8f640b50 | 41 | { |
Miriam | 5:6fac8f640b50 | 42 | M2 = !M2; |
Miriam | 5:6fac8f640b50 | 43 | E2 = potmeter2.read(); //Set brightness of let (0,1) |
Miriam | 5:6fac8f640b50 | 44 | wait(5.0f); |
Miriam | 2:30f6f422c00d | 45 | } |
Miriam | 0:7af8b67c64f8 | 46 | } |