ather_88
Newbie level 5
- Joined
- May 22, 2014
- Messages
- 10
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 90
I have written a code for a robot which is receiving commands serially, If it Receives 'W' it moves forward,
'S' it moves backward,
'A' it moves left,
'D' it moves right,
I have used limit switches at the rear portion, when it touches any obstacle while moving behind, i.e. the limit switch makes contact in the BACKWARD direction and the robot should stop and wait for the obstacle to move so that It can move again the backward direction, mean while in the stop condition If it receives other three commands the robot should move in that direction.
The code which I've tested doesn't stops the robot when any of the two limit switches gets ZERO '0' at pins D0 and D1 in void chk_lmt_swt() function.
'S' it moves backward,
'A' it moves left,
'D' it moves right,
I have used limit switches at the rear portion, when it touches any obstacle while moving behind, i.e. the limit switch makes contact in the BACKWARD direction and the robot should stop and wait for the obstacle to move so that It can move again the backward direction, mean while in the stop condition If it receives other three commands the robot should move in that direction.
The code which I've tested doesn't stops the robot when any of the two limit switches gets ZERO '0' at pins D0 and D1 in void chk_lmt_swt() function.
Code:
#include<16f877a.h>
#fuses HS,NOWDT,NOLVP
#use delay(clock=20000000)
//#use rs232(baud=115200,parity=N,xmit=PIN_C6,rcv=PIN_C7,stream=RS232,bits=8)
#use rs232(baud=57600,parity=N,xmit=PIN_C6,rcv=PIN_C7,stream=RS232,bits=8)
#define M1_1 PIN_B0 //left motor 1 Fwd
#define M1_2 PIN_B1 //left motor 1 Rev
#define M2_1 PIN_B2 //left motor 2 Fwd
#define M2_2 PIN_B3 //left motor 2 Rev
#define M3_1 PIN_B4 //Right motor 1 Fwd
#define M3_2 PIN_B5 //Right motor 1 Rev
#define M4_1 PIN_B6 //Right motor 2 Fwd
#define M4_2 PIN_B7 //Right motor 2 Rev
#define trig PIN_D6
//trig = PIN_RD6 //pin 29 OUTPUT
//Echo = PIN_RD7 //pin 30 INPUT
void stop();
void forward();
void reverse();
void right();
void left();
void ultrasonic();
void chk_lmt_swt();
int going_forward;
int going_reverse;
int going_right;
int going_left;
void chk_lmt_swt()
{
if(!input(PIN_D0) || !input(PIN_D1))
{
stop();
}
}
void ultrasonic()
{
int16 width;
int16 range=0.00;
width=0;
range=0;
output_high(trig); //send pulse with a 10us width
delay_us(20);
output_low(trig);
while (!input(PIN_D7)) //wait until an echo is received
{
}
while (input(PIN_D7)) //calculate the width of the echo received
{
delay_us(10);
width++;
}
range=(width*10)/58; //calculate range
range=range/2;
if(range<5)
{
stop();
output_high(PIN_B6);
}
}
void forward()
{
output_high(M1_1);
output_high(M2_1);
output_high(M3_1);
output_high(M4_1);
going_forward =1;
}
void reverse()
{
output_high(M1_2);
output_high(M2_2);
output_high(M3_2);
output_high(M4_2);
going_reverse=1;
chk_lmt_swt();
}
void right()
{
output_high(M1_1);
output_high(M2_1);
output_low(M3_1);
output_low(M4_1);
going_right = 1;
}
void left()
{
output_high(M3_1);
output_high(M4_1);
output_low(M1_1);
output_low(M2_1);
going_left=1;
}
void stop()
{
output_low(M1_1);
output_low(M2_1);
output_low(M3_1);
output_low(M4_1);
output_low(M1_2);
output_low(M2_2);
output_low(M3_2);
output_low(M4_2);
going_forward=0;
going_reverse=0;
going_right=0;
going_left=0;
delay_ms(500);
}
void main()
{
char data;
set_tris_d(0x80);
output_high(PIN_B7);
stop();
while(1)
{
data=getc();
switch(data)
{
case 0x57: // for forward W
if(going_reverse==1||going_right==1||going_left==1)
stop();
forward();
// if(going_forward==1)
// ultrasonic();
break;
case 0x53: // for Reverse S
if(going_forward==1||going_left==1||going_right==1)
{
stop();
reverse();
}
else
if(going_reverse==1)
chk_lmt_swt();
break;
case 0x41: //for Left A
if(going_forward==1||going_reverse==1||going_right==1)
stop();
//if (going_left==1)
left();
break;
case 0x44: //FOR RIGHT D
if(going_forward==1||going_left==1||going_reverse==1)
stop();
right();
break;
case 0x58: //for STOP X
stop();
}
}
}