class project digital Group B4-B5

Dependencies:   ArduMotoShield ArduinoMotorShield mbed

main.cpp

Committer:
NorNick
Date:
2015-12-09
Revision:
0:3aaac6f4553e
Child:
2:83f5986b5e23

File content as of revision 0:3aaac6f4553e:

#include "ArduMotoShield.h"
#include "mbed.h"

ArduMotoShield Motor;
AnalogIn EMS(A0),A(A1),current(A2),RV_set(A3),RV2(A4);
DigitalIn mybutton(USER_BUTTON),L339(D4);

Serial Device(D8, D2);
Serial pc(SERIAL_TX, SERIAL_RX);

void swap(float &x,float &y);

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;
    
    pc.printf("Hello PROJECT_CAR.\n");
    while(1)
    {
       // printf("Valuenery = %.2f. %d\n", RV2.read(),abs(i));
        //wait(0.75);
        Motor.stop();
        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 == '#')
                { 
                    value = current.read()*1023;
                    Device.printf("%.2f,",0);//ความเร็ว
                    Device.printf("%.2f,",(((value-490)*5/1023)/0.0645));//กระแส
                    if(RV_set.read()>=0&&RV_set.read()<0.25)
                        Device.printf(" แห้ง ,\n");//โหมด
                    else if(RV_set.read()>=0.25&&RV_set.read()<0.50)
                        Device.printf(" ผอม ,\n");//โหมด
                    else if(RV_set.read()>=0.50&&RV_set.read()<0.75)
                        Device.printf(" อวบ ,\n");//โหมด
                    else
                        Device.printf(" อ้วน ,\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;
                        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); 
                        value = current.read()*1023;
                        Device.printf("%.2f,",((Left+Right)/2));//ความเร็ว
                        Device.printf("%.2f,",(((value-490)*5/1023)/0.0645));//กระแส
                        if(1==0)
                            Device.printf(" แห้ง ,\n");//โหมด
                        else if(i==1)
                            Device.printf(" ผอม ,\n");//โหมด
                        else if(i==2)
                            Device.printf(" อวบ ,\n");//โหมด
                        else
                            Device.printf(" อ้วน ,\n");//โหมด
                    wait(0.5);
                }
            }
        }
    }
    //Motor.stop();
}

void swap(float &x,float &y)
{
    float temp;
    temp = x;
    x = y;
    y = temp;
}