Floris Hoek / Mbed 2 deprecated template_biorobotics_Group_18

Dependencies:   mbed QEI HIDScope biquadFilter MODSERIAL FastPWM

Committer:
paulstuiver
Date:
Mon Oct 07 12:25:49 2019 +0000
Revision:
2:75b2f713161c
Parent:
1:b862262a9d14
Child:
3:4f281c336a8b
Non working motor control

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RobertoO 0:67c50348f842 1 #include "mbed.h"
RobertoO 1:b862262a9d14 2 #include "MODSERIAL.h"
paulstuiver 2:75b2f713161c 3 #include "FastPWM.h"
paulstuiver 2:75b2f713161c 4
paulstuiver 2:75b2f713161c 5 DigitalIn button1(D12);
paulstuiver 2:75b2f713161c 6 AnalogIn pot2(A0);
paulstuiver 2:75b2f713161c 7 Ticker john;
paulstuiver 2:75b2f713161c 8 DigitalOut motor1Direction(D6);
paulstuiver 2:75b2f713161c 9 FastPWM motor1Velocity(D7);
paulstuiver 2:75b2f713161c 10 Serial pc(USBTX, USBRX);
paulstuiver 2:75b2f713161c 11 volatile double frequency = 17000.0;
paulstuiver 2:75b2f713161c 12 volatile double period_signal = 1.0/frequency;
paulstuiver 2:75b2f713161c 13
paulstuiver 2:75b2f713161c 14 //get the measured velocity
paulstuiver 2:75b2f713161c 15 double getmeasuredvelocity()
paulstuiver 2:75b2f713161c 16 {
paulstuiver 2:75b2f713161c 17 float measuredvelocity;
paulstuiver 2:75b2f713161c 18 measuredvelocity = pot2.read();
paulstuiver 2:75b2f713161c 19 return measuredvelocity;
paulstuiver 2:75b2f713161c 20 }
paulstuiver 2:75b2f713161c 21
paulstuiver 2:75b2f713161c 22 //get the reference of the velocity: positive or negative
paulstuiver 2:75b2f713161c 23 double getreferencevelocity()
paulstuiver 2:75b2f713161c 24 {
paulstuiver 2:75b2f713161c 25 //const float maxvelocity = 8.4;
paulstuiver 2:75b2f713161c 26 /*volatile float referencevelocity;
paulstuiver 2:75b2f713161c 27 if (button1.read() == 0)
paulstuiver 2:75b2f713161c 28 {
paulstuiver 2:75b2f713161c 29 referencevelocity = pot2.read()*maxvelocity;
paulstuiver 2:75b2f713161c 30 }
paulstuiver 2:75b2f713161c 31 else
paulstuiver 2:75b2f713161c 32 {
paulstuiver 2:75b2f713161c 33 referencevelocity = -1*pot2.read()*maxvelocity;
paulstuiver 2:75b2f713161c 34 }*/
paulstuiver 2:75b2f713161c 35 float referencevelocity = -1.0 + 2.0*pot2.read();
paulstuiver 2:75b2f713161c 36 //pc.printf("The reference velocity is %f\r\n", referencevelocity);
paulstuiver 2:75b2f713161c 37 return referencevelocity;
paulstuiver 2:75b2f713161c 38 }
RobertoO 0:67c50348f842 39
paulstuiver 2:75b2f713161c 40 float vel = 0.0f;
paulstuiver 2:75b2f713161c 41 //send value to motor
paulstuiver 2:75b2f713161c 42 void sendtomotor(float motorvalue)
paulstuiver 2:75b2f713161c 43 {
paulstuiver 2:75b2f713161c 44 /*
paulstuiver 2:75b2f713161c 45 pc.printf("The motor value is %f\r\n", motorvalue);
paulstuiver 2:75b2f713161c 46 if (motorvalue >= 0)
paulstuiver 2:75b2f713161c 47 {
paulstuiver 2:75b2f713161c 48 motor1Direction = 1;
paulstuiver 2:75b2f713161c 49 pc.printf("hello");
paulstuiver 2:75b2f713161c 50 }
paulstuiver 2:75b2f713161c 51 else
paulstuiver 2:75b2f713161c 52 {
paulstuiver 2:75b2f713161c 53 motor1Direction = 0;
paulstuiver 2:75b2f713161c 54 pc.printf("wow");
paulstuiver 2:75b2f713161c 55 }
paulstuiver 2:75b2f713161c 56 if (fabs(motorvalue)>1)
paulstuiver 2:75b2f713161c 57 {
paulstuiver 2:75b2f713161c 58 motor1Velocity = 1;
paulstuiver 2:75b2f713161c 59 }
paulstuiver 2:75b2f713161c 60 else
paulstuiver 2:75b2f713161c 61 {
paulstuiver 2:75b2f713161c 62 motor1Velocity = fabs(motorvalue);
paulstuiver 2:75b2f713161c 63 }*/
paulstuiver 2:75b2f713161c 64 vel = fabs(motorvalue);
paulstuiver 2:75b2f713161c 65 vel = vel > 1.0f ? 1.0f : vel;
paulstuiver 2:75b2f713161c 66 motor1Velocity = vel;
RobertoO 0:67c50348f842 67
paulstuiver 2:75b2f713161c 68 motor1Direction = (motorvalue > 0.0f);
paulstuiver 2:75b2f713161c 69 //pc.printf("The motor value is %f\r\n", motorvalue);
paulstuiver 2:75b2f713161c 70 //pc.printf("The motor direction is %s\r\n", motor1Direction ? "links" : "rechts");
paulstuiver 2:75b2f713161c 71 }
RobertoO 0:67c50348f842 72
paulstuiver 2:75b2f713161c 73 // function to call reference velocity, measured velocity and controls motor with feedback
paulstuiver 2:75b2f713161c 74 void measureandcontrol(void)
paulstuiver 2:75b2f713161c 75 {
paulstuiver 2:75b2f713161c 76 float referencevelocity = getreferencevelocity();
paulstuiver 2:75b2f713161c 77 float measuredvelocity = getmeasuredvelocity();
paulstuiver 2:75b2f713161c 78 // float motorvalue = feedbackcontrol(referencevelocity - measuredvelocity);
paulstuiver 2:75b2f713161c 79 sendtomotor(referencevelocity);
paulstuiver 2:75b2f713161c 80 }
RobertoO 0:67c50348f842 81 int main()
RobertoO 0:67c50348f842 82 {
RobertoO 0:67c50348f842 83 pc.baud(115200);
paulstuiver 2:75b2f713161c 84 pc.printf("Starting...\r\n");
paulstuiver 2:75b2f713161c 85 motor1Velocity.period(period_signal);
paulstuiver 2:75b2f713161c 86 john.attach(measureandcontrol, 0.001f);
paulstuiver 2:75b2f713161c 87 while(true)
paulstuiver 2:75b2f713161c 88 {
paulstuiver 2:75b2f713161c 89 wait(0.5);
paulstuiver 2:75b2f713161c 90 pc.printf("%f\r\n", vel);
RobertoO 0:67c50348f842 91 }
paulstuiver 2:75b2f713161c 92 }