Samenwerking Groep 12
Dependencies: Encoder MODSERIAL HIDScope mbed
Foo
Diff: main.cpp
- Revision:
- 19:269f19917d80
- Parent:
- 16:8975d6c900f0
- Child:
- 20:4d128c3f1228
diff -r 8975d6c900f0 -r 269f19917d80 main.cpp --- a/main.cpp Mon Sep 21 15:05:33 2015 +0000 +++ b/main.cpp Tue Sep 22 12:39:06 2015 +0000 @@ -5,52 +5,57 @@ //Motor 2 DigitalOut motor2direction(D4); //D4 en D5 zijn motor 2 (op het motorshield) -PwmOut motor2speed(D5); -DigitalIn button(PTA4); -Encoder motor2(D13,D12); -MODSERIAL pc(USBTX,USBRX); -Ticker myControllerTicker; +PwmOut motor2speed(D5); // D5 is an input on the motor that controls the speed of motor number 2 +DigitalIn button(PTA4); // PTA4 is used as a button controller (0 when pressed and 1 when not pressed) +Encoder motor2(D13,D12); // The encoder is able to read and set position/speed values +MODSERIAL pc(USBTX,USBRX); // This input is used to send data to the pc +Ticker TickerController; // This adds a Ticker to the function +TickerController //AnalogIn potmeter2(A0); /NIEUW -// constantes definieren -double counts_per_revolution=4200; +// Defining constants +double counts_per_revolution=4200; double degrees_per_turn=360; double counts_per_degree=counts_per_revolution/degrees_per_turn; //11.67 counts/degree -const double motor2_Kp = 2.5; //constante motor +const double motor2_Kp = 2.5; //controller gain which will be multiplied with the error (*how fast will the error be corrected) -//functies -double P(double error, const double Kp) +// Function P-controller +double P(double error, const double Kp) //returns error * controller gain { - return Kp*error; + return Kp*error; } +// Function that calls the P-controller +void motor2_controller() // Void function that compares the current position with a reference value and outputs + { + double reference=400; + double position=motor2.getPosition(); //current motor position + double motor2_p_output=P((reference-position)*motor2_Kp); // reference-position=> current -> this is multiplied with the controller gain motor2_Kp + - - +// MAIN int main() { - pc.baud(9600); - motor2.setPosition(0); + pc.baud(9600); // baud rate at which the information is processed to the computer + motor2.setPosition(0); // calls the current position of the motor zero while(true) { if (button.read() < 0.5) { //if button pressed motor2direction = 0; // zero is clockwise (front view) - motor2speed = 0.5f; + motor2speed = 0.5f; // motorspeed pc.printf("positie = %d \r\n", motor2.getPosition()); } else { // If button is not pressed motor2direction = 0; motor2speed = 0; //while (motor2_Controller /// WHILE MOTOR > ... <... HIER BEZIG - double reference=400; - double position=motor2.getPosition(); - double Error_position=(reference-position)*motor2_Kp; + pc.printf("positie = %d en Error_positie =%d \r\n", motor2.getPosition(), Error_position); //myControllerTicker.attach(&motor2_Controller,0.01f); - while (Error_position > 0) { - motor2direction = 0; - motor2speed=0.3f;} - while (Error_position < 0) { - motor2direction = 1; - motor2speed=0.3f;} +// while (Error_position > 0) { +// motor2direction = 0; +// motor2speed=0.3f;} +// while (Error_position < 0) { +// motor2direction = 1; +// motor2speed=0.3f;} } + while(1) {} 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 {