Saturday, 7 March 2015

WEEK 2- Arduino Ethernet, Simulation of a helicopter control system

Location: 3rd floor, EEE building, University of Liverpool
Date: 13th February, 2015
Thank you for waiting for a long time. This is the second blog for what we achieved in week 2.
After the discussion in week 1, our group began to write the codes which control the two DC motors. By changing the voltage of the motor, for states which are up, down, forward and backward can be achieved. Figure 1 is the screen shot of our codes.

int motorPin1=3,motorPin2=4;
char getstr;
String f="";
String b="";
String u="";
String d="";
String s="";
String r="";
String comdata = "";
int flag;
void setup()
{
    
    pinMode(motorPin1, OUTPUT);
    pinMode(motorPin2, OUTPUT);
    Serial.begin(9600);
    f="f";
    b="b";
    u="u";
    d="d";
    s="s";
    r="r";
    flag=0;
}

void loop()
{
  
    while (Serial.available() > 0)  
    {
        comdata += char(Serial.read());
        delay(2);
    }
    if (comdata.length() > 0)
    {
        
          if(comdata==u&&(flag==0||flag==1))  //flag=0 initial condition, flag=1 stable condition
        {
           motorUp();
           flag=2;
           Serial.println(flag);
         }
         else
         if(comdata==d&&(flag==1||flag==2))  //flag=2 up condition
         {
           if(flag==2)
         {
           motorStable();
         }
           motorDown();
           motorOff();
           
           Serial.println(flag);
           flag=0;
         }
         else
         if(comdata==f&&flag==1) //flag=3 forward condition
         {
           motorForwardPrepare();
           motorStable();
           flag=3;
           Serial.println("s");
         }
         else 
         if(comdata==b&&flag==1) //flag=4 backward condition
         {
           motorBackwardPrepare();
           motorStable();
           flag=4;
         }
         else
         if(comdata==s&&flag==2)
         {
           motorStable();
           flag=1;
         }
         else
         if(comdata==r&&(flag==3||flag==4))
         {
           if(flag==3)
           {
            motorFordwardRecover();
            flag=1;
           }
           else
           {
            motorBackwardRecover();
            flag= 1;
           }
           
         }
         else
         { 
          Serial.println("input wrong!")
         }
         comdata="";
    }
}
  
  
  void motorUp()
{
int onTime0=200;
analogWrite(motorPin1,255);
analogWrite(motorPin2,255);
delay(onTime0);
}



void motorStable()
{
int delayTime1=5;
int onTime1=100;
for (int i=255; i>=200;i--)
{
analogWrite(motorPin2,i);
analogWrite(motorPin1,i);
delay(delayTime1);
}
analogWrite(motorPin2,200);
analogWrite(motorPin1,200);
delay(onTime1);
}



void motorDown()
{
int delayTime2=50;
int onTime2=100;
for(int j=200;j>100;j--)
{
analogWrite(motorPin1, j);
analogWrite(motorPin2, j);
delay(delayTime2);
}
analogWrite(motorPin1,200);
analogWrite(motorPin2,200)                                                                                                                                   ;
delay(onTime2);
}



void motorOff()
{
int delayTime3=50;

int delayTime4=20000;
for (int k=100; k>=0;k--)
{
analogWrite(motorPin1,k);
analogWrite(motorPin2,k);
delay(delayTime3);

}

analogWrite(motorPin1,0);

analogWrite(motorPin2,0);

delay(delayTime4);
 Serial.println(flag);
}


void motorForwardPrepare()

{
int delayTime5=50;
for(int m=200;m>=170;m--)

{

analogWrite(motorPin1,200);

analogWrite(motorPin2,m);

delay(delayTime5);

}

analogWrite(motorPin2,200);
}


void motorForwardRecover()
{
delayTime5=50;
for(n=200;n<=255;n++)
{
analogWrite(motorPin1,n);
analogWrite(motorPin2,200);
delay(dealyTime50);
}
analogWrite(motorPin1,200);
analogwrite(motorPin2,200);
}





void motorBackwardPrepare()
{
int delayTime6=50;
for(int n=200;n>=170;n--)
{
analogWrite(motorPin2,200);
analogWrite(motorPin1,n);
delay(delayTime6);
}
analogWrite(motorPin1,200);
}


void motorBackwardRecover()
{
delayTime5=50;
for(n=200;n<=255;n++)
{
analogWrite(motorPin2,n);
analogWrite(motorPin1,200);
delay(dealyTime50);
}
analogWrite(motorPin1,200);
analogWrite(motorPin2,200);
}


In the design, the whole process is divided into 5 parts: up, stable, forward, backward and down. STABLE must follow the states UP, FORWARD and BACKWARD. When conducting the up, the two motors running at high speed. When conducting forward, the second motor accelerates. When conducting backward, the first motor accelerates.

Thank you for reading the second week blog. In the third week, we prepare to finish the code for Bluetooth control and debug the Bluetooth.

No comments:

Post a Comment