Two sensor line sensing

stan74

Senior Member
hippy.I posted code that put 128 pixels into 16 bytes and suggested checking 8 or 16 bits at a time. erco has built several line followers and from only 1 real test can see the advantage of edge detection. The track I used was irregular width so the centring algo I used performed poorly. v2 will have 2 sensors and make the servos limited speed range proportional ie steer to correct, checking in a loop and increase speed each loop. Probably search how other people made theirs first. Why reinvent the wheel? IR detectors don't see black felt marker pen lines so don't waste time trying to calibrate.
 

erco

Senior Member
IR detectors don't see black felt marker pen lines so don't waste time trying to calibrate.
IR and visible light are very different, for eyes and sensors. Also various sensors see black/white lines very differently. I have tested two sides of posterboard (one glossy, one matte) and they give very different results. Black shiny electrical tape reflects more than black marker does. Different marker ink matters. It's wise to test sensors and specific courses for compatability and adjust or play to your sensor's strength.

Ambient light can be devastating too. Parallax had an outdoor line following event using shiny aluminum tape stuck down to the black parking lot. Sunlight wreaked havoc with many bots' sensors and most could not compete. Nonetheless, the scale of the course was breathtaking. IIRC it was to commemorate the 40th anniversary of the 555 timer.

http://forums.parallax.com/discussion/comment/1091052/#Comment_1091052

 

stan74

Senior Member
erco summed up my thoughts about a line follower when I saw his bot in a pizza box.
Doesn't need space.
It works.
Minimal input hardware hence less code.
The idea of 128 sensors was appealing because of price,size and serial out and the intention of following a thin scribble line. All on a smallish scale.
A circuit of erco's series photo cell idea be useful. open air aluminium tape hmmm..thinks could detect that with 2 sensors. Not seen that. Follow the mains lead.Why not?
 

edmunds

Senior Member
erco summed up my thoughts about a line follower when I saw his bot in a pizza box.
Doesn't need space.
It works.
Minimal input hardware hence less code.
The idea of 128 sensors was appealing because of price,size and serial out and the intention of following a thin scribble line. All on a smallish scale.
A circuit of erco's series photo cell idea be useful. open air aluminium tape hmmm..thinks could detect that with 2 sensors. Not seen that. Follow the mains lead.Why not?
I tried two sensors of the type you used. Could not get a reliable read and gave up. I thought I need more resolution than two or three points to achieve 'smooth' ride and I don't have space for 6 or 7 led/sensor pairs. This is when linear arrays came into picture. I have scale 30km/h by now, so minimum program is already achieved :).

Edmunds
 

stan74

Senior Member
Glad to hear it's progressing Edmunds.
"I tried two sensors of the type you used. Could not get a reliable read and gave up." Yeah,they're rubbish but I got a result of sorts with 6 sensors and a few lines of code which is encouraging so I'm going to use 2 sensors if they don't interfere with each other and try edge following. Looks simple-
start:
if left sensor 1 and right sensor 0 go straight
if left sensor 0 go left faster each loop
if right sensor 1 go right faster each loop
goto start
 

stan74

Senior Member
"In theory there is no difference between theory and practice. In practice there is."
why reinvent the wheel?...because I've never invented one before
 

stan74

Senior Member
Made a start. Wee bit o coding and see what happens.SAM_1402.JPG
...and I didn't start this :)
 
Last edited:

stan74

