1 year ago

#379113

test-img

Talar

C script for guiding a line follower

So for class I am trying to write a C script for a line follower robot. The robot has 3 sensors (ABC) which give logic 1 on black and logic 0 on white, A is on the left side, B in the middle and C on the right when looking straight down on the robot. It also has 2 motors, one on each side.

The board I am using is an Texas Instruments MSP-EXP430G2 and I am using the ports P1.0 - P1.7.

Now, I have literally 0 experience writing C scripts so all pointers are very much appreciated.

Here is the code.

# include "msp430G2553.h"
# include "stdio.h"
# include "math.h"

#define Both_On 1 // States
#define Left_On 2
#define Right_On 3
int main (void)
{
char STATE;
P1DIR|=0x0F; // Port1 four lower bits as OUTput, others as INput(0000 1111)
WDTCTL = WDTPW + WDTHOLD;// stop watch dog
STATE = Both_On; // Start here
char MASK1=0x10; //Sensor mask 0001 0000 (P1.0 - P1.7 from left to right)
char L; //Direction switch
while(1)
{
L = P1IN & MASK1; //Sensor value based on input-port
switch (STATE)
{
case Both_On:
    P1OUT = 0x03;//Both motors on
    puts("Both_On"); //for testing
    if (L == 0x00 ){
        P1OUT=0x01;//left motor on
        STATE = Left_On;}
    else if (L==0x10){
        P1OUT=0x03;
        STATE = Both_On;}
    break;
case Left_On:
    P1OUT = 0x01;//left motor on
    puts("Left_on"); //for testing
    if (L == 0x10 ){
        P1OUT=0x03;//both motors on
        STATE = Both_On;}
    else if (L==0x00){
        P1OUT= 0x02;
        STATE = Right_On;}
    break;
case Right_On:
    P1OUT = 0x02;//right motor on
    puts("Right_on"); //for testing
    if (L == 0x10 ){
        P1OUT=0x03;//both motors on
        STATE = Both_On;}
    else if (L==0x00){
        P1OUT= 0x01;
        STATE = Left_On;}
    break;
break;
}//end of Switch
}// end of while

I think I've understood the method for switching from one active motor to another or both. The first 4 bits are reserved for the input from the sensors and the last 4 bits direct the motors. So P1OUT=0x03 basically means the output is 0000 0011 so the ports P1.6 and P1.7 are active and the motors wired to those ports would turn on.

I've been instructed to use an "L" variable as the direction switch. My problem is that I don't quite understand how this works.

I've been thinking that I would wire the B sensor to the port "L" takes it value from and then saying, if L==1 then both motors should be on and if L==0 then the state should change from "Left_on" to "Right_on" in a loop but this behavior doesn't feel very logical, I would want better detection.

Could I change the mask to be "MASK1=0x70" (0111) which would give me 3 ports to wire the sensors into, and guide the motors using for example:

else if (L==0x40){ //0100 only the left side sensor A is on black
        P1OUT= 0x02; //0010 P1.6
        STATE = Right_On;} // turn the right motor on to keep following the line
    break;

As I said, all pointers and suggestions are highly appreciated.

c

state-machine

robot

0 Answers

Your Answer

Accepted video resources