ROBOTC.net forums
http://www.robotc.net/forums/

Maze Help for PIC
http://www.robotc.net/forums/viewtopic.php?f=11&t=5779
Page 1 of 1

Author:  jwkappel [ Mon Apr 29, 2013 7:48 am ]
Post subject:  Maze Help for PIC

Hello All,

I am new to programming with RobotC and am working with a student to figure out what is going on with his maze. We are using RobotC 3.60 to program our V.5 PIC. Our problem is this: We are wanting to use the bump sensors to bump a wall, backup and make a turn, continue driving straight, then repeat the process. The only variable would be which direction the robot turns when it bumps the wall. We like the idea of having a variable (turns) in order to keep track of where he is at in the maze. I would like to know if there is anything obvious that stands out in his code right of the bat. The code is not complete, we were using it for testing purposes. I will attach it below. I'd prefer to be able to continue with his own code, but will accept any other suggestions or approaches. Thanks!

#pragma config(Sensor, in16, bumper, sensorTouch)
#pragma config(Motor, port1, leftmotor, tmotorVex393, openLoop)
#pragma config(Motor, port2, rightmotor, tmotorVex393, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

task main()
{
int turns = 0;

while (turns == 0)
{

motor[port1] = 127;
motor[port2] = 127;

if (SensorValue(bumper) == 1)
motor[port1] = -127;
motor[port2] = -127;
wait1Msec(1000);
motor[port1] = 127;
motor[port2] = -127;
wait1Msec(1000);
turns++;
}

while (turns == 1)
{

motor[port1] = 127;
motor[port2] = 127;

if (SensorValue(bumper) == 1)
motor[port1] = -127;
motor[port2] = -127;
wait1Msec(1000);
motor[port1] = -127;
motor[port2] = 127;
wait1Msec(1000);
turns++;

}

while (turns == 2)
{
motor[port1] = 127;
motor[port2] = 127;

if (SensorValue(bumper) == 1)
motor[port1] = -127;
motor[port2] = -127;
wait1Msec(1000);
motor[port1] = 127;
motor[port2] = -127;
wait1Msec(1000);
turns++;
}


}

Attachments:
File comment: code
robotc_pic_maze.c [1.2 KiB]
Downloaded 324 times
File comment: Maze Layout
maze_layout.pdf [125.08 KiB]
Downloaded 452 times

Author:  JohnWatson [ Tue May 21, 2013 9:51 am ]
Post subject:  Re: Maze Help for PIC

Code:
if (SensorValue(bumper) == 1)
motor[port1] = -127;
motor[port2] = -127;
wait1Msec(1000);
motor[port1] = 127;
motor[port2] = -127;
wait1Msec(1000);


This code may be part of the problem. Remember, your if statements, while loops, etc should use the braces to encompass they code they want to control. In this example, only the first line (motor[port1] = -127;) is being controlled by the if statement; the rest of the code will run regardless of the state of the bumper switch. Instead, try this:

Code:
if (SensorValue(bumper) == 1)
{
motor[port1] = -127;
motor[port2] = -127;
wait1Msec(1000);
motor[port1] = 127;
motor[port2] = -127;
wait1Msec(1000);
}


This will run that code segment if and only if the value of the 'bumper' sensor is equal to 1. You may need to make these changes throughout the rest of the program as well.

Author:  jwkappel [ Wed May 22, 2013 3:47 pm ]
Post subject:  Re: Maze Help for PIC

OK. Thank You! That got me further, but I am still having an issue. Now if I run the code with the cable still connected and the debugger open, it works great. Then, if I unplug and turn the robot off and on, it runs through the while loop and the first if statement before the sensor is pressed, which throws off my count. Shouldn't it run through the while loop continuing to drive forward until the sensor is pressed the first time, then run through the first if, which advances the count? I am including my updated code.

Thanks Again.

#pragma config(Sensor, in16, bumper, sensorTouch)
#pragma config(Motor, port1, leftmotor, tmotorVex393, openLoop)
#pragma config(Motor, port2, rightmotor, tmotorVex393, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

task main()
{
int turns = 0;


while (turns == 0)
{

motor[port1] = 127;
motor[port2] = 127;

if (SensorValue(bumper) == 1)
{
motor[port1] = -127;
motor[port2] = -127;
wait1Msec(1000);
motor[port1] = 127;
motor[port2] = -127;
wait1Msec(1000);
turns++;
}
}

while (turns == 1)
{

motor[port1] = 127;
motor[port2] = 127;

if (SensorValue(bumper) == 1)
{
motor[port1] = -127;
motor[port2] = -127;
wait1Msec(1000);
motor[port1] = -127;
motor[port2] = 127;
wait1Msec(1000);
turns++;
}


}

while (turns == 2)
{
motor[port1] = 127;
motor[port2] = 127;

if (SensorValue(bumper) == 1)
{
motor[port1] = -127;
motor[port2] = -127;
wait1Msec(1000);
motor[port1] = 0;
motor[port2] = 0;
turns = 0;
}

}


}

Attachments:
robot_pic_maze.c [1014 Bytes]
Downloaded 345 times

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/