Samenwerking Groep 12

Dependencies:   Encoder MODSERIAL HIDScope mbed

Foo

Committer:
ThomasBNL
Date:
Tue Sep 22 12:39:06 2015 +0000
Revision:
19:269f19917d80
Parent:
16:8975d6c900f0
Child:
20:4d128c3f1228
Comments added to the script

Who changed what in which revision?

UserRevisionLine numberNew 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 19:269f19917d80 8 PwmOut motor2speed(D5); // D5 is an input on the motor that controls the speed of motor number 2
ThomasBNL 19:269f19917d80 9 DigitalIn button(PTA4); // PTA4 is used as a button controller (0 when pressed and 1 when not pressed)
ThomasBNL 19:269f19917d80 10 Encoder motor2(D13,D12); // The encoder is able to read and set position/speed values
ThomasBNL 19:269f19917d80 11 MODSERIAL pc(USBTX,USBRX); // This input is used to send data to the pc
ThomasBNL 19:269f19917d80 12 Ticker TickerController; // This adds a Ticker to the function +TickerController
ThomasBNL 10:e923f51f18c4 13 //AnalogIn potmeter2(A0); /NIEUW
ThomasBNL 13:a0113cf2fc5f 14
ThomasBNL 19:269f19917d80 15 // Defining constants
ThomasBNL 19:269f19917d80 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 19:269f19917d80 19 const double motor2_Kp = 2.5; //controller gain which will be multiplied with the error (*how fast will the error be corrected)
ThomasBNL 13:a0113cf2fc5f 20
ThomasBNL 19:269f19917d80 21 // Function P-controller
ThomasBNL 19:269f19917d80 22 double P(double error, const double Kp) //returns error * controller gain
ThomasBNL 13:a0113cf2fc5f 23 {
ThomasBNL 19:269f19917d80 24 return Kp*error;
ThomasBNL 13:a0113cf2fc5f 25 }
ThomasBNL 19:269f19917d80 26 // Function that calls the P-controller
ThomasBNL 19:269f19917d80 27 void motor2_controller() // Void function that compares the current position with a reference value and outputs
ThomasBNL 19:269f19917d80 28 {
ThomasBNL 19:269f19917d80 29 double reference=400;
ThomasBNL 19:269f19917d80 30 double position=motor2.getPosition(); //current motor position
ThomasBNL 19:269f19917d80 31 double motor2_p_output=P((reference-position)*motor2_Kp); // reference-position=> current -> this is multiplied with the controller gain motor2_Kp
ThomasBNL 19:269f19917d80 32
ThomasBNL 13:a0113cf2fc5f 33
ThomasBNL 19:269f19917d80 34 // MAIN
ThomasBNL 10:e923f51f18c4 35 int main()
ThomasBNL 10:e923f51f18c4 36 {
ThomasBNL 19:269f19917d80 37 pc.baud(9600); // baud rate at which the information is processed to the computer
ThomasBNL 19:269f19917d80 38 motor2.setPosition(0); // calls the current position of the motor zero
ThomasBNL 10:e923f51f18c4 39 while(true) {
ThomasBNL 10:e923f51f18c4 40 if (button.read() < 0.5) { //if button pressed
ThomasBNL 13:a0113cf2fc5f 41 motor2direction = 0; // zero is clockwise (front view)
ThomasBNL 19:269f19917d80 42 motor2speed = 0.5f; // motorspeed
ThomasBNL 10:e923f51f18c4 43 pc.printf("positie = %d \r\n", motor2.getPosition());
ThomasBNL 10:e923f51f18c4 44 } else { // If button is not pressed
ThomasBNL 10:e923f51f18c4 45 motor2direction = 0;
ThomasBNL 10:e923f51f18c4 46 motor2speed = 0;
ThomasBNL 15:80af7a7671d4 47 //while (motor2_Controller /// WHILE MOTOR > ... <... HIER BEZIG
ThomasBNL 19:269f19917d80 48
ThomasBNL 15:80af7a7671d4 49 pc.printf("positie = %d en Error_positie =%d \r\n", motor2.getPosition(), Error_position);
ThomasBNL 13:a0113cf2fc5f 50 //myControllerTicker.attach(&motor2_Controller,0.01f);
ThomasBNL 19:269f19917d80 51 // while (Error_position > 0) {
ThomasBNL 19:269f19917d80 52 // motor2direction = 0;
ThomasBNL 19:269f19917d80 53 // motor2speed=0.3f;}
ThomasBNL 19:269f19917d80 54 // while (Error_position < 0) {
ThomasBNL 19:269f19917d80 55 // motor2direction = 1;
ThomasBNL 19:269f19917d80 56 // motor2speed=0.3f;}
ThomasBNL 10:e923f51f18c4 57 }
ThomasBNL 19:269f19917d80 58 while(1) {}
ThomasBNL 12:af428d56b4eb 59
ThomasBNL 13:a0113cf2fc5f 60 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 61 {
ThomasBNL 10:e923f51f18c4 62 motor2.setPosition(0);
ThomasBNL 13:a0113cf2fc5f 63 pc.printf(" HE \r\n LLO \r\n WO \r\n RLD \r\n !!! \r\n FOO! \r\n");
ThomasBNL 10:e923f51f18c4 64 break;
ThomasBNL 12:af428d56b4eb 65 }
ThomasBNL 10:e923f51f18c4 66 }
ThomasBNL 12:af428d56b4eb 67 }