Senior Member
This won't compile...omg, I've gone code blind
Code:
[color=Green];2 ir sensor line edge follower robotty thing[/color]
[color=Navy]#picaxe [/color][color=Black]28x2[/color]
[color=Blue]setfreq m8[/color]
[color=Black]init:[/color]
[color=Blue]symbol [/color][color=Black]lsensor[/color][color=DarkCyan]=[/color][color=Blue]a.2
symbol [/color][color=Black]rsensor[/color][color=DarkCyan]=[/color][color=Blue]a.3
symbol [/color][color=Black]lmotordir [/color][color=DarkCyan]= [/color][color=Blue]B.6
symbol [/color][color=Black]rmotordir [/color][color=DarkCyan]= [/color][color=Blue]B.7
symbol [/color][color=Black]lpwm[/color][color=DarkCyan]=[/color][color=Navy]299[/color][color=Green];60%[/color]
[color=Blue]symbol [/color][color=Black]rpwm[/color][color=DarkCyan]=[/color][color=Navy]299[/color][color=Green];60%[/color]
[color=Blue]high [/color][color=Black]lmotordir [/color][color=Green]; motors[/color]
[color=Blue]high [/color][color=Black]rmotordir [/color][color=Green]; forward[/color]
[color=Blue]pwmout pwmdiv16[/color][color=Black], [/color][color=Blue]C.1[/color][color=Black],[/color][color=Navy]124[/color][color=Black],[/color][color=Navy]0 [/color][color=Green]; 1000Hz @  8MHz left motor [/color]
[color=Blue]pwmout pwmdiv16[/color][color=Black], [/color][color=Blue]C.2[/color][color=Black],[/color][color=Navy]124[/color][color=Black],[/color][color=Navy]0 [/color][color=Green]; 1000Hz @  8MHz right motor[/color]
[color=Blue]pwmduty C.1[/color][color=Black],lpwm[/color]
[color=Blue]pwmduty C.2[/color][color=Black],rpwm

main: [/color][color=Green];----------------------- START ------------------[/color]
[color=Blue]if [/color][color=Black]lsensor[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=DarkCyan]and [/color][color=Black]rsensor[/color][color=DarkCyan]=[/color][color=Navy]0 [/color][color=Blue]then
      pwmduty c.1[/color][color=Black],[/color][color=Navy]299
      [/color][color=Blue]pwmduty c.2[/color][color=Black],[/color][color=Navy]299
      [/color][color=Blue]goto [/color][color=Black]main[/color]
[color=Blue]endif[/color]
[color=Green];[/color]
[color=Blue]if a.2 high then [/color][color=Green];left sensor hit black-steer right
      [/color][color=Black]lpwm[/color][color=DarkCyan]=[/color][color=Black]lpwm[/color][color=DarkCyan]+[/color][color=Navy]10 [/color][color=DarkCyan]max [/color][color=Navy]499
      [/color][color=Black]rpwm[/color][color=DarkCyan]=[/color][color=Black]rpwm[/color][color=DarkCyan]-[/color][color=Navy]10 [/color][color=DarkCyan]min [/color][color=Navy]99 
      [/color][color=Blue]pwmduty c.1[/color][color=Black],lpwm
      [/color][color=Blue]pwmduty c.2[/color][color=Black],rpwm  
      [/color][color=Blue]goto [/color][color=Black]main[/color]
[color=Blue]endif[/color]
[color=Green];[/color]
[color=Blue]if a.3[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=Blue]then[/color][color=Green];  right sensor hit white-steer left
      [/color][color=Black]rpwm[/color][color=DarkCyan]=[/color][color=Black]rpwm[/color][color=DarkCyan]+[/color][color=Navy]10 [/color][color=DarkCyan]max [/color][color=Navy]499
      [/color][color=Black]lpwm[/color][color=DarkCyan]=[/color][color=Black]lpwm[/color][color=DarkCyan]-[/color][color=Navy]10 [/color][color=DarkCyan]min [/color][color=Navy]99 
      [/color][color=Blue]pwmduty c.1[/color][color=Black],lpwm
      [/color][color=Blue]pwmduty c.2[/color][color=Black],rpwm  
      [/color][color=Blue]goto [/color][color=Black]main[/color]
[color=Blue]endif[/color]
[color=Green];[/color]
[color=Blue]goto [/color][color=Black]main[/color]
[color=Green];[/color]
[color=Black]motors_fullspeed:[/color]
[color=Blue]pwmduty C.1[/color][color=Black],[/color][color=Navy]499 [/color][color=Green]; motors[/color]
[color=Blue]pwmduty C.2[/color][color=Black],[/color][color=Navy]499 [/color][color=Green];  full[/color]
[color=Blue]pwmout pwmdiv16[/color][color=Black], [/color][color=Blue]C.1[/color][color=Black],[/color][color=Navy]124[/color][color=Black],[/color][color=Navy]499[/color]
[color=Blue]return[/color]
it's the if a.2=1 or pin a.2. Won't have any permutation.obviously so obvious but not moi.
 

