My robot keep acting awkwardly....

HarryLee

Member
Here is the video I taped: http://www.youtube.com/watch?v=5F7eTJjFdpo
1#: I didn't write anything about moving backward...
2#: I checked my sub"drive forward" is working correctly when I input it separately...
3#:Here is the formula I made from taking the data from Sharp(Ir sensor) y=-8.8x + 238 (y=reading data) (x=distance)
(I am new to programming... can somebody actually look at it and tell me is it work correctly in my robot?) Thanks so much ...:)

Here is the code:
Code:
' This is the program ued to make the video on letsmakerobots.com/start. The program is optimized to make the robot look alive on video, and so it is made to stop up and look around at objects. and if it finds something close (like the camera ;) it stops and animates.
' The program is not optimized for abject avoidance or anything else like that.
' Makes a cute robot, though :) And if you made the robot just like the instructions, it should be ready to just download and run!

wait 5 ' just to give you time to put it down on the table

symbol way = bit0 ' which direction is head moving
symbol neck = b1 ' value for servo turning Sharp
symbol maxdistvalue = b2 ' max value for distance when panning
symbol maxdistvaluepos = b3 ' position where maxdistvalue is achieved
symbol dist = b4 ' Value returning from Sharp. non liniar, closer is higher, 35 is 40cm away, but from 100-150 (10 CM) it starts to count dramatically down
symbol lastturn = b5
symbol sharppin = 0
symbol servocenter = b6
symbol soundvar = b7
symbol servopin = 0

gosub lightoff

servocenter = 150

main:
servo servopin, servocenter ' look forward
gosub distance ' measure distance
	
	if dist > 60 and dist < 80 then ' hey, something ahead, turn a little to the side we where turning last time
	servocenter = 150
	gosub lighton
	if lastturn > 175 then
		gosub driveright 
				inc lastturn
		else
			gosub driveleft
				inc lastturn
			end if
			gosub lightoff
			goto main
		end if	
	
if dist >80 and dist < 115 then ' Hey, lets have a look around
servocenter = 150

gosub totalhalt ' stop
	pause 500 ' make a natural stop
	gosub choser ' enter subroutine that decides where to look and stuff
	goto main ' now return and see if all is OK, and we can proceed
endif 

if dist >= 115 then ' danger: Something is close
servocenter = 150
gosub totalhalt ' stop
gosub hard_chose
	goto main
endif

if dist < 90 then ' no danger. we can proceed
gosub driveforward ' drive forward
endif

if dist < 40 then ' Absolutely no danger. Look around while drivin
gosub driveforward
	if way = 1 then
	servocenter = servocenter +1 max 225
	else
	servocenter = servocenter -1 min 75
	end if
	if servocenter = 225  or servocenter = 75 then 
		inc way
		
	end if

endif

goto main ' start over


choser:

 maxdistvalue = 0 maxdistvaluepos = servocenter ' initialize everything

for neck = servocenter to 75 step -1
	servo servopin, neck ' turn neck
	pause 30 ' allow servo to turn
	gosub distance ' measure distance
		if dist >70 then
			'pause 50 ' move hed slower on closer objects
			'neck = neck - 3 min 75
		end if
	if dist >  maxdistvalue then
			maxdistvalue = dist
			maxdistvaluepos = neck
	end if
next neck
servo servopin, maxdistvaluepos wait 1
servo servopin, servocenter pause 50 ' turn neck
	
for neck = servocenter to 225 step 1
	servo servopin, neck ' turn neck
	pause 30 ' allow servo to turn
	gosub distance ' measure distance
		if dist >70 then
			'pause 50 ' move hed slower on closer objects
			'neck = neck + 3 max 225
		end if
	if dist >  maxdistvalue then
			maxdistvalue = dist
			maxdistvaluepos = neck
	end if
next neck

'********
' maxdistvaluepos  is now where the closest object is
'here should be inserted: If really close, have a look, pause and move on
if maxdistvalue > 121 then
	servo servopin, maxdistvaluepos
	wait 2
	for soundvar = 120 to maxdistvalue
		sound 4, (soundvar, 1) pause 1
		sound 6, (soundvar, 1) pause 1
				sound 1, (soundvar, 1) pause 1
				sound 1, (soundvar, 1) pause 1
	next soundvar
