Homing system
homingsystem.cpp@0:c49ff6a6ebee, 2017-10-31 (annotated)
- Committer:
- maaikelaagland
- Date:
- Tue Oct 31 16:16:34 2017 +0000
- Revision:
- 0:c49ff6a6ebee
homing system ;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
maaikelaagland | 0:c49ff6a6ebee | 1 | #include "QEI.h" |
maaikelaagland | 0:c49ff6a6ebee | 2 | #include "Servo.h" |
maaikelaagland | 0:c49ff6a6ebee | 3 | #include "mbed.h" |
maaikelaagland | 0:c49ff6a6ebee | 4 | |
maaikelaagland | 0:c49ff6a6ebee | 5 | Serial pc(USBTX, USBRX); |
maaikelaagland | 0:c49ff6a6ebee | 6 | //Use X4 encoding. |
maaikelaagland | 0:c49ff6a6ebee | 7 | //QEI wheel(p29, p30, NC, 624, QEI::X4_ENCODING); |
maaikelaagland | 0:c49ff6a6ebee | 8 | //Use X2 encoding by default. |
maaikelaagland | 0:c49ff6a6ebee | 9 | QEI wheel1(D10, D11, NC, 32); |
maaikelaagland | 0:c49ff6a6ebee | 10 | QEI wheel2(D13, D12, NC, 32); //enable the encoder |
maaikelaagland | 0:c49ff6a6ebee | 11 | PwmOut motor1_speed(D6); |
maaikelaagland | 0:c49ff6a6ebee | 12 | PwmOut motor2_speed(D5); |
maaikelaagland | 0:c49ff6a6ebee | 13 | DigitalOut motor1_direction (D7); |
maaikelaagland | 0:c49ff6a6ebee | 14 | DigitalOut motor2_direction(D4); |
maaikelaagland | 0:c49ff6a6ebee | 15 | AnalogIn potmeter(A0); |
maaikelaagland | 0:c49ff6a6ebee | 16 | AnalogIn potmeter2(A1); |
maaikelaagland | 0:c49ff6a6ebee | 17 | InterruptIn button(D8); |
maaikelaagland | 0:c49ff6a6ebee | 18 | DigitalIn button2(D2); |
maaikelaagland | 0:c49ff6a6ebee | 19 | DigitalIn button3(D3); //deze |
maaikelaagland | 0:c49ff6a6ebee | 20 | DigitalOut led(D3); |
maaikelaagland | 0:c49ff6a6ebee | 21 | Ticker motor_update; |
maaikelaagland | 0:c49ff6a6ebee | 22 | |
maaikelaagland | 0:c49ff6a6ebee | 23 | |
maaikelaagland | 0:c49ff6a6ebee | 24 | |
maaikelaagland | 0:c49ff6a6ebee | 25 | float motor1_set_speed= 0; //dit is x procent van het volledige snelheid |
maaikelaagland | 0:c49ff6a6ebee | 26 | float motor2_set_speed= 0; |
maaikelaagland | 0:c49ff6a6ebee | 27 | float position; |
maaikelaagland | 0:c49ff6a6ebee | 28 | |
maaikelaagland | 0:c49ff6a6ebee | 29 | void motor_control(){ |
maaikelaagland | 0:c49ff6a6ebee | 30 | motor1_speed.write(motor1_set_speed); |
maaikelaagland | 0:c49ff6a6ebee | 31 | motor2_speed.write(motor2_set_speed); |
maaikelaagland | 0:c49ff6a6ebee | 32 | motor1_direction.write(1); //hij gaat één kant op |
maaikelaagland | 0:c49ff6a6ebee | 33 | motor2_direction.write(1); |
maaikelaagland | 0:c49ff6a6ebee | 34 | |
maaikelaagland | 0:c49ff6a6ebee | 35 | } |
maaikelaagland | 0:c49ff6a6ebee | 36 | |
maaikelaagland | 0:c49ff6a6ebee | 37 | void homing_system () { |
maaikelaagland | 0:c49ff6a6ebee | 38 | pc.baud(115200); |
maaikelaagland | 0:c49ff6a6ebee | 39 | motor_update.attach(motor_control,0.1); |
maaikelaagland | 0:c49ff6a6ebee | 40 | button2.mode(PullDown); |
maaikelaagland | 0:c49ff6a6ebee | 41 | button3.mode(PullDown); |
maaikelaagland | 0:c49ff6a6ebee | 42 | while (true){ |
maaikelaagland | 0:c49ff6a6ebee | 43 | |
maaikelaagland | 0:c49ff6a6ebee | 44 | if (button == 0){ |
maaikelaagland | 0:c49ff6a6ebee | 45 | motor1_set_speed = 0.15; |
maaikelaagland | 0:c49ff6a6ebee | 46 | } |
maaikelaagland | 0:c49ff6a6ebee | 47 | |
maaikelaagland | 0:c49ff6a6ebee | 48 | if (button2 == 1){ |
maaikelaagland | 0:c49ff6a6ebee | 49 | motor1_set_speed = 0; |
maaikelaagland | 0:c49ff6a6ebee | 50 | motor2_set_speed=0.15; |
maaikelaagland | 0:c49ff6a6ebee | 51 | } |
maaikelaagland | 0:c49ff6a6ebee | 52 | if (button3 == 1) { |
maaikelaagland | 0:c49ff6a6ebee | 53 | |
maaikelaagland | 0:c49ff6a6ebee | 54 | motor2_set_speed = 0; |
maaikelaagland | 0:c49ff6a6ebee | 55 | float position = 0; |
maaikelaagland | 0:c49ff6a6ebee | 56 | } |
maaikelaagland | 0:c49ff6a6ebee | 57 | |
maaikelaagland | 0:c49ff6a6ebee | 58 | if (button2 == 1 && button3 ==1) { |
maaikelaagland | 0:c49ff6a6ebee | 59 | break; |
maaikelaagland | 0:c49ff6a6ebee | 60 | } |
maaikelaagland | 0:c49ff6a6ebee | 61 | } |
maaikelaagland | 0:c49ff6a6ebee | 62 | } |
maaikelaagland | 0:c49ff6a6ebee | 63 | |
maaikelaagland | 0:c49ff6a6ebee | 64 | |
maaikelaagland | 0:c49ff6a6ebee | 65 | |
maaikelaagland | 0:c49ff6a6ebee | 66 | int main(){ |
maaikelaagland | 0:c49ff6a6ebee | 67 | homing_system (); |
maaikelaagland | 0:c49ff6a6ebee | 68 | return 0; |
maaikelaagland | 0:c49ff6a6ebee | 69 | } |