SAborn

Senior Member
Same goes for PinA.3
Why do you use a symbol to name pins etc and then not use the names in your code, all a bit strange way of writing code.
 

stan74

Senior Member
if sensor high is clearer to me than if a.2 high. What's a.2? what's sensor? It's supposed to help others and if the writer is not bright and has to remind himself what he did like me, show what's going on in the program...so I heard.
 

stan74

Senior Member
didn't declare but still errors,I;ve had this before,doh
init:
symbol lsensor=a.2
symbol rsensor=a.3
symbol lmotordir = B.6
symbol rmotordir = B.7
symbol lpwm=b0
symbol rpwm=b1
lpwm=299;60%
rpwm=299;60%
b0 299 sod it,bed time
 
Last edited:

stan74

Senior Member
hippy,there's a pint in it for you when I see you if you can tell me why this doesn't compile.
Code:
[color=Navy]#picaxe [/color][color=Black]28x2[/color]
[color=Blue]setfreq m8[/color]
[color=Black]init:[/color]
[color=Blue]input a.2[/color][color=Black],[/color][color=Blue]a.3
symbol [/color][color=Black]lsensor[/color][color=DarkCyan]=[/color][color=Blue]a.2
symbol [/color][color=Black]rsensor[/color][color=DarkCyan]=[/color][color=Blue]a.3
symbol [/color][color=Black]lmotordir [/color][color=DarkCyan]= [/color][color=Blue]B.6
symbol [/color][color=Black]rmotordir [/color][color=DarkCyan]= [/color][color=Blue]B.7
symbol [/color][color=Black]lpwm[/color][color=DarkCyan]=[/color][color=Purple]w0[/color]
[color=Blue]symbol [/color][color=Black]rpwm[/color][color=DarkCyan]=[/color][color=Purple]w1[/color]
[color=Black]lpwm[/color][color=DarkCyan]=[/color][color=Navy]299[/color][color=Green];60%[/color]
[color=Black]rpwm[/color][color=DarkCyan]=[/color][color=Navy]299[/color][color=Green];60%[/color]
[color=Blue]high [/color][color=Black]lmotordir [/color][color=Green]; motors[/color]
[color=Blue]high [/color][color=Black]rmotordir [/color][color=Green]; forward[/color]
[color=Blue]pwmout pwmdiv16[/color][color=Black], [/color][color=Blue]C.1[/color][color=Black],[/color][color=Navy]124[/color][color=Black],[/color][color=Navy]0 [/color][color=Green]; 1000Hz @  8MHz left motor [/color]
[color=Blue]pwmout pwmdiv16[/color][color=Black], [/color][color=Blue]C.2[/color][color=Black],[/color][color=Navy]124[/color][color=Black],[/color][color=Navy]0 [/color][color=Green]; 1000Hz @  8MHz right motor[/color]
[color=Blue]pwmduty C.1[/color][color=Black],lpwm[/color]
[color=Blue]pwmduty C.2[/color][color=Black],rpwm

main: [/color][color=Green];----------------------- START ------------------
;if lsensor=1 and if rsensor=0 then
;     pwmduty c.1,299
;     pwmduty c.2,299
;     goto main
;endif
;[/color]
[color=Blue]if [/color][color=Black]lsensor[/color][color=DarkCyan]=[/color][color=Navy]0 [/color][color=Blue]then [/color][color=Green];left sensor hit black-steer right
      [/color][color=Black]lpwm[/color][color=DarkCyan]=[/color][color=Black]lpwm[/color][color=DarkCyan]+[/color][color=Navy]10 [/color][color=DarkCyan]max [/color][color=Navy]499
      [/color][color=Black]rpwm[/color][color=DarkCyan]=[/color][color=Black]rpwm[/color][color=DarkCyan]-[/color][color=Navy]10 [/color][color=DarkCyan]min [/color][color=Navy]99 
      [/color][color=Blue]pwmduty c.1[/color][color=Black],lpwm
      [/color][color=Blue]pwmduty c.2[/color][color=Black],rpwm  
      [/color][color=Blue]goto [/color][color=Black]main[/color]