end if
'wait 1 ' look back at the closest object


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'calculate opposite:
if maxdistvaluepos <= 150 then
	neck = 150 - maxdistvaluepos
	neck = neck +150
	
	servo servopin, neck ' turn head to opposite
	maxdistvaluepos = neck
	pause 200
	'start turning body and all
	for neck = maxdistvaluepos to 150 step -1
		servo servopin, neck ' turn neck while turning body
		gosub driveright pause 3 lastturn = 175
	next neck
	
	dontstopturningA: ' keep turning till we are frr in front
		gosub distance
			if dist > 60 then goto dontstopturninga
else
	maxdistvaluepos = maxdistvaluepos -150 
	neck = 150- maxdistvaluepos
	
	servo servopin, neck ' turn head to opposite
	maxdistvaluepos = neck
	pause 200
	'start turning body and all
	for neck = maxdistvaluepos to 150 step 1
		servo servopin, neck ' turn neck while turning body
		gosub driveleft pause 3 lastturn = 0
	next neck
	
	dontstopturningB:
		gosub distance
			if dist > 60 then goto dontstopturningB
	
end if


'servo servopin, neck wait 10 ' look away from closest


return



hard_chose:
high 1
servo servopin, 75
wait 1
gosub distance
maxdistvalue = dist

servo servopin, 225
wait 1
gosub distance
	servo servopin, 150
	wait 1
	
	if  maxdistvalue > dist then
	
	turnon:
	gosub driveleft
	gosub distance
	if dist > 50 then goto turnon
	
	else
		
		turnoff:
		gosub driveright
			gosub distance
				if dist > 50 then goto turnoff
			end if
	low 1
return



distance:
readadc sharppin, dist
return

totalhalt:
low 4 low 5 low 6 low 7
return

driveright:
'servo servopin, alltv
high 4 low 5  ' h
high 7 low 6' v
return

driveleft:
'servo servopin, allth
high 5 low 4  ' h
high 6 low 7  ' v
return

driveforward:
high 5 low 4  ' h
high 7 low 6  ' v
return


lightoff:
high 2
return

lighton:
low 2
return
View attachment robot new program.bas
 
Last edited by a moderator:

bfgstew

Senior Member
Hi Harry

First thing is if you post your code use the code tags, this lets it be viewed more easily, and takes up less space on the post.

OK, the guys are going to need more info before they can help you, what Picaxe chip are you using? What robot is it? A picture (hi res) of your circuit board, schematic diagram? The more info, the easier help comes.
Hope you get it sorted.



Code:
wait 5 ' just to give you time to put it down on the table,
 
symbol way = bit0 ' which direction is head moving
 symbol neck = b1 ' value for servo turning Sharp
 symbol maxdistvalue = b2 ' max value for distance when panning
 symbol maxdistvaluepos = b3 ' position where maxdistvalue is achieved
 symbol dist = b4 ' Value returning from Sharp. non liniar, closer is higher, 35 is 40cm away, but from 100-150 (10 CM) it starts to count dramatically down
 symbol lastturn = b5
 symbol sharppin = 0
 symbol servocenter = b6
 symbol soundvar = b7
 symbol servopin = 0
 
gosub lightoff
 
servocenter = 150
 
main:
 servo servopin, servocenter ' look forward
 gosub distance ' measure distance

 if dist > 60 and dist < 80 then ' hey, something ahead, turn a little to the side we where turning last time
 servocenter = 150
 gosub lighton
 if lastturn > 175 then
 gosub driveright 
inc lastturn
 else
 gosub driveleft
 inc lastturn
 end if
 gosub lightoff
 goto main
 end if 

if dist >80 and dist < 115 then ' Hey, lets have a look around
 servocenter = 150
 
gosub totalhalt ' stop
 pause 500 ' make a natural stop
 gosub choser ' enter subroutine that decides where to look and stuff
 goto main ' now return and see if all is OK, and we can proceed
 endif 

if dist >= 115 then ' danger: Something is close
 servocenter = 150
 gosub totalhalt ' stop
 gosub hard_chose
 goto main
 endif
 
if dist < 90 then ' no danger. we can proceed
 gosub driveforward ' drive forward
 endif
 
if dist < 40 then ' Absolutely no danger. Look around while drivin
 gosub driveforward
 if way = 1 then
 servocenter = servocenter +1 max 225
 else
 servocenter = servocenter -1 min 75
 end if
 if servocenter = 225 or servocenter = 75 then 
inc way

 end if
 
endif
 
goto main ' start over
 

choser:
 
maxdistvalue = 0 maxdistvaluepos = servocenter ' initialize everything
 
