Samenwerking Groep 12
Dependencies: Encoder MODSERIAL HIDScope mbed
Foo
main.cpp@13:a0113cf2fc5f, 2015-09-21 (annotated)
- Committer:
- ThomasBNL
- Date:
- Mon Sep 21 14:32:58 2015 +0000
- Revision:
- 13:a0113cf2fc5f
- Parent:
- 12:af428d56b4eb
- Child:
- 15:80af7a7671d4
Bezig implementeren van P systeem
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 | 13:a0113cf2fc5f | 27 | void motor2_Controller(){ |
ThomasBNL | 13:a0113cf2fc5f | 28 | double reference=400; |
ThomasBNL | 13:a0113cf2fc5f | 29 | double position=motor2.getPosition(); |
ThomasBNL | 13:a0113cf2fc5f | 30 | double Error_position=P(reference-position,motor2_Kp); |
ThomasBNL | 13:a0113cf2fc5f | 31 | } |
ThomasBNL | 13:a0113cf2fc5f | 32 | |
ThomasBNL | 10:e923f51f18c4 | 33 | int main() |
ThomasBNL | 10:e923f51f18c4 | 34 | { |
ThomasBNL | 10:e923f51f18c4 | 35 | pc.baud(9600); |
ThomasBNL | 10:e923f51f18c4 | 36 | motor2.setPosition(0); |
ThomasBNL | 10:e923f51f18c4 | 37 | while(true) { |
ThomasBNL | 10:e923f51f18c4 | 38 | if (button.read() < 0.5) { //if button pressed |
ThomasBNL | 13:a0113cf2fc5f | 39 | motor2direction = 0; // zero is clockwise (front view) |
ThomasBNL | 10:e923f51f18c4 | 40 | motor2speed = 0.5f; |
ThomasBNL | 10:e923f51f18c4 | 41 | pc.printf("positie = %d \r\n", motor2.getPosition()); |
ThomasBNL | 10:e923f51f18c4 | 42 | } else { // If button is not pressed |
ThomasBNL | 10:e923f51f18c4 | 43 | motor2direction = 0; |
ThomasBNL | 10:e923f51f18c4 | 44 | motor2speed = 0; |
ThomasBNL | 13:a0113cf2fc5f | 45 | while (motor2_Controller /// WHILE MOTOR > ... <... HIER BEZIG |
ThomasBNL | 13:a0113cf2fc5f | 46 | pc.printf("positie = %d en Error_positie \r\n", motor2.getPosition(), Error_position); |
ThomasBNL | 13:a0113cf2fc5f | 47 | //myControllerTicker.attach(&motor2_Controller,0.01f); |
ThomasBNL | 13:a0113cf2fc5f | 48 | //while(1){} |
ThomasBNL | 10:e923f51f18c4 | 49 | } |
ThomasBNL | 12:af428d56b4eb | 50 | |
ThomasBNL | 13:a0113cf2fc5f | 51 | 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 | 52 | { |
ThomasBNL | 10:e923f51f18c4 | 53 | motor2.setPosition(0); |
ThomasBNL | 13:a0113cf2fc5f | 54 | pc.printf(" HE \r\n LLO \r\n WO \r\n RLD \r\n !!! \r\n FOO! \r\n"); |
ThomasBNL | 10:e923f51f18c4 | 55 | break; |
ThomasBNL | 12:af428d56b4eb | 56 | } |
ThomasBNL | 10:e923f51f18c4 | 57 | } |
ThomasBNL | 12:af428d56b4eb | 58 | } |