Samenwerking Groep 12
Dependencies: Encoder MODSERIAL HIDScope mbed
Foo
main.cpp@16:8975d6c900f0, 2015-09-21 (annotated)
- Committer:
- ThomasBNL
- Date:
- Mon Sep 21 15:05:33 2015 +0000
- Revision:
- 16:8975d6c900f0
- Parent:
- 15:80af7a7671d4
- Child:
- 19:269f19917d80
bezig met P controller, errors weggewerkt (systeem functioneert nog niet naar wens)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ThomasBNL | 10:e923f51f18c4 | 1 | #include "mbed.h" |
ThomasBNL | 10:e923f51f18c4 | 2 | #include "HIDScope.h" |
ThomasBNL | 10:e923f51f18c4 | 3 | #include "encoder.h" |
ThomasBNL | 10:e923f51f18c4 | 4 | #include "MODSERIAL.h" |
ThomasBNL | 10:e923f51f18c4 | 5 | |
ThomasBNL | 10:e923f51f18c4 | 6 | //Motor 2 |
ThomasBNL | 10:e923f51f18c4 | 7 | DigitalOut motor2direction(D4); //D4 en D5 zijn motor 2 (op het motorshield) |
ThomasBNL | 10:e923f51f18c4 | 8 | PwmOut motor2speed(D5); |
ThomasBNL | 10:e923f51f18c4 | 9 | DigitalIn button(PTA4); |
ThomasBNL | 10:e923f51f18c4 | 10 | Encoder motor2(D13,D12); |
ThomasBNL | 10:e923f51f18c4 | 11 | MODSERIAL pc(USBTX,USBRX); |
ThomasBNL | 13:a0113cf2fc5f | 12 | Ticker myControllerTicker; |
ThomasBNL | 10:e923f51f18c4 | 13 | //AnalogIn potmeter2(A0); /NIEUW |
ThomasBNL | 13:a0113cf2fc5f | 14 | |
ThomasBNL | 13:a0113cf2fc5f | 15 | // constantes definieren |
ThomasBNL | 13:a0113cf2fc5f | 16 | double counts_per_revolution=4200; |
ThomasBNL | 13:a0113cf2fc5f | 17 | double degrees_per_turn=360; |
ThomasBNL | 13:a0113cf2fc5f | 18 | double counts_per_degree=counts_per_revolution/degrees_per_turn; //11.67 counts/degree |
ThomasBNL | 13:a0113cf2fc5f | 19 | const double motor2_Kp = 2.5; //constante motor |
ThomasBNL | 13:a0113cf2fc5f | 20 | |
ThomasBNL | 13:a0113cf2fc5f | 21 | //functies |
ThomasBNL | 13:a0113cf2fc5f | 22 | double P(double error, const double Kp) |
ThomasBNL | 13:a0113cf2fc5f | 23 | { |
ThomasBNL | 13:a0113cf2fc5f | 24 | return Kp*error; |
ThomasBNL | 13:a0113cf2fc5f | 25 | } |
ThomasBNL | 13:a0113cf2fc5f | 26 | |
ThomasBNL | 15:80af7a7671d4 | 27 | |
ThomasBNL | 13:a0113cf2fc5f | 28 | |
ThomasBNL | 10:e923f51f18c4 | 29 | int main() |
ThomasBNL | 10:e923f51f18c4 | 30 | { |
ThomasBNL | 10:e923f51f18c4 | 31 | pc.baud(9600); |
ThomasBNL | 10:e923f51f18c4 | 32 | motor2.setPosition(0); |
ThomasBNL | 10:e923f51f18c4 | 33 | while(true) { |
ThomasBNL | 10:e923f51f18c4 | 34 | if (button.read() < 0.5) { //if button pressed |
ThomasBNL | 13:a0113cf2fc5f | 35 | motor2direction = 0; // zero is clockwise (front view) |
ThomasBNL | 10:e923f51f18c4 | 36 | motor2speed = 0.5f; |
ThomasBNL | 10:e923f51f18c4 | 37 | pc.printf("positie = %d \r\n", motor2.getPosition()); |
ThomasBNL | 10:e923f51f18c4 | 38 | } else { // If button is not pressed |
ThomasBNL | 10:e923f51f18c4 | 39 | motor2direction = 0; |
ThomasBNL | 10:e923f51f18c4 | 40 | motor2speed = 0; |
ThomasBNL | 15:80af7a7671d4 | 41 | //while (motor2_Controller /// WHILE MOTOR > ... <... HIER BEZIG |
ThomasBNL | 15:80af7a7671d4 | 42 | double reference=400; |
ThomasBNL | 15:80af7a7671d4 | 43 | double position=motor2.getPosition(); |
ThomasBNL | 15:80af7a7671d4 | 44 | double Error_position=(reference-position)*motor2_Kp; |
ThomasBNL | 15:80af7a7671d4 | 45 | pc.printf("positie = %d en Error_positie =%d \r\n", motor2.getPosition(), Error_position); |
ThomasBNL | 13:a0113cf2fc5f | 46 | //myControllerTicker.attach(&motor2_Controller,0.01f); |
ThomasBNL | 16:8975d6c900f0 | 47 | while (Error_position > 0) { |
ThomasBNL | 15:80af7a7671d4 | 48 | motor2direction = 0; |
ThomasBNL | 15:80af7a7671d4 | 49 | motor2speed=0.3f;} |
ThomasBNL | 16:8975d6c900f0 | 50 | while (Error_position < 0) { |
ThomasBNL | 15:80af7a7671d4 | 51 | motor2direction = 1; |
ThomasBNL | 15:80af7a7671d4 | 52 | motor2speed=0.3f;} |
ThomasBNL | 10:e923f51f18c4 | 53 | } |
ThomasBNL | 12:af428d56b4eb | 54 | |
ThomasBNL | 13:a0113cf2fc5f | 55 | while ((motor2.getPosition()>counts_per_revolution) || (motor2.getPosition()<-counts_per_revolution)) // If value is outside -4200 and 4200 (number of counts equal to one revolution) reset to zero |
ThomasBNL | 12:af428d56b4eb | 56 | { |
ThomasBNL | 10:e923f51f18c4 | 57 | motor2.setPosition(0); |
ThomasBNL | 13:a0113cf2fc5f | 58 | pc.printf(" HE \r\n LLO \r\n WO \r\n RLD \r\n !!! \r\n FOO! \r\n"); |
ThomasBNL | 10:e923f51f18c4 | 59 | break; |
ThomasBNL | 12:af428d56b4eb | 60 | } |
ThomasBNL | 10:e923f51f18c4 | 61 | } |
ThomasBNL | 12:af428d56b4eb | 62 | } |