[color=Blue]endif[/color]
[color=Green];[/color]
[color=Blue]if [/color][color=Black]rsensor[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=Blue]then[/color][color=Green];  right sensor hit white-steer left
      [/color][color=Black]rpwm[/color][color=DarkCyan]=[/color][color=Black]rpwm[/color][color=DarkCyan]+[/color][color=Navy]10 [/color][color=DarkCyan]max [/color][color=Navy]499
      [/color][color=Black]lpwm[/color][color=DarkCyan]=[/color][color=Black]lpwm[/color][color=DarkCyan]-[/color][color=Navy]10 [/color][color=DarkCyan]min [/color][color=Navy]99 
      [/color][color=Blue]pwmduty c.1[/color][color=Black],lpwm
      [/color][color=Blue]pwmduty c.2[/color][color=Black],rpwm  
      [/color][color=Blue]goto [/color][color=Black]main[/color]
[color=Blue]endif[/color]
[color=Green];[/color]
[color=Blue]goto [/color][color=Black]main[/color]
 

SAborn

Senior Member
Try this and then figure out what you done wrong.

Code:
;2 ir sensor line edge follower robotty thing
#picaxe 28x2
setfreq m8
init:
symbol lsensor=pina.2
symbol rsensor=pina.3
symbol lmotordir = B.6
symbol rmotordir = B.7


symbol lpwm= w1
symbol rpwm= w2
high lmotordir ; motors
high rmotordir ; forward
pwmout pwmdiv16, C.1,124,0 ; 1000Hz @  8MHz left motor 
pwmout pwmdiv16, C.2,124,0 ; 1000Hz @  8MHz right motor

lpwm=299;60%
rpwm=299;60%

pwmduty C.1,lpwm
pwmduty C.2,rpwm

main: ;----------------------- START ------------------
if lsensor=1 and rsensor=0 then
      pwmduty c.1,299
      pwmduty c.2,299
      goto main
endif
;
if lsensor = 1 then ;left sensor hit black-steer right
      lpwm=lpwm+10 max 499
      rpwm=rpwm-10 min 99 
      pwmduty c.1,lpwm
      pwmduty c.2,rpwm  
      goto main
endif
;
if rsensor = 1 then;  right sensor hit white-steer left
      rpwm=rpwm+10 max 499
      lpwm=lpwm-10 min 99 
      pwmduty c.1,lpwm
      pwmduty c.2,rpwm  
      goto main
endif
;
goto main
;
motors_fullspeed:
pwmduty C.1,499 ; motors
pwmduty C.2,499 ;  full
pwmout pwmdiv16, C.1,124,499
return
 

stan74

Senior Member
What an idiot I am.I didn't plug in the serial cable and power. Sorry..be back with a vid..still o u a pint hippy
 

erco

Senior Member
Minimal input hardware hence less code.
Pretty much.

Code:
#picaxe 08m2

servo 0,150 ' steer servo 70=left 155=straight 230=right
servo 1,110 ' drive servo 110=forward 150=stop
'pin c.2 laser sensor , no lens  black=1    white=0

do
if pinc.2=0 then let b0=b0-2 min 70
else let b0=b0+2 max 230
endif
servopos 0,b0
pause 10
loop
 

stan74

Senior Member
Too busy that day, how was I supposed to change motor speeds when I was using c.1 and c.2 which run at the same speed. The sensors and code works but pwm and cheap dc motors is poor below 60%. Above that the robot goes too fast. I've been busy with a nextion lcd.
 

erco

Senior Member
Yup, always select the right speed gearmotor before building, PWM is a poor bandaid. If you must, use the lowest PWM frequency (70-ish) for toy grade DC brushed motors.

Yes, that laser trike code should also work for any digital (sense/not sensed) sensor.
 

stan74

Senior Member
I modded 2 MG996R for continuous.I saw a youtube vid how to but these have a brass, not plastic slot atatched to pot so cut the blade off pot instead.Pot was 5k so used two 2.7K .Video said 2.2K. Building another chassis with tracks and not a quick bodge job.
 
Top