timing

We need our one sensor to continue the loop for a certain amount of time, but we having a hard time figuring out what to do with the code.

We just manipulated the sharp sensor code from the autonomous code from lynxmotion.

if (ir_center < 10) then
sound 9, [100\1175]
LSpeed = (LSpeed - ir_center) min Lspinspeed ;
RSpeed = (RSpeed - ir_center) min Rspinspeed
endif

all we did was change the greater than or less than sign. This sensor will back up the robot when it does not see ground. We want it to back up and spin for around 3-5 seconds. Help would be greatly appreciated.

HI it would help to have some context of what you are doing in the rest of the program. For now I will guess that this is something like a rover and maybe the code is similar to the 4wdauto1.bas that James Frye did as part of the tutorial… If I understand correctly is that when the center sensor gets to some setting you want the rover to do something specific for a certain amount of time. Maybe something like:
Please note: I did this on the fly so there may be and probably are some problems…

...
LSpeedBU var word		; backup speed
RSpeedBU var word
BackupCnt var word		; how many times to backup....
BackupCnt = 0

main
	gosub sensor_check
	
	if BackupCnt then
		LSpeed = LSPeedBU;
		RSpeed = RSpeedBU;
		BackupCnt = BackupCnt - 1
	else
		LSpeed = (LSpeed - 10) min minspeed
		RSpeed = (RSpeed - 10) min minspeed
	
		LSpeed = (LSpeed + ir_left) max maxspeed
		RSpeed = (RSpeed + ir_right) max maxspeed
	
		if (ir_center < 10) then
			sound 9, [100\1175]
			LSpeed = (LSpeed - ir_center) min Lspinspeed ;
			RSpeed = (RSpeed - ir_center) min Rspinspeed
			LSpeedBU = LSpeed
			RSpeedBU = RSpeed
			BackupCnt = 200		; we loop about 50 times per second so close to 4 seconds
		endif
	endif
	' Send out the servo pulses	
	pulsout 0,(LSpeed*2)	' Left Sabertooth channel.
	pulsout 1,(RSpeed*2)	' Right Sabertooth channel.
	pause 20
goto main

Again I don’t know your context so don’t know how else your code sets the speeds. also this only goes one direction… Could be modified to for example set backup count and speeds, do that and when that count goes to zero then have a saved set of spin speeds and count which you then compare against. Could also enhance this with multiple step sequences, but if I were you, I would try simple stuff first.

Good Luck and I hope that helps
Kurt

thank you very much and that does help. What we are doing is having the robot driving around autonomously collecting data. We are using the impression that we are on the moon. This center sensor will detect a cliff and back up or spin to avoid the cliff. We have wireless mote sensor that are attached to the bot board that when they detect a signal we have the robot stop. And yes we are just modifying the code that Jim Frye wrote.

Here is the full code. Your code helped a lot but we had to make some modifictions. In the else statement we removed those sensor codes because the front sensors would not function. We are however having problems with the speed as it backs up. Before when we had it backup without time we had one side spinning faster then the other so it would rotate away from a cliff. Now using your code the speeds will not change when we change the values.

[code]temp var byte
filter var word(10)
ir_right var word
ir_left var word
ir_rear var word
ir_center var word
mote var word
LSpeedBU var word
RSpeedBU var word
BackupCnt var word
BackupCnt = 0

LSpeed var word
RSpeed var word

minspeed con 1650
maxspeed con 1350
stopspeed con 1500
Lspinspeed con 2200
Rspinspeed con 1800

LSpeed = 1500
RSpeed = 1500

low p0
low p1

sound 9, [100\880, 100\988, 100\1046, 100\1175]

main
gosub sensor_check

; Numbers lower than 1500 result in forward direction.
; Numbers higher than 1500 result in reverse direction.

if BackupCnt then
LSpeed = LSPeedBU;
RSpeed = RSpeedBU;
BackupCnt = BackupCnt - 1

else

  if (ir_center < 10) then
     sound 9, [100\1175]
     LSpeed = (LSpeed + ir_center) min Lspinspeed
     RSpeed = (RSpeed + ir_center) min Rspinspeed
     LSpeedBU = LSpeed
     RSpeedBU = RSpeed
     BackupCnt = 50      ; we loop about 50 times per second so close to 4 seconds
  endif

endif

LSpeed = (LSpeed - 10) min maxspeed ;accelerates the motors
RSpeed = (RSpeed - 10) min maxspeed

LSpeed = (LSpeed + ir_left) max minspeed ;when something is detected, this decelerates the opposite side
RSpeed = (RSpeed + ir_right) max minspeed

if (ir_rear > 10) then
LSpeed = (LSpeed - ir_rear) min maxspeed ;if something is detected behind the robot, accelerates both sides
RSpeed = (RSpeed - ir_rear) min maxspeed
endif

if (mote > 5) then
LSpeed = (LSpeed - mote) min stopspeed ;
RSpeed = (RSpeed - mote) min stopspeed
endif

; Send out the servo pulses
pulsout 0,(LSpeed2) ; Left Sabertooth channel.
pulsout 1,(RSpeed
2) ; Right Sabertooth channel.
pause 20

goto main

sensor_check

for temp = 0 to 9
adin 17, filter(temp)
next
ir_right = 0
for temp = 0 to 9
ir_right = ir_right + filter(temp)
next
ir_right = ir_right / 50

