Jona Vonk
/
MoveMotors
Hier zitten extra leipe functies in naast PID ook x en y naar hoeken
main.cpp@1:363c5230fe25, 2019-10-04 (annotated)
- Committer:
- samzijp
- Date:
- Fri Oct 04 09:26:44 2019 +0000
- Revision:
- 1:363c5230fe25
- Parent:
- 0:6986a58c4515
- Child:
- 2:f832050b1b4a
Motor control with buttons;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JonaVonk | 0:6986a58c4515 | 1 | /* mbed Microcontroller Library |
JonaVonk | 0:6986a58c4515 | 2 | * Copyright (c) 2018 ARM Limited |
JonaVonk | 0:6986a58c4515 | 3 | * SPDX-License-Identifier: Apache-2.0 |
JonaVonk | 0:6986a58c4515 | 4 | */ |
JonaVonk | 0:6986a58c4515 | 5 | |
JonaVonk | 0:6986a58c4515 | 6 | #include "mbed.h" |
JonaVonk | 0:6986a58c4515 | 7 | #include "MODSERIAL.h" |
JonaVonk | 0:6986a58c4515 | 8 | |
JonaVonk | 0:6986a58c4515 | 9 | |
samzijp | 1:363c5230fe25 | 10 | DigitalOut M1(D4); //Direction control |
samzijp | 1:363c5230fe25 | 11 | DigitalOut M2(D7); //Direction control |
samzijp | 1:363c5230fe25 | 12 | PwmOut E1(D5); //Speed control |
samzijp | 1:363c5230fe25 | 13 | PwmOut E2(D6); //Speed control |
samzijp | 1:363c5230fe25 | 14 | InterruptIn Btn1(A0); //Potentionmeter |
samzijp | 1:363c5230fe25 | 15 | InterruptIn Btn2(A1); //Potentionmeter |
samzijp | 1:363c5230fe25 | 16 | DigitalIn M1A(D2); //Encoder |
samzijp | 1:363c5230fe25 | 17 | DigitalIn M1B(D3); //Encoder |
JonaVonk | 0:6986a58c4515 | 18 | |
samzijp | 1:363c5230fe25 | 19 | //float potVal1; |
samzijp | 1:363c5230fe25 | 20 | //float potVal2; |
JonaVonk | 0:6986a58c4515 | 21 | int EncA1; |
JonaVonk | 0:6986a58c4515 | 22 | int EncB1; |
JonaVonk | 0:6986a58c4515 | 23 | int counts = 0; |
samzijp | 1:363c5230fe25 | 24 | int speedCount = 0; |
JonaVonk | 0:6986a58c4515 | 25 | |
JonaVonk | 0:6986a58c4515 | 26 | //Ticker readEnc |
JonaVonk | 0:6986a58c4515 | 27 | |
JonaVonk | 0:6986a58c4515 | 28 | MODSERIAL pc(USBTX, USBRX); |
JonaVonk | 0:6986a58c4515 | 29 | |
samzijp | 1:363c5230fe25 | 30 | void speedUp(){ |
samzijp | 1:363c5230fe25 | 31 | speedCount++; |
samzijp | 1:363c5230fe25 | 32 | } |
samzijp | 1:363c5230fe25 | 33 | void speedDown(){ |
samzijp | 1:363c5230fe25 | 34 | speedCount--; |
samzijp | 1:363c5230fe25 | 35 | } |
JonaVonk | 0:6986a58c4515 | 36 | |
JonaVonk | 0:6986a58c4515 | 37 | |
JonaVonk | 0:6986a58c4515 | 38 | // main() runs in its own thread in the OS |
JonaVonk | 0:6986a58c4515 | 39 | int main(){ |
JonaVonk | 0:6986a58c4515 | 40 | pc.baud(9600); |
samzijp | 1:363c5230fe25 | 41 | Btn1.rise(&speedUp); |
samzijp | 1:363c5230fe25 | 42 | Btn2.rise(&speedDown); |
JonaVonk | 0:6986a58c4515 | 43 | while(true){ |
samzijp | 1:363c5230fe25 | 44 | if(speedCount > 0){ |
samzijp | 1:363c5230fe25 | 45 | M1 = 1; |
samzijp | 1:363c5230fe25 | 46 | E1 = speedCount/10; |
samzijp | 1:363c5230fe25 | 47 | } |
samzijp | 1:363c5230fe25 | 48 | else{ |
samzijp | 1:363c5230fe25 | 49 | M1 = 0; |
samzijp | 1:363c5230fe25 | 50 | E1 = -speedCount/10; |
samzijp | 1:363c5230fe25 | 51 | } |
samzijp | 1:363c5230fe25 | 52 | /*potVal1 = Pot1.read(); |
JonaVonk | 0:6986a58c4515 | 53 | potVal2 = Pot2.read(); |
JonaVonk | 0:6986a58c4515 | 54 | if (potVal1 > 0.5){ |
JonaVonk | 0:6986a58c4515 | 55 | M1 = 1; |
JonaVonk | 0:6986a58c4515 | 56 | E1 = potVal1-0.5; |
JonaVonk | 0:6986a58c4515 | 57 | }else{ |
JonaVonk | 0:6986a58c4515 | 58 | M1 = 0; |
JonaVonk | 0:6986a58c4515 | 59 | E1 = -(potVal1-0.5); |
JonaVonk | 0:6986a58c4515 | 60 | } |
JonaVonk | 0:6986a58c4515 | 61 | if (potVal2 > 0.5){ |
JonaVonk | 0:6986a58c4515 | 62 | M2 = 1; |
JonaVonk | 0:6986a58c4515 | 63 | E2 = potVal2-0.5; |
JonaVonk | 0:6986a58c4515 | 64 | }else{ |
JonaVonk | 0:6986a58c4515 | 65 | M2 = 0; |
JonaVonk | 0:6986a58c4515 | 66 | E2 = -(potVal2-0.5); |
JonaVonk | 0:6986a58c4515 | 67 | } |
samzijp | 1:363c5230fe25 | 68 | pc.printf("Pot1: %f \t Pot2: %f \n\r", potVal1, potVal2);*/ |
JonaVonk | 0:6986a58c4515 | 69 | } |
JonaVonk | 0:6986a58c4515 | 70 | } |