BOT122 wheel encoder

Tony P

Member
I have just received my Picaxe 20X2 Microbot and all 'add on' packs and I am having great fun learning the best way to program for each sensor etc.

My problem is the BOT122 wheel encoders. I can read the revolutions using the COUNT command as suggested in the datasheet but how do I apply this to a program?

All the COUNT does is move the robot forwards for a set amount of time and then display the result, I would like to be able to count the revolutions and stop at a set distance

etc.
 

Dippy

Moderator
I don't know the details so....

You could use an interrupt to increment a variable on every edge of encoder pulse.
This is available on a number of PICAXEs and has a mask option if needed.
When your main routine detects the set amount has been reached then do whatever.
A simple IF statement for that may suffice for that.

PS> I've never used the BOT122 sensors so I'm only thinking out loud.
Read Manual 2 hintsetup etc.

You could practice with a simple loop and IF pin.status test to see if it would work.
 

Tony P

Member
Thank you Dippy, I have tried the following:

Code:
#picaxe 20X2
	FORWARD A				
	setint %000010000,%000010000	;ENCODER ON C.4

MAIN:
DEBUG
	FORWARD A		
		IF W1>=20 THEN 
			SOUND B.2,(50,100)
			HALT A
			PAUSE 1000
			LET W1=0		;RESET THE COUNT
		ENDIF
	GOTO MAIN


	
INTERRUPT:

	LET W1 = W1 +1
	setint %000010000,%000010000
	RETURN
It seems to work OK apart from a glitch where, if the white mark for the encoder to read happens to stop beneath the encoder, the loop stops but I will persevere. (that is why I have added a FORWARD A command before the MAIN loop.)
 

boriz

Senior Member
Well I did a search and found this. Unfortunately the so called 'datasheet' is next to useless. It also contains a link to code examples which don't exist :( I'm sure someone will know where to find the relevant information, but Techsupplies aren't making it easy. In the mean time...

This line from the PDF: "The bottom of the worm gear (attached to the motor) must be carefully modified so that it is half black and half white in colour.", implies a simple single phase reflective sensor. I've no idea if it has built in amplification, hysteresis, and/or TTL signal levels. But assuming it does...

Interrupt might not be the best way to go. I expect the worm gear will be rotating quite fast, maybe 2000RPM (educated guess in the absence of data). That's a HIGH logic change followed by a LOW logic change, roughly every 500uS. A better strategy might be to begin a movement, count the pulses and stop the movement after a certain number of pulses. With such rapid pulses, you really don't want to be doing much else other than counting pulses. An interrupt every 500uS could mess up your main routine and at the very least, occasionally make your count data inaccurate or out-of-date. Especially if you use any blocking commands (Like SOUND) which do not get interrupted.

So something like this:
Code:
'Pseudocode
move_forward:
   Forward motor1,motor2
   do
      pulsin encoder_pin,1,word_variable
      inc position_counter
   loop until position_counter>distance_to_travel or word_variable>50000
   stop motor1,motor2
return
PULSIN is a blocking command which has a timeout of about half a second if no pulses are detected. It has a resolution (@4MHz) of 10uS, so should have no problem catching a 500uS pulse. The Word_variable will contain the duration of the pulse, but for this application that information is redundant. Before entering the subroutine, set the variable Distance_to_travel to the number of pulses you want for the distance you want. The "or word_variable>50000" will exit the loop if for some reason the motor stops turning before the count is up. Similar subroutines can be written for Backward, Left, Right, etc..

I've had to make many assumptions, and they could be wrong, but I think the idea is sound.
 

boriz

Senior Member
Interrupt could be used for object/collision detection.

Hmm. Thinking about it. The pulse duration information could be used to detect 'motor turning too slow' events. And if the loop does exit with Word_variable>50000, it could be flagged as an error condition. But that's another story :)
 

hia

New Member
Hi, I recently bought a bot120 for my project in school but it seems like both of the wheels don't operate in the same speed. My right wheel always run faster than the left one.
Does anyone here know how to fix that problem? I'd be very grateful for that. Thanks in advance :D
 

Technical

Technical Support
Staff member
Make sure:

1) both gears are well greased. It makes a huge difference.
2) if one is much slower it's gears are probably not aligned correctly. Make sure the motor is correctly positioned in the plastic case, try 'wobbling' the motor forward/backwards to get the best position
 
Top