met comments 710

Dependencies:   mbed QEI HIDScope biquadFilter MODSERIAL FastPWM demomode

Committer:
Mortimerz
Date:
Fri Oct 04 09:38:34 2019 +0000
Revision:
23:cd0a4eb31a90
Parent:
22:8340aa6676d7
Child:
24:0b5b7ec374e6
beide motoren kunnen draaien en andere kant op;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RobertoO 0:67c50348f842 1 #include "mbed.h"
boydmartherus 16:0ff43ed4d259 2 #include "MODSERIAL.h"
boydmartherus 17:3751c2c685ca 3 #include "FastPWM.h"
boydmartherus 17:3751c2c685ca 4 #include "QEI.h"
boydmartherus 7:d5e07647dfea 5
boydmartherus 7:d5e07647dfea 6 Serial pc(USBTX,USBRX);
boydmartherus 7:d5e07647dfea 7
boydmartherus 17:3751c2c685ca 8 // functies aan PINs toevoegen
Mortimerz 19:dd9a61699449 9 DigitalOut led1(D2);
boydmartherus 9:cdcf30051100 10 FastPWM led(PTA1);
Mortimerz 23:cd0a4eb31a90 11 FastPWM motor1(D6);
Mortimerz 23:cd0a4eb31a90 12 DigitalOut motor1_dir(D7);
Mortimerz 23:cd0a4eb31a90 13 FastPWM motor2(D5);
Mortimerz 23:cd0a4eb31a90 14 DigitalOut motor2_dir(D4);
Mortimerz 23:cd0a4eb31a90 15 AnalogIn pot1(PTB2);
Mortimerz 23:cd0a4eb31a90 16 AnalogIn pot2(PTB3);
Mortimerz 23:cd0a4eb31a90 17 InterruptIn button1(PTB10);
Mortimerz 23:cd0a4eb31a90 18 InterruptIn button2(PTB11);
boydmartherus 9:cdcf30051100 19
boydmartherus 17:3751c2c685ca 20 // timer opzetten = ticker
Mortimerz 19:dd9a61699449 21 Ticker ticker;
boydmartherus 17:3751c2c685ca 22
boydmartherus 17:3751c2c685ca 23 // opzetten van hoekbepaling
Mortimerz 19:dd9a61699449 24 QEI encoder(D13,D12,NC,32,QEI::X4_ENCODING);
Mortimerz 19:dd9a61699449 25 QEI encoder_2(D11,D10,NC,32,QEI::X4_ENCODING);
MarliesWeggemans 4:3e9a8f5d9a1a 26
Mortimerz 23:cd0a4eb31a90 27 int motordir1 = 1;
Mortimerz 23:cd0a4eb31a90 28 int motordir2 = 1;
boydmartherus 17:3751c2c685ca 29 // aparte functies
Mortimerz 22:8340aa6676d7 30 //functie motor aansturen met pot
Mortimerz 23:cd0a4eb31a90 31 void motor_direction1()
boydmartherus 17:3751c2c685ca 32 {
Mortimerz 22:8340aa6676d7 33
Mortimerz 23:cd0a4eb31a90 34 motordir1 = !motordir1;
Mortimerz 22:8340aa6676d7 35 led1 = !led1;
Mortimerz 22:8340aa6676d7 36 wait_ms(0.1f);
Mortimerz 22:8340aa6676d7 37 led1 = !led1;
Mortimerz 22:8340aa6676d7 38 wait_ms(0.1f);
Mortimerz 23:cd0a4eb31a90 39 motor1_dir.write(motordir1);
Mortimerz 23:cd0a4eb31a90 40 }
Mortimerz 23:cd0a4eb31a90 41 void motor_direction2()
Mortimerz 23:cd0a4eb31a90 42 {
Mortimerz 23:cd0a4eb31a90 43
Mortimerz 23:cd0a4eb31a90 44 motordir2 = !motordir2;
Mortimerz 23:cd0a4eb31a90 45 led1 = !led1;
Mortimerz 23:cd0a4eb31a90 46 wait_ms(0.1f);
Mortimerz 23:cd0a4eb31a90 47 led1 = !led1;
Mortimerz 23:cd0a4eb31a90 48 wait_ms(0.1f);
Mortimerz 23:cd0a4eb31a90 49 motor2_dir.write(motordir2);
boydmartherus 17:3751c2c685ca 50 }
boydmartherus 17:3751c2c685ca 51
Mortimerz 19:dd9a61699449 52 void motor_speed()
boydmartherus 17:3751c2c685ca 53 {
Mortimerz 23:cd0a4eb31a90 54 motor1.write(pot1);
Mortimerz 23:cd0a4eb31a90 55 motor2.write(pot2);
boydmartherus 17:3751c2c685ca 56 }
Mortimerz 22:8340aa6676d7 57
boydmartherus 17:3751c2c685ca 58
boydmartherus 17:3751c2c685ca 59 void main_loop()
Mortimerz 19:dd9a61699449 60 {
Mortimerz 19:dd9a61699449 61 motor_speed();
Mortimerz 23:cd0a4eb31a90 62 if (button1.read() == 0){
Mortimerz 23:cd0a4eb31a90 63 motor_direction1();
Mortimerz 22:8340aa6676d7 64 }
Mortimerz 23:cd0a4eb31a90 65 if (button2.read() == 0){
Mortimerz 23:cd0a4eb31a90 66 motor_direction2();
Mortimerz 23:cd0a4eb31a90 67 }
Mortimerz 19:dd9a61699449 68
boydmartherus 17:3751c2c685ca 69 }
boydmartherus 18:3de4412d8b63 70
boydmartherus 10:9101c7a4f219 71 int main()
boydmartherus 10:9101c7a4f219 72 {
Mortimerz 23:cd0a4eb31a90 73 motor1_dir.write(motordir1);
Mortimerz 23:cd0a4eb31a90 74 motor2_dir.write(motordir2);
Mortimerz 19:dd9a61699449 75 ticker.attach(&main_loop, 0.1f);
Mortimerz 19:dd9a61699449 76 while(true){
Mortimerz 23:cd0a4eb31a90 77 pc.printf("pot1(%f),enc1 is (%d),pot2(%f),enc2 is (%d)\r\n",pot1.read(),encoder.getPulses(),pot2.read(),encoder_2.getPulses());
Mortimerz 19:dd9a61699449 78 wait(0.1f);
Mortimerz 19:dd9a61699449 79 };
boydmartherus 10:9101c7a4f219 80 }