class project digital Group B4-B5
Dependencies: ArduMotoShield ArduinoMotorShield mbed
main.cpp
- Committer:
- NorNick
- Date:
- 2015-12-09
- Revision:
- 2:83f5986b5e23
- Parent:
- 0:3aaac6f4553e
File content as of revision 2:83f5986b5e23:
#include "ArduMotoShield.h" #include "mbed.h" ArduMotoShield Motor; AnalogIn current(A2),RV_set(A3),RV2(A4); DigitalIn mybutton(USER_BUTTON); Serial Device(D8, D2); Serial pc(SERIAL_TX, SERIAL_RX); float max(float ,float ); float min(float ,float ); int main(void){ Device.baud(9600); pc.baud(9600); float value =0; float Speed = 0; float Left = 0,Right = 0; float x=0,y=0; char word; int i=0,count=0; float maxAmps = 0,minAmps = 0; pc.printf("Hello PROJECT_CAR.\n"); while(1) { // printf("Valuenery = %.2f. %d\n", RV2.read(),abs(i)); //wait(0.75); //Motor.forward(1.0,1.0); //pc.printf(" %.2f\n ",(current.read()*1000+10)); //wait(0.1); /*float amps = (501 - (current.read()*1000))*27.03/1023; maxAmps = max(maxAmps , amps); minAmps = min(minAmps , amps); float noise = maxAmps - minAmps; pc.printf("Curent is %.2f A. %.2f ",amps,abs(amps)); pc.printf("Noise is %.2f\n",noise); if (pc.readable() != -1){maxAmps = amps; minAmps = amps;} wait(0.1);*/ if(count<=0) { if(Device.readable()) { word = Device.getc(); pc.printf(" %c.\n",word); if(word == '%') { Speed = RV_set.read()*3.3; if(Speed >=0 && Speed < 0.825)//0-0.825 { x=0;y=0.824; pc.printf("Level %d is = %.3f \t %.3f \t %.3f \t %.3f.\n",i=0,x,y,RV_set.read(),Speed); } else if(Speed >= 0.825 && Speed < 1.650)//0.825-1.650 { x=0.825;y=1.649; pc.printf("Level %d is = %.3f \t %.3f \t %.3f \t %.3f.\n",i=1,x,y,RV_set.read(),Speed); } else if(Speed >= 1.650 && Speed < 2.475)//1.650-2.475 { x=1.650;y=2.474; pc.printf("Level %d is = %.3f \t %.3f \t %.3f \t %.3f.\n",i=2,x,y,RV_set.read(),Speed); } else//2.475-3.300 { x=2.475;y=3.300; pc.printf("Level %d is = %.3f \t %.3f \t %.3f \t %.3f.\n",i=3,x,y,RV_set.read(),Speed); } count++; } if(word == '#') { //Device.printf("24.3,324.5,234.4,\n"); Device.printf("%.2f,",0);//ความเร็ว Device.printf("%.2f,",0);//กระแส if(RV_set.read()>=0&&RV_set.read()<0.25) Device.printf("Kange,\n");//โหมด else if(RV_set.read()>=0.25&&RV_set.read()<0.50) Device.printf("Thin,\n");//โหมด else if(RV_set.read()>=0.50&&RV_set.read()<0.75) Device.printf("Long,\n");//โหมด else Device.printf("FAT,\n");//โหมด } } wait(0.5); } else { if(Device.readable()) { if(RV_set.read()>=0.03) { if(RV2.read()<0.49) { Right = y/3.3; Left = RV2.read()/2+(0.25*i); pc.printf("Value = %.2f , %.2f , %.2f.\n",RV2.read(),Left,Right); } else if(RV2.read()>=0.49&&RV2.read()<=0.51) { Left = (y/3.3)-0.072; Right = y/3.3; pc.printf("CentF = %.2f , %.2f , %.2f\n",Left,Right,RV2.read()); } else { Left = y/3.3; Right = (((RV2.read()*2)-2)*(-1)/4)+(0.25*i); pc.printf("Right = %.2f , %.2f , %.2f.\n",RV2.read(),Left,Right); } Motor.forward(Left,Right); float amps = (501 - (current.read()*1000))*27.03/1023; maxAmps = max(maxAmps , amps); minAmps = min(minAmps , amps); float noise = maxAmps - minAmps; Device.printf("%.2f,",abs(amps));//กระแส Device.printf("%.2f,",((Left+Right)/2));//ความเร็ว pc.printf("Current is %.2f\n",abs(amps)); if(1==0) Device.printf("Khang ,\n");//โหมด else if(i==1) Device.printf("Thin,\n");//โหมด else if(i==2) Device.printf("Long,\n");//โหมด else Device.printf("FAT,\n");//โหมด if (pc.readable() != -1){maxAmps = amps; minAmps = amps;} wait(0.8); } } } } //Motor.stop(); } float max(float a,float b) { if(a>b) return a; else return b; } float min(float a,float b) { if(a<b) return a; else return b; }