for temp = 0 to 9
adin 18, filter(temp)
next
ir_left = 0
for temp = 0 to 9
ir_left = ir_left + filter(temp)
next
ir_left = ir_left / 50

for temp = 0 to 9
adin 19, filter(temp)
next
ir_rear = 0
for temp = 0 to 9
ir_rear = ir_rear + filter(temp)
next
ir_rear = ir_rear / 50

for temp = 0 to 9
adin 3, filter(temp)
next
mote = 0
for temp = 0 to 5
mote = mote + filter(temp)
next
mote = mote / 20

for temp = 0 to 9
adin 2, filter(temp)
next
ir_center = 0
for temp = 0 to 9
ir_center = ir_center + filter(temp)
next
ir_center = ir_center / 75

serout s_out,i38400,"ir_right - ", dec ir_right, " ir_left - ", dec ir_left, " ir_rear - ", dec ir_rear, "LSpeed - ", dec LSpeed, " RSpeed - ", dec RSpeed, 13]

return[/code]

Hi,

Yes I would expect that you would need to experiment and that I was simply providing a possible first step of showing that you could introduce state variables and do something different with different states… There are many different ways you can try out and see what works for you. I don’t mind helping out a little and making suggestions, but I don’t have the energy to flesh it out completely.

I did take a quick look through your posted code and I do think it has issues on how the ifs are nested or not nested… I think it should probably be closer to:

[code]main
gosub sensor_check

; Numbers lower than 1500 result in forward direction.
; Numbers higher than 1500 result in reverse direction.

; we are in backup mode
if BackupCnt then
	LSpeed = LSPeedBU;
	RSpeed = RSpeedBU;
	BackupCnt = BackupCnt - 1
else
	; see if in the center we are getting too close  	
	if (ir_center < 10) then
 		sound 9, [100\1175]
 		LSpeed = (LSpeed + ir_center) min Lspinspeed
 		RSpeed = (RSpeed + ir_center) min Rspinspeed
 		LSpeedBU = LSpeed
 		RSpeedBU = RSpeed
 		BackupCnt = 50      ; we loop about 50 times per second so close to 4 seconds

	else
		LSpeed = (LSpeed - 10) min maxspeed   ;accelerates the motors
		RSpeed = (RSpeed - 10) min maxspeed
		
		LSpeed = (LSpeed + ir_left) max minspeed   ;when something is detected, this decelerates the opposite side
		RSpeed = (RSpeed + ir_right) max minspeed
		
		if (ir_rear > 10) then
			LSpeed = (LSpeed - ir_rear) min maxspeed   ;if something is detected behind the robot, accelerates both sides
			RSpeed = (RSpeed - ir_rear) min maxspeed
		endif
			
		if (mote > 5) then
			LSpeed = (LSpeed - mote) min stopspeed   ;
			RSpeed = (RSpeed - mote) min stopspeed
		endif
	
	endif
endif	

; Send out the servo pulses   
pulsout 0,(LSpeed*2)   ; Left Sabertooth channel.
pulsout 1,(RSpeed*2)   ; Right Sabertooth channel.
pause 20

goto main[/code]

But again you said earlier you wanted to backup and then spin. so for case like that I would have expected you might need to expand on it to a multistep process. Something like:

[code]main
gosub sensor_check

; Numbers lower than 1500 result in forward direction.
; Numbers higher than 1500 result in reverse direction.

; we are in backup mode
if BackupCnt then
	LSpeed = LSPeedBU;
	RSpeed = RSpeedBU;
	BackupCnt = BackupCnt - 1
	if BackupCnt = 0 then
		SpinCnt = 50 ; change to whatever count that works...
	endif
elseif SpinCnt
	LSpeed = 1800	; could set anywhich way you want...
	RSpeed = 1200	; both wheels going opposit directions will turn...
	SpinCnt = SpinCnt - 1
else
	; see if in the center we are getting too close  	
	if (ir_center < 10) then
 		sound 9, [100\1175]
 		LSpeed = (LSpeed + ir_center) min Lspinspeed
 		RSpeed = (RSpeed + ir_center) min Rspinspeed
 		LSpeedBU = LSpeed	; could also hard code both...
 		RSpeedBU = RSpeed
 		BackupCnt = 50      ; we loop about 50 times per second so close to 4 seconds

	else
		LSpeed = (LSpeed - 10) min maxspeed   ;accelerates the motors
		RSpeed = (RSpeed - 10) min maxspeed
		
		LSpeed = (LSpeed + ir_left) max minspeed   ;when something is detected, this decelerates the opposite side
		RSpeed = (RSpeed + ir_right) max minspeed
		
		if (ir_rear > 10) then
			LSpeed = (LSpeed - ir_rear) min maxspeed   ;if something is detected behind the robot, accelerates both sides
			RSpeed = (RSpeed - ir_rear) min maxspeed
		endif
			
		if (mote > 5) then
			LSpeed = (LSpeed - mote) min stopspeed   ;
			RSpeed = (RSpeed - mote) min stopspeed
		endif
	
	endif
endif	

; Send out the servo pulses   
pulsout 0,(LSpeed*2)   ; Left Sabertooth channel.
pulsout 1,(RSpeed*2)   ; Right Sabertooth channel.
pause 20

goto main

[/code]
Note you need to define SpinCount and init to 0 like backup count…

Have fun
Kurt

Thanks a lot for your help. I will try these out tomorrow and let you know how they work out.