Sumejja Porča Almir Husić

Dependencies:   mbed sMotor

Committer:
tim008
Date:
Sun Jun 08 23:25:27 2014 +0000
Revision:
3:7f61e6ae0184
Parent:
2:0daf6588a802
Child:
4:dae3f24939ab
bla bla bla bla

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tim008 0:9464a1d8de8d 1 #include "mbed.h"
tim008 0:9464a1d8de8d 2 #include "sMotor.h"
tim008 0:9464a1d8de8d 3 #include <string>
tim008 0:9464a1d8de8d 4
tim008 0:9464a1d8de8d 5 sMotor motor(dp9, dp10, dp11, dp13); // creates new stepper motor: IN1, IN2, IN3, IN4
tim008 0:9464a1d8de8d 6 Serial pc(USBTX, USBRX);
tim008 0:9464a1d8de8d 7
tim008 0:9464a1d8de8d 8 int step_speed = 1200 ; // set default motor speed
tim008 0:9464a1d8de8d 9 int numstep = 512 ; // defines full turn of 360 degree
tim008 0:9464a1d8de8d 10 int direction = 0; //0 for right, 1 for left
tim008 0:9464a1d8de8d 11
tim008 3:7f61e6ae0184 12 AnalogIn sensor(dp4);
tim008 0:9464a1d8de8d 13
tim008 0:9464a1d8de8d 14 enum State {Idle, Working};
tim008 0:9464a1d8de8d 15
tim008 0:9464a1d8de8d 16 State state = Idle;
tim008 0:9464a1d8de8d 17 int index = 0;
tim008 0:9464a1d8de8d 18 int buffer_size = 300;
tim008 0:9464a1d8de8d 19 int samples_num = 13;
tim008 0:9464a1d8de8d 20 float distances[13] = {15.0, 20.0, 25.0, 30.0, 35.0, 40.0, 45.0, 50.0, 55.0, 60.0, 65.0, 70.0, 75.0};
tim008 0:9464a1d8de8d 21 float voltages[13] = {0.78, 0.72, 0.63, 0.55, 0.47, 0.43, 0.39, 0.36, 0.34, 0.33, 0.32, 0.311, 0.31};
tim008 0:9464a1d8de8d 22 float value;
tim008 3:7f61e6ae0184 23 int angle(10);
tim008 3:7f61e6ae0184 24 char number [4]={'0','0','0','0'};
tim008 3:7f61e6ae0184 25 void ToString(float num)
tim008 0:9464a1d8de8d 26 {
tim008 0:9464a1d8de8d 27 number[0] = num/10 + '0';
tim008 0:9464a1d8de8d 28 number[1] = int(num) % 10 + '0';
tim008 0:9464a1d8de8d 29 number[2] = '.';
tim008 0:9464a1d8de8d 30 number[3] = int((num - int(num)) * 10) + '0';
tim008 3:7f61e6ae0184 31
tim008 0:9464a1d8de8d 32 }
tim008 0:9464a1d8de8d 33
tim008 0:9464a1d8de8d 34 void MoveMotor()
tim008 0:9464a1d8de8d 35 {
tim008 3:7f61e6ae0184 36 motor.step(int(numstep / 360.0 * (angle % 360) + 0.5), direction, step_speed); // number of steps, direction, speed
tim008 0:9464a1d8de8d 37 }
tim008 0:9464a1d8de8d 38
tim008 0:9464a1d8de8d 39 void CheckState()
tim008 3:7f61e6ae0184 40 {
tim008 0:9464a1d8de8d 41 if (pc.readable())
tim008 0:9464a1d8de8d 42 {
tim008 3:7f61e6ae0184 43 string command;
tim008 3:7f61e6ae0184 44 pc.scanf("%s",&command);
tim008 3:7f61e6ae0184 45 if (command == "Start")
tim008 0:9464a1d8de8d 46 {
tim008 0:9464a1d8de8d 47 state = Working;
tim008 0:9464a1d8de8d 48 }
tim008 3:7f61e6ae0184 49 else if (command == "Stop")
tim008 3:7f61e6ae0184 50 { state = Idle;
tim008 0:9464a1d8de8d 51 }
tim008 3:7f61e6ae0184 52
tim008 3:7f61e6ae0184 53
tim008 0:9464a1d8de8d 54 }
tim008 0:9464a1d8de8d 55 }
tim008 0:9464a1d8de8d 56
tim008 0:9464a1d8de8d 57 float InterpolatedValue( int i )
tim008 0:9464a1d8de8d 58 {
tim008 0:9464a1d8de8d 59 float alpha = (value - voltages[i]) / (voltages[i + 1] - voltages[i]);
tim008 3:7f61e6ae0184 60 return (1 - alpha)* distances[i] + alpha*distances[i + 1];
tim008 0:9464a1d8de8d 61 }
tim008 0:9464a1d8de8d 62
tim008 0:9464a1d8de8d 63 void GenerateValue()
tim008 0:9464a1d8de8d 64 {
tim008 0:9464a1d8de8d 65
tim008 0:9464a1d8de8d 66 value = sensor;
tim008 0:9464a1d8de8d 67 if( value < 0.0001)
tim008 0:9464a1d8de8d 68 value = -1.0;
tim008 0:9464a1d8de8d 69 else
tim008 0:9464a1d8de8d 70 for( int i = 0; i < samples_num - 1; i++)
tim008 0:9464a1d8de8d 71 {
tim008 0:9464a1d8de8d 72 if( value > voltages[0])
tim008 0:9464a1d8de8d 73 {
tim008 0:9464a1d8de8d 74 value = 10;
tim008 0:9464a1d8de8d 75 break;
tim008 0:9464a1d8de8d 76 }
tim008 0:9464a1d8de8d 77 else if( value >= voltages[i] && value <= voltages[i + 1])
tim008 0:9464a1d8de8d 78 {
tim008 0:9464a1d8de8d 79 value = InterpolatedValue(i);
tim008 0:9464a1d8de8d 80 break;
tim008 0:9464a1d8de8d 81 }
tim008 0:9464a1d8de8d 82 }
tim008 0:9464a1d8de8d 83
tim008 3:7f61e6ae0184 84 ToString(value);
tim008 3:7f61e6ae0184 85 string distance = number;
tim008 3:7f61e6ae0184 86 pc.printf("%s\n", distance);
tim008 0:9464a1d8de8d 87 }
tim008 0:9464a1d8de8d 88 int main() {
tim008 3:7f61e6ae0184 89 pc.attach(&CheckState);
tim008 0:9464a1d8de8d 90 while (1) {
tim008 3:7f61e6ae0184 91
tim008 3:7f61e6ae0184 92
tim008 0:9464a1d8de8d 93 if (state == Working)
tim008 0:9464a1d8de8d 94 {
tim008 3:7f61e6ae0184 95
tim008 0:9464a1d8de8d 96 GenerateValue();
tim008 0:9464a1d8de8d 97 MoveMotor();
tim008 3:7f61e6ae0184 98 wait(1);
tim008 0:9464a1d8de8d 99 }
tim008 3:7f61e6ae0184 100
tim008 3:7f61e6ae0184 101 }
tim008 3:7f61e6ae0184 102 }
tim008 3:7f61e6ae0184 103