Minimal code to spin the motor

Dependencies:   Encoder MODSERIAL mbed

Committer:
ThomasBNL
Date:
Thu Sep 17 10:15:44 2015 +0000
Revision:
2:f5c9d981de51
Parent:
1:b0d3c64bd4d8
Child:
3:eee8d5461256
set triangle_signal to 3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ThomasBNL 0:843492f4fe62 1 #include "mbed.h"
ThomasBNL 0:843492f4fe62 2 #include "HIDScope.h"
ThomasBNL 0:843492f4fe62 3
ThomasBNL 1:b0d3c64bd4d8 4 // Define the HIDScope and Ticker object
ThomasBNL 1:b0d3c64bd4d8 5 HIDScope scope(1);
ThomasBNL 1:b0d3c64bd4d8 6 Ticker scopeTimer;
ThomasBNL 1:b0d3c64bd4d8 7
ThomasBNL 1:b0d3c64bd4d8 8 // Read the analog input
ThomasBNL 2:f5c9d981de51 9 float triangle_signal = 3;
ThomasBNL 1:b0d3c64bd4d8 10
ThomasBNL 1:b0d3c64bd4d8 11 // The data read and send function
ThomasBNL 1:b0d3c64bd4d8 12 void scopeSend()
ThomasBNL 1:b0d3c64bd4d8 13 {
ThomasBNL 1:b0d3c64bd4d8 14 scope.set(0,triangle_signal);
ThomasBNL 1:b0d3c64bd4d8 15 scope.send();
ThomasBNL 1:b0d3c64bd4d8 16 }
ThomasBNL 1:b0d3c64bd4d8 17
ThomasBNL 1:b0d3c64bd4d8 18 int main()
ThomasBNL 1:b0d3c64bd4d8 19 {
ThomasBNL 1:b0d3c64bd4d8 20 // Attach the data read and send function at 100 Hz
ThomasBNL 1:b0d3c64bd4d8 21 scopeTimer.attach_us(&scopeSend, 1e4);
ThomasBNL 1:b0d3c64bd4d8 22
ThomasBNL 1:b0d3c64bd4d8 23 while(1) { }
ThomasBNL 1:b0d3c64bd4d8 24 }
ThomasBNL 1:b0d3c64bd4d8 25
ThomasBNL 1:b0d3c64bd4d8 26 ////// P Motor Controller
ThomasBNL 0:843492f4fe62 27
ThomasBNL 0:843492f4fe62 28 // Controller gain
ThomasBNL 0:843492f4fe62 29 const double motorP_Kp=2.5;
ThomasBNL 0:843492f4fe62 30 const double Convert_volt_to_position=0.00300;
ThomasBNL 0:843492f4fe62 31
ThomasBNL 0:843492f4fe62 32 // Reusable P controller (FUNCTIE)
ThomasBNL 0:843492f4fe62 33 double P (double error, const double Kp)
ThomasBNL 0:843492f4fe62 34 {
ThomasBNL 0:843492f4fe62 35 Return Kp*error;
ThomasBNL 0:843492f4fe62 36 }
ThomasBNL 0:843492f4fe62 37
ThomasBNL 0:843492f4fe62 38 // Error measurement and apply the output to the plant
ThomasBNL 0:843492f4fe62 39 void motorP_Controller()
ThomasBNL 0:843492f4fe62 40 {
ThomasBNL 0:843492f4fe62 41 double reference_position = potmeter1.read();
ThomasBNL 0:843492f4fe62 42 double position = Convert_volt_to_position*encoder1.getPosition();
ThomasBNL 0:843492f4fe62 43 motorP=P(reference_position - position, motorP_Kp);
ThomasBNL 0:843492f4fe62 44 }
ThomasBNL 0:843492f4fe62 45
ThomasBNL 0:843492f4fe62 46 int main()
ThomasBNL 0:843492f4fe62 47 {
ThomasBNL 0:843492f4fe62 48 myControllerTicker.attach(&motorP_Controller,0.01f); //100Hz
ThomasBNL 0:843492f4fe62 49 while(1){}
ThomasBNL 0:843492f4fe62 50 }