for neck = servocenter to 75 step -1
 servo servopin, neck ' turn neck
 pause 30 ' allow servo to turn
 gosub distance ' measure distance
 if dist >70 then
 'pause 50 ' move hed slower on closer objects
 'neck = neck - 3 min 75
 end if
 if dist > maxdistvalue then
 maxdistvalue = dist
 maxdistvaluepos = neck
 end if
 next neck
 servo servopin, maxdistvaluepos wait 1
 servo servopin, servocenter pause 50 ' turn neck

 for neck = servocenter to 225 step 1
 servo servopin, neck ' turn neck
 pause 30 ' allow servo to turn
 gosub distance ' measure distance
 if dist >70 then
 'pause 50 ' move hed slower on closer objects
 'neck = neck + 3 max 225
 end if
 if dist > maxdistvalue then
 maxdistvalue = dist
 maxdistvaluepos = neck
 end if
 next neck
 
'********
 ' maxdistvaluepos is now where the closest object is
 'here should be inserted: If really close, have a look, pause and move on
 if maxdistvalue > 121 then
 servo servopin, maxdistvaluepos
 wait 2
 for soundvar = 120 to maxdistvalue
 sound 4, (soundvar, 1) pause 1
 sound 6, (soundvar, 1) pause 1
 sound 1, (soundvar, 1) pause 1
 sound 1, (soundvar, 1) pause 1
 next soundvar
 end if
 'wait 1 ' look back at the closest object
 

'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''
 'calculate opposite:
 if maxdistvaluepos <= 150 then
 neck = 150 - maxdistvaluepos
 neck = neck +150

 servo servopin, neck ' turn head to opposite
 maxdistvaluepos = neck
 pause 200
 'start turning body and all
 for neck = maxdistvaluepos to 150 step -1
 servo servopin, neck ' turn neck while turning body
 gosub driveright pause 3 lastturn = 175
 next neck

 dontstopturningA: ' keep turning till we are frr in front
 gosub distance
 if dist > 60 then goto dontstopturninga
 else
 maxdistvaluepos = maxdistvaluepos -150 
neck = 150- maxdistvaluepos

 servo servopin, neck ' turn head to opposite
 maxdistvaluepos = neck
 pause 200
 'start turning body and all
 for neck = maxdistvaluepos to 150 step 1
 servo servopin, neck ' turn neck while turning body
 gosub driveleft pause 3 lastturn = 0
 next neck

 dontstopturningB:
 gosub distance
 if dist > 60 then goto dontstopturningB

 end if
 

'servo servopin, neck wait 10 ' look away from closest
 

return
 


hard_chose:
 high 1
 servo servopin, 75
 wait 1
 gosub distance
 maxdistvalue = dist
 
servo servopin, 225
 wait 1
 gosub distance
 servo servopin, 150
 wait 1

 if maxdistvalue > dist then

 turnon:
 gosub driveleft
 gosub distance
 if dist > 50 then goto turnon

 else

 turnoff:
 gosub driveright
 gosub distance
 if dist > 50 then goto turnoff
 end if
 low 1
 return
 


distance:
 readadc sharppin, dist
 return
 
totalhalt:
 low 4 low 5 low 6 low 7
 return
 
driveright:
 'servo servopin, alltv
 high 4 low 5 ' h
 high 7 low 6' v
 return
 
driveleft:
 'servo servopin, allth
 high 5 low 4 ' h
 high 6 low 7 ' v
 return
 
driveforward:
 high 5 low 4 ' h
 high 7 low 6 ' v
 return
 

lightoff:
 high 2
 return
 
lighton:
 low 2
 return
 

bfgstew

Senior Member
Harry, when you say 'acting akwardly' do you mean its jumping and jerking instead of running smoothly?

As far as I can tell, and that aint much, I would start to add a few more pauses in the routine, also on the readac part, add a for/next loop, this will give the robot chance to scan its enviroment.

Code:
distance:
 readadc sharppin, dist
 return
Code:
distance:
for b33 = 1 to 20
readadc sharppin, dist
next b33
return
This will let it take 20 readings rather then 1, try it and see, if it doesn't work remove it.
 

HarryLee

Member
I think I didn't write anything about moving backward... But as you see in the video ... It moved backward ....
 

bfgstew

Senior Member
Harry, when you built this as the link shows, did you go through the simple step by step stages of programming the robot to check all the systems worked correctly? Especially the motor drives?
 

SAborn

Senior Member
If you watch the video of the robot, on first power up it travels in reverse in a wonky motion then turns around, looking through the code i can not see where or how it can enter into a reverse motion, as there is no reverse sequence to the motor drivers, only a left, right, and forward sequence.
I agree its a bit strange??
 

