Shop OBEX P1 Docs P2 Docs Learn Events
Scribbler Maze Program and Timers — Parallax Forums

Scribbler Maze Program and Timers

jphilljphill Posts: 4
edited 2013-06-11 08:36 in Robotics
I'm trying to make a program that would accomplish basically what happens in this video, which has been posted before: http://www.youtube.com/watch?feature=player_detailpage&v=Hqq6gWrpWQI

The basic idea is that the robot goes forward until it hits a wall, tries turning right, if it hits a wall again right away, it makes a 180 degree turn, effectively turning left from its original position, and goes forward.

I'm new to programming but I can put that much together in the S2 GUI using flags. When it hits the first wall, it puts up a flag. If it hits the second wall and the flag is up, it turns 180 degrees and puts the flag back down. The thing I can't do in the GUI is this: If the robot turns right and is successful (i.e. goes 3 seconds without hitting a wall), at the next wall it hits it should turn right, not make a 180 degree turn. To do this, I need to have some kind of timer that tells it to forget the old wall if it's been longer than 3 seconds. The GUI has a delay function, but I don't think that will work because the delay stops other processing from taking place, so it can't check for new walls while that delay timer is running. Perhaps the GUI only lets you use one cog, so you can't get two programs to run simultaneously? Is this correct?

So I think I need to use the Propeller tool to make my program. I've pasted the code below.

At the beginning of the program I start a timer. When the robot hits a wall it resets the timer, puts up a flag, and turns right. The turn takes 4 seconds, so if the timer gets above 7 seconds, it should put down the flag. I used the LEDs to tell me when the flag was up or down. If the flag is up, the middle LED is red. If the flag is down, the outside LEDs are red.

When I ran the program, the timer never seemed to get above 7000 milliseconds. The flag never went down and the robot never made two consecutive right turns.

I changed the 6th line of code from "if elapsed > 7000" to "if elapsed > 7." This made no difference. Apparently the timer never even got above 7 milliseconds.

Then I changed it to "if elapsed < 7000." This made a big difference. The flag was always down, and the robot only made right turns, never U-turns.

The problem must be in the way I'm asking it to reset and check the timer. The variable "elapsed" doesn't seem to change over time. So either the timer isn't counting up correctly or the variable isn't updating to the new values of the timer.

What am I doing wrong? Am I not resetting the timers correctly? Am I not defining the variable "elapsed" correctly? Do I need some other .spin file in the same folder so that my program can refer to it?

I've only included the PUB Green part of the program, but let me know if the rest of the code would be helpful to look at.

Thanks for your time



PUB Green

if (ResetCount == 1)
MotorSet(128, 128, 0)
s2.start_timer (2)
repeat
elapsed := s2.get_timer(2)
if elapsed < 7000
Flag_green := false
s2.set_leds(s2#RED, s2#OFF, s2#RED, s2#NO_CHANGE)
if (Flag_green)
s2.set_leds(s2#OFF, s2#RED, s2#OFF, s2#NO_CHANGE)
ReadLine
if (LeftLine == 0 and RightLine == 0)
s2.set_leds(s2#OFF, s2#OFF, s2#OFF, s2#NO_CHANGE)
MotorSet(128, 128, 0)
elseif (LeftLine == 0 and RightLine == 1)
s2.set_leds(s2#OFF, s2#OFF, s2#GREEN, s2#NO_CHANGE)
MotorSet(84, 128, 0)
elseif (LeftLine == 1 and RightLine == 0)
s2.set_leds(s2#GREEN, s2#OFF, s2#OFF, s2#NO_CHANGE)
MotorSet(128, 84, 0)
elseif (LeftLine == 1 and RightLine == 1)
s2.set_leds(s2#GREEN, s2#OFF, s2#GREEN, s2#NO_CHANGE)
s2.start_timer (2)
if (Flag_green)
MotorSet(-128, 128, 4000)
Flag_green := false
else
MotorSet(-128, -128, 2000)
MotorSet(128, -128, 2000)
Flag_green := true
else
MotorSet(0, 0, 0)
Sign In or Register to comment.