Determines the setpoint in x direction of the robot. Robot specific variables still have to be set.

Dependencies:   MODSERIAL mbed

Committer:
Duif
Date:
Mon Oct 15 09:41:10 2018 +0000
Revision:
2:8f1765df595c
Parent:
1:f120a8734106
Child:
3:ded7541b44da
Determining setpoint x and printing the setpoint (actual variables still have to be determined)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Duif 0:0d70149fdb98 1 //Determining the setpoint that the thing will have to follow
Duif 0:0d70149fdb98 2 #include "mbed.h"
Duif 0:0d70149fdb98 3 #include "MODSERIAL.h"
Duif 1:f120a8734106 4 Ticker SetX;
Duif 0:0d70149fdb98 5 DigitalIn button(SW2);
Duif 0:0d70149fdb98 6 DigitalIn direction(SW3);
Duif 0:0d70149fdb98 7 MODSERIAL pc(USBTX, USBRX); //makes sure the computer is hooked up
Duif 0:0d70149fdb98 8
Duif 0:0d70149fdb98 9 //defining of the variables
Duif 2:8f1765df595c 10 //static float x_prev = 0; //previous setpoint
Duif 2:8f1765df595c 11 float x_new = 0; //new setpoint
Duif 2:8f1765df595c 12 const float x_max = 20; //maximum value of setpoint
Duif 2:8f1765df595c 13 const float x_min = 0; //minimum value of setpoint
Duif 2:8f1765df595c 14 float v=1; //moving speed of setpoint
Duif 2:8f1765df595c 15 volatile int s;//value of the button and store as switch
Duif 2:8f1765df595c 16 int dir = 1; //determine the direction of the setpoint placement
Duif 0:0d70149fdb98 17
Duif 2:8f1765df595c 18 //equation is x_new = x_prev + dir*s*v*dt
Duif 0:0d70149fdb98 19
Duif 1:f120a8734106 20 //function that determines the setpoint of the x coordinate
Duif 2:8f1765df595c 21 float EMGOn(int s){
Duif 2:8f1765df595c 22 if (x_new < x_min)
Duif 2:8f1765df595c 23 x_new = x_min;
Duif 2:8f1765df595c 24 if (x_new > x_max)
Duif 2:8f1765df595c 25 x_new = x_max;
Duif 2:8f1765df595c 26 else x_new = x_new + dir*s*v;
Duif 2:8f1765df595c 27 return x_new;
Duif 2:8f1765df595c 28 }
Duif 2:8f1765df595c 29
Duif 2:8f1765df595c 30 void ChangeDirection(){
Duif 2:8f1765df595c 31 dir = -1*dir;
Duif 0:0d70149fdb98 32 }
Duif 0:0d70149fdb98 33
Duif 0:0d70149fdb98 34 int main()
Duif 0:0d70149fdb98 35 {
Duif 2:8f1765df595c 36 InterruptIn direction(SW3);
Duif 2:8f1765df595c 37 direction.fall(ChangeDirection); //change the direction of the setpoint
Duif 2:8f1765df595c 38
Duif 2:8f1765df595c 39 //InterruptIn button(SW2);
Duif 0:0d70149fdb98 40 pc.baud(115200);
Duif 0:0d70149fdb98 41
Duif 0:0d70149fdb98 42 while (true) {
Duif 2:8f1765df595c 43 s = !button.read();
Duif 2:8f1765df595c 44 pc.printf("Value: %i, Set: %f \r\n",s,EMGOn(s)); //print the value of the button to check if it is correct
Duif 2:8f1765df595c 45 wait(0.5f);
Duif 0:0d70149fdb98 46 }
Duif 0:0d70149fdb98 47 }