rossko57

Senior Member
If you watch the video of the robot, on first power up it travels in reverse in a wonky motion then turns around, looking through the code i can not see where or how it can enter into a reverse motion, as there is no reverse sequence to the motor drivers, only a left, right, and forward sequence.
I agree its a bit strange??
The program opens with a long wait, then eventually makes sure the light is off. There doesn't seem to be any code to ensure the drive is set to 'stop' during that long wait.
I would have thought the very first things to do are setting up drive-off light-off etc before the wait.
 

SAborn

Senior Member
There has been several verions of the program that is used posted and to my understanding it is this program that is in use

Code:
symbol way = 			 b0 			' which direction is head moving

symbol neck =			 b1 			' value for servo turning Sharp
symbol maxdistvalue =		 b2 			' max value for distance when panning
symbol maxdistvaluepos = 	 b3 			' position where maxdistvalue is achieved
symbol dist = 			 b4 			' Value returning from Sharp. non liniar, closer is higher, 35 is 40cm away, but from 100-150 (10 CM) it starts to count dramatically down
symbol lastturn = 		 b5
symbol servocenter = 		 b6
symbol soundvar = 		 b7

symbol servopin = 		 0
symbol sharppin = 		 0

servocenter = 			 150

wait 					 5			' just to give you time to put it down on the table

gosub lightoff




'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



main:



	servo servopin, servocenter 				' look forward
	gosub distance 						' measure distance
	
	if dist > 60 and dist < 80 then 			' hey, something ahead, turn a little to the side we where turning last time
	servocenter = 150
	gosub lighton
	
	if lastturn > 175 then
	gosub driveright 
	
	inc lastturn
	
	else
	gosub driveleft
	
	inc lastturn
	end if
	
	gosub lightoff
	goto main
	end if
	
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%		
	
	if dist >80 and dist < 115 then 			' Hey, lets have a look around
	servocenter = 150
	gosub totalhalt 						' stop
	pause 500 							' make a natural stop
	gosub choser 						' enter subroutine that decides where to look and stuff
	goto main 							' now return and see if all is OK, and we can proceed
	endif 
	
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	

	if dist >= 115 then 					' danger: Something is close
	servocenter = 150
	gosub totalhalt 						' stop
	gosub hard_chose
	goto main
	endif
	
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%	
	

	if dist < 90 then 					' no danger. we can proceed
	gosub driveforward 					' drive forward
	endif

	if dist < 40 then 					' Absolutely no danger. Look around while drivin
	
	if way = 1 then
	servocenter = servocenter +1 max 225
	
	else
	servocenter = servocenter -1 min 75
	end if
	if servocenter = 225  or servocenter = 75 then 
	inc way
		
	endif
	endif

	goto main 							' start over

'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


choser:

 	maxdistvalue = 0 maxdistvaluepos = servocenter ' initialize everything



'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	for neck = servocenter to 75 step -1
	servo servopin, neck 					' turn neck
	pause 30 							' allow servo to turn
	gosub distance 						' measure distance
	if dist >70 then
	
	'pause 50							' move hed slower on closer objects
	'neck = neck - 3 min 75
	
	end if
	
	if dist >  maxdistvalue then
	maxdistvalue = dist
	maxdistvaluepos = neck
	end if
	next neck
	
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	servo servopin, maxdistvaluepos wait 1
	servo servopin, servocenter pause 50 	' turn neck

'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	for neck = servocenter to 225 step 1
	servo servopin, neck 				' turn neck
	pause 30 						' allow servo to turn
	gosub distance 					' measure distance

	if dist >70 then
	
	'pause 50 						' move hed slower on closer objects							
	'neck = neck + 3 max 225
	
	end if
	
	if dist >  maxdistvalue then
	maxdistvalue = dist
	maxdistvaluepos = neck
	end if
	next neck

'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



'********
' maxdistvaluepos  is now where the closest object is
'here should be inserted: If really close, have a look, pause and move on


	if maxdistvalue > 121 then
	servo servopin, maxdistvaluepos
	
	wait 2
	for soundvar = 120 to maxdistvalue
	sound 4, (soundvar, 1) pause 1
	sound 6, (soundvar, 1) pause 1
	sound 1, (soundvar, 1) pause 1
	sound 1, (soundvar, 1) pause 1
	next soundvar
	end if
	
	
	'wait 1 						' look back at the closest object


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
								'calculate opposite:


	if maxdistvaluepos <= 150 then
	neck = 150 - maxdistvaluepos
	neck = neck +150
	
	servo servopin, neck 				' turn head to opposite
	maxdistvaluepos = neck
	pause 200	


'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

								'start turning body and all
	for neck = maxdistvaluepos to 150 step -1
	servo servopin, neck 				' turn neck while turning body
	gosub driveright 
	pause 3 
	lastturn = 175
	next neck


'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	
	
dontstopturningA: 
								' keep turning till we are frr in front
	gosub distance
	if dist > 60 then goto dontstopturninga
	
	else
	maxdistvaluepos = maxdistvaluepos -150 
	neck = 150- maxdistvaluepos
	
	servo servopin, neck 				' turn head to opposite
	maxdistvaluepos = neck
	pause 200

'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

								'start turning body and all
	for neck = maxdistvaluepos to 150 step 1
	servo servopin, neck 				' turn neck while turning body
	gosub driveleft 
	pause 3 
	lastturn = 0
	next neck
	
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	
	
dontstopturningB:

	gosub distance
	if dist > 60 then goto dontstopturningB
	end if


	'servo servopin, neck wait 10               ' look away from closest

	return



hard_chose:

	high 1
	servo servopin, 75
	wait 1
	gosub distance
	maxdistvalue = dist

	servo servopin, 225
	wait 1
	gosub distance
	servo servopin, 150
	wait 1
	
	if  maxdistvalue > dist then
	
	
turnon:


	gosub driveleft
	gosub distance
	if dist > 50 then goto turnon
	
	else
	
			
turnoff:


	gosub driveright
	gosub distance
	if dist > 50 then goto turnoff
	end if
	low 1
	return



distance:

	readadc sharppin, dist
	return


totalhalt:


	low 4 low 5 low 6 low 7
	return


driveright:

					'servo servopin, alltv
	high 4 low 5  		' h
	high 7 low 6		' v
	return
	

driveleft:

					'servo servopin, allth
	high 5 low 4  		' h
	high 6 low 7  		' v
	return


driveforward:

	high 5 low 4  		' h
	high 7 low 6  		' v
	return


lightoff:

	high 2
	return


lighton:

	low 2
	return
Its the last few lines of code that controls the motor so how can it run backwards, as in the video.
 

hippy

Ex-Staff (retired)
Its the last few lines of code that controls the motor so how can it run backwards, as in the video.
It does seem mysterious.

I was wondering if it's not actually moving backwards but turning rapidly left and right with friction and/or other mechanical effects causing it to waggle with a resulting overall backwards motion.

There are few delays in the code, motors altering perhaps every few milliseconds, and it's a very dynamic system with no way to tell what it thinks it is seeing or what it intends doing, so it is difficult to tell exactly what is happening or to diagnose the problem.

Two of the motor control pins seem to also be used as sound outputs. Not sure if that's causing any problems but it could -

sound 4, (soundvar, 1) pause 1
sound 6, (soundvar, 1) pause 1
 

Buzby

Senior Member
Are the motors wired to the H-Bridges correctly ?.

If the motors are accidentally cross-wired between the two 'halves' of the LM293D they will behave very strangely.
 

SAborn

Senior Member
I think hippy might have found the problem, a clear head and a re-read of the code might solve the problem, one might say cant see the forest from the trees, and a fresh set of eyes can make a big difference.

Glade Hippy had his morning coffee.
 

SAborn

Senior Member
Try changing the code for sound from this

Code:
	if maxdistvalue > 121 then
	servo servopin, maxdistvaluepos
	
	wait 2
	for soundvar = 120 to maxdistvalue
	sound 4, (soundvar, 1) pause 1
	sound 6, (soundvar, 1) pause 1
	sound 1, (soundvar, 1) pause 1
	sound 1, (soundvar, 1) pause 1
	next soundvar
	end if
To this

Code:
	if maxdistvalue > 121 then
	servo servopin, maxdistvaluepos
	
	wait 2
	for soundvar = 120 to maxdistvalue
	sound 1, (soundvar, 1) pause 1
	sound 1, (soundvar, 1) pause 1
	sound 1, (soundvar, 1) pause 1
	sound 1, (soundvar, 1) pause 1
	next soundvar
	end if
Which is just changing the sound 4 and sound 6 to be all sound 1.
That should fix your problem.
 

HarryLee

Member
Hi SAborn, it does fixed the problem, I am uploading the video... even it didn't turn backward now .... I don't why it hang around when there is nothing next to it....
 

SAborn

Senior Member
Yes it do look as the code is not quite correct in the way it looks around for so long before it makes a move.
You are slowly getting there though.
 
Top