problème programmation picaxe 28x1

escarpins

New Member
bonjour

je débute avec picaxe .

mon robot mr général ne réagit pas comme il faut .

voici mon programme , pourriez vous me dire si quelque chose ne va pas .
merci d avance .

première partie initialisation

#picaxe 28X1
#no_data
#no_table


' .---------------------------------------------------------------------------.
' | Define constants |
' `---------------------------------------------------------------------------'

symbol LRscalefactor = 10 'Left/Right scale factor used to control pan servo speed
symbol UDscalefactor = 14 'Up/Down scale factor used to control tilt servo speed
symbol distancemax = 200 'object must be withing this distance to track
symbol bestdistance = 550 'robot tries to maintain this distance when tracking
symbol neckLRcenter = 130 'servo center position
symbol neckUDcenter = 160 'servo center position
symbol leftmotorstop = 146 'servo center position
symbol rightmotorstop = 142 'servo center position

symbol LRmax = neckLRcenter+50 'maximum pan value
symbol LRmin = neckLRcenter-50 'minimum pan value
symbol UDmax = neckUDcenter+90 'maximum tilt value
symbol UDmin = neckUDcenter-60 'minimum tilt value

' .---------------------------------------------------------------------------.
' | Define Input and Output Pins |
' `---------------------------------------------------------------------------'

symbol IRleft = 1 'analog input
symbol IRright = 3 'analog input
symbol IRup = 2 'analog input
symbol IRdown = 0 'analog input

symbol leftmotor = 0 'servo output
symbol rightmotor = 1 'servo output
symbol neckupdown = 2 'servo output
symbol neckleftright = 3 'servo output

symbol IRleds = 4 'digital output
symbol Speaker = 5 'Audio output

' .---------------------------------------------------------------------------.
' | Define Variables Used |
' `---------------------------------------------------------------------------'

symbol time = b0 'program loop counter used to measure time / boredom
symbol boredom = b1 'boredom counter for personality
symbol pan = b2 'neck LR position
symbol tilt = b3 'neck UD position
symbol panscale = b4 'scale factor for pan calculations
symbol tiltscale = b5 'scale factor for tilt calculations
symbol leftspeed = b6 'stores value for left motor speed/direction
symbol rightspeed = b7 'stores value for right motor speed/direction
symbol lightchaser = b8 'value represents which LEDs are lit
symbol edge = b9 'input being tested for edge detection
symbol edgesensors = b10 'data from all edge detection sensors

symbol distance = w6 'b12:b13 average of analog IR inputs
symbol temp = w7 'b14:b15 temporary value used in calculations
symbol updown = w8 'b16:b17 scaled diferential value of up and down sensors
symbol leftright = w9 'b18:b19 scaled diferential value of left and right sensors
symbol leftIRvalue = w10 'b20:b21 raw data from left IR sensor
symbol rightIRvalue = w11 'b22:b23 raw data from right IR sensor
symbol upIRvalue = w12 'b24:b25 raw data from upper IR sensor
symbol downIRvalue = w13 'b26:b27 raw data from lower IR sensor


Initialise:
{
' .---------------------------------------------------------------------------.
' | Initialise program |
' `---------------------------------------------------------------------------'

setfreq em16 'set clock speed to external 16MHz resonator
dirsc=%00001111 'configure port C
pan=neckLRcenter 'set neck left/right to center
tilt=neckUDcenter 'set neck up/down to center
panscale=15 'scale factor to prevent servo overcorrection
tiltscale=15 'scale factor to prevent servo overcorrection
servo neckleftright,neckLRcenter 'initialise servo output
servo neckupdown,neckUDcenter 'initialise servo output
leftspeed=leftmotorstop 'set left motor to stop
rightspeed=rightmotorstop 'set right motor to stop
servo leftmotor,leftspeed 'initialise servo output
servo rightmotor,rightspeed 'initialise servo output
pause 4000 'wait 1 second
play speaker,3 'plays "happy birthday" to test speaker
}
 

escarpins

New Member
bonjour

je débute avec picaxe .

mon robot mr général ne réagit pas comme il faut .

voici mon programme , pourriez vous me dire si quelque chose ne va pas .
merci d avance .

première partie initialisation

#picaxe 28X1
#no_data
#no_table


' .---------------------------------------------------------------------------.
' | Define constants |
' `---------------------------------------------------------------------------'

symbol LRscalefactor = 10 'Left/Right scale factor used to control pan servo speed
symbol UDscalefactor = 14 'Up/Down scale factor used to control tilt servo speed
symbol distancemax = 200 'object must be withing this distance to track
symbol bestdistance = 550 'robot tries to maintain this distance when tracking
symbol neckLRcenter = 130 'servo center position
symbol neckUDcenter = 160 'servo center position
symbol leftmotorstop = 146 'servo center position
symbol rightmotorstop = 142 'servo center position

symbol LRmax = neckLRcenter+50 'maximum pan value
symbol LRmin = neckLRcenter-50 'minimum pan value
symbol UDmax = neckUDcenter+90 'maximum tilt value
symbol UDmin = neckUDcenter-60 'minimum tilt value

' .---------------------------------------------------------------------------.
' | Define Input and Output Pins |
' `---------------------------------------------------------------------------'

symbol IRleft = 1 'analog input
symbol IRright = 3 'analog input
symbol IRup = 2 'analog input
symbol IRdown = 0 'analog input

symbol leftmotor = 0 'servo output
symbol rightmotor = 1 'servo output
symbol neckupdown = 2 'servo output
symbol neckleftright = 3 'servo output

symbol IRleds = 4 'digital output
symbol Speaker = 5 'Audio output

' .---------------------------------------------------------------------------.
' | Define Variables Used |
' `---------------------------------------------------------------------------'

symbol time = b0 'program loop counter used to measure time / boredom
symbol boredom = b1 'boredom counter for personality
symbol pan = b2 'neck LR position
symbol tilt = b3 'neck UD position
symbol panscale = b4 'scale factor for pan calculations
symbol tiltscale = b5 'scale factor for tilt calculations
symbol leftspeed = b6 'stores value for left motor speed/direction
symbol rightspeed = b7 'stores value for right motor speed/direction
symbol lightchaser = b8 'value represents which LEDs are lit
symbol edge = b9 'input being tested for edge detection
symbol edgesensors = b10 'data from all edge detection sensors

symbol distance = w6 'b12:b13 average of analog IR inputs
symbol temp = w7 'b14:b15 temporary value used in calculations
symbol updown = w8 'b16:b17 scaled diferential value of up and down sensors
symbol leftright = w9 'b18:b19 scaled diferential value of left and right sensors
symbol leftIRvalue = w10 'b20:b21 raw data from left IR sensor
symbol rightIRvalue = w11 'b22:b23 raw data from right IR sensor
symbol upIRvalue = w12 'b24:b25 raw data from upper IR sensor
symbol downIRvalue = w13 'b26:b27 raw data from lower IR sensor


Initialise:
{
' .---------------------------------------------------------------------------.
' | Initialise program |
' `---------------------------------------------------------------------------'

setfreq em16 'set clock speed to external 16MHz resonator
dirsc=%00001111 'configure port C
pan=neckLRcenter 'set neck left/right to center
tilt=neckUDcenter 'set neck up/down to center
panscale=15 'scale factor to prevent servo overcorrection
tiltscale=15 'scale factor to prevent servo overcorrection
servo neckleftright,neckLRcenter 'initialise servo output
servo neckupdown,neckUDcenter 'initialise servo output
leftspeed=leftmotorstop 'set left motor to stop
rightspeed=rightmotorstop 'set right motor to stop
servo leftmotor,leftspeed 'initialise servo output
servo rightmotor,rightspeed 'initialise servo output
pause 4000 'wait 1 second
play speaker,3 'plays "happy birthday" to test speaker
}
deuxième partie
Main:
{
' .---------------------------------------------------------------------------.
' | Main navigation routine |
' `---------------------------------------------------------------------------'

inc w0 'count program loops
temp=time and 7
if temp=0 then 'chase LEDs every 8 loops
gosub LightChase
end if
servopos leftmotor,leftspeed 'update left servo motor
servopos rightmotor,rightspeed 'update right servo motor
servopos neckleftright,pan 'update pan servo motor
servopos neckupdown,tilt 'update tilt servo motor
gosub IRfollow
if boredom>0 then 'is robot being entertained
gosub Bored
end if
gosub EdgeDetection 'prevent robot falling off edge Use this subroutine if IR sensors face down
'gosub ObjectDetection 'detects objects Use this subroutine if IR sensors face outward
goto Main} 'repeat main loop

IRfollow:
{
' .---------------------------------------------------------------------------.
' | Read IR values |
' `---------------------------------------------------------------------------'

high IRleds 'turn on IR LEDs
readadc10 IRleft,leftIRvalue 'read left IR sensor
readadc10 IRright,rightIRvalue 'read right IR sensor
readadc10 IRup,upIRvalue 'read upper IR sensor
readadc10 IRdown,downIRvalue 'read lower IR sensor

low IRleds 'turn off IR LEDs
readadc10 IRleft,temp 'read left IR sensor ambiant light
leftIRvalue=leftIRvalue-temp 'subtract ambiant light from reading
readadc10 IRright,temp 'read right IR sensor ambiant light
rightIRvalue=rightIRvalue-temp 'subtract ambiant light from reading
readadc10 IRup,temp 'read upper IR sensor ambiant light
upIRvalue=upIRvalue-temp 'subtract ambiant light from reading
readadc10 IRdown,temp 'read lower IR sensor ambiant light
downIRvalue=downIRvalue-temp 'subtract ambiant light from reading


' .---------------------------------------------------------------------------.
' | Determine if object is in range otherwise return head to centre |
' `---------------------------------------------------------------------------'

distance=leftIRvalue+rightIRvalue+upIRvalue+downIRvalue/4 'average of all sensors
temp=distance/8+5 max 127 'adjust distance value to suit "sound" command
sound speaker,(temp,2) 'play sound to indicated distance of object
if distance<distancemax then 'if object is not in range then return neck to center position

if pan>neckLRcenter then 'pan correction
pan=pan-1
end if
if pan<neckLRcenter then 'pan correction
pan=pan+1
end if
if tilt>neckUDcenter then 'tilt correction
tilt=tilt-1
end if
if tilt<neckUDcenter then 'tilt correction
tilt=tilt+1
end if

leftspeed=leftmotorstop 'stop motors if no object in range
rightspeed=rightmotorstop

return
end if

' .---------------------------------------------------------------------------.
' | Track Object |
' `---------------------------------------------------------------------------'

temp=leftIRvalue+rightIRvalue/LRscalefactor 'calculate pan scale value to prevent servo overshoot
panscale=temp

temp=upIRvalue+downIRvalue/UDscalefactor 'calculate titlt scale value to prevent servo overshoot
tiltscale=temp


if leftIRvalue>rightIRvalue then 'pan left if object is to the left
leftright=leftIRvalue-rightIRvalue/panscale
pan=pan-leftright min LRmin max LRmax
end if

if leftIRvalue<rightIRvalue then 'pan right if object is to the right
leftright=rightIRvalue-leftIRvalue/panscale
pan=pan+leftright min LRmin max LRmax
end if


if upIRvalue>downIRvalue then 'tilt up if object is above
updown=upIRvalue-downIRvalue/tiltscale
tilt=tilt-updown min UDmin max UDmax
end if

if upIRvalue<downIRvalue then 'tilt down if object is below
updown=downIRvalue-upIRvalue/tiltscale
tilt=tilt+updown min UDmin max UDmax
end if

if leftright>4 or updown>4 then
boredom=0 'reset bordom counter if object moves enough
end if
 

escarpins

New Member
deuxième partie
Main:
{
' .---------------------------------------------------------------------------.
' | Main navigation routine |
' `---------------------------------------------------------------------------'

inc w0 'count program loops
temp=time and 7
if temp=0 then 'chase LEDs every 8 loops
gosub LightChase
end if
servopos leftmotor,leftspeed 'update left servo motor
servopos rightmotor,rightspeed 'update right servo motor
servopos neckleftright,pan 'update pan servo motor
servopos neckupdown,tilt 'update tilt servo motor
gosub IRfollow
if boredom>0 then 'is robot being entertained
gosub Bored
end if
gosub EdgeDetection 'prevent robot falling off edge Use this subroutine if IR sensors face down
'gosub ObjectDetection 'detects objects Use this subroutine if IR sensors face outward
goto Main} 'repeat main loop

IRfollow:
{
' .---------------------------------------------------------------------------.
' | Read IR values |
' `---------------------------------------------------------------------------'

high IRleds 'turn on IR LEDs
readadc10 IRleft,leftIRvalue 'read left IR sensor
readadc10 IRright,rightIRvalue 'read right IR sensor
readadc10 IRup,upIRvalue 'read upper IR sensor
readadc10 IRdown,downIRvalue 'read lower IR sensor

low IRleds 'turn off IR LEDs
readadc10 IRleft,temp 'read left IR sensor ambiant light
leftIRvalue=leftIRvalue-temp 'subtract ambiant light from reading
readadc10 IRright,temp 'read right IR sensor ambiant light
rightIRvalue=rightIRvalue-temp 'subtract ambiant light from reading
readadc10 IRup,temp 'read upper IR sensor ambiant light
upIRvalue=upIRvalue-temp 'subtract ambiant light from reading
readadc10 IRdown,temp 'read lower IR sensor ambiant light
downIRvalue=downIRvalue-temp 'subtract ambiant light from reading


' .---------------------------------------------------------------------------.
' | Determine if object is in range otherwise return head to centre |
' `---------------------------------------------------------------------------'

distance=leftIRvalue+rightIRvalue+upIRvalue+downIRvalue/4 'average of all sensors
temp=distance/8+5 max 127 'adjust distance value to suit "sound" command
sound speaker,(temp,2) 'play sound to indicated distance of object
if distance<distancemax then 'if object is not in range then return neck to center position

if pan>neckLRcenter then 'pan correction
pan=pan-1
end if
if pan<neckLRcenter then 'pan correction
pan=pan+1
end if
if tilt>neckUDcenter then 'tilt correction
tilt=tilt-1
end if
if tilt<neckUDcenter then 'tilt correction
tilt=tilt+1
end if

leftspeed=leftmotorstop 'stop motors if no object in range
rightspeed=rightmotorstop

return
end if

' .---------------------------------------------------------------------------.
' | Track Object |
' `---------------------------------------------------------------------------'

temp=leftIRvalue+rightIRvalue/LRscalefactor 'calculate pan scale value to prevent servo overshoot
panscale=temp

temp=upIRvalue+downIRvalue/UDscalefactor 'calculate titlt scale value to prevent servo overshoot
tiltscale=temp


if leftIRvalue>rightIRvalue then 'pan left if object is to the left
leftright=leftIRvalue-rightIRvalue/panscale
pan=pan-leftright min LRmin max LRmax
end if

if leftIRvalue<rightIRvalue then 'pan right if object is to the right
leftright=rightIRvalue-leftIRvalue/panscale
pan=pan+leftright min LRmin max LRmax
end if


if upIRvalue>downIRvalue then 'tilt up if object is above
updown=upIRvalue-downIRvalue/tiltscale
tilt=tilt-updown min UDmin max UDmax
end if

if upIRvalue<downIRvalue then 'tilt down if object is below
updown=downIRvalue-upIRvalue/tiltscale
tilt=tilt+updown min UDmin max UDmax
end if

if leftright>4 or updown>4 then
boredom=0 'reset bordom counter if object moves enough
end if
partie 3

' .---------------------------------------------------------------------------.
' | Motor speed / direction if pan is near limit or distance is not ideal |
' `---------------------------------------------------------------------------'

leftspeed=leftmotorstop 'start with motor speed at 0
rightspeed=rightmotorstop 'start with motor speed at 0

temp=LRmax-pan 'determine how close neck is to full right pan
if temp<30 then 'if pan is near right limit then
leftspeed=leftmotorstop-30+temp 'make left wheel go forward
rightspeed=rightmotorstop-30+temp 'make right wheel go backward
end if

temp=pan-LRmin 'determine how close neck is to full left pan
if temp<30 then 'if pan is near left limit then
rightspeed=rightmotorstop+30-temp 'make right wheel go forward
leftspeed=leftmotorstop+30-temp 'make left wheel go backward
end if

if distance>bestdistance then 'check distance of object compared to ideal distance
temp=distance-bestdistance
else
temp=bestdistance-distance
end if

if temp>50 then 'if distance is not within 50 of ideal
temp=temp-50/10 'calculate speed, slow as object is aproached
if distance>bestdistance then 'if distance is too close
rightspeed=rightspeed-temp 'adjust right motor speed to reverse
leftspeed=leftspeed+temp 'adjust left motor speed to reverse
else 'if not too close then too far away
rightspeed=rightspeed+temp 'adjust right motor speed to move forward
leftspeed=leftspeed-temp 'adjust left motor speed to move forward
end if
endif
return}

EdgeDetection:
{
' .---------------------------------------------------------------------------.
' | Check edge detectors |
' `---------------------------------------------------------------------------'

pinsc=%00001111 'turn on edge detection LEDs
edgesensors=pins and %11110000 'read digital inputs
pinsc=%00000000 'turn off edge detection LEDs
if edgesensors<>240 then 'if an edge is detected
temp=edgesensors >> 4 xor %00001111 'right shift 4 bits and invert for LED output
pinsC=temp 'turn on LED to indicate which corner
else
pinsC = lightChaser 'if no edge is detected then turn on chaser LED
end if

edge = edgesensors and 32 'read rear left sensor
if edge =0 and leftspeed>leftmotorstop then 'if edge detected and left motor is reversing
leftspeed=leftmotorstop 'stop left motor
end if

edge = edgesensors and 64 'read front left sensor
if edge =0 and leftspeed<leftmotorstop then 'if edge detected and left motor is moving forward
leftspeed=leftmotorstop 'stop left motor
end if

edge = edgesensors and 16 'read rear right sensor
if edge =0 and rightspeed<rightmotorstop then 'if edge detected and right motor is reversing
rightspeed=rightmotorstop 'stop right motor
end if

edge = edgesensors and 128 'read front right sensor
if edge =0 and rightspeed>rightmotorstop then 'if edge detected and right motor is moving forward
rightspeed=rightmotorstop 'stop right motor
end if

return
}
 

escarpins

New Member
partie 3

' .---------------------------------------------------------------------------.
' | Motor speed / direction if pan is near limit or distance is not ideal |
' `---------------------------------------------------------------------------'

leftspeed=leftmotorstop 'start with motor speed at 0
rightspeed=rightmotorstop 'start with motor speed at 0

temp=LRmax-pan 'determine how close neck is to full right pan
if temp<30 then 'if pan is near right limit then
leftspeed=leftmotorstop-30+temp 'make left wheel go forward
rightspeed=rightmotorstop-30+temp 'make right wheel go backward
end if

temp=pan-LRmin 'determine how close neck is to full left pan
if temp<30 then 'if pan is near left limit then
rightspeed=rightmotorstop+30-temp 'make right wheel go forward
leftspeed=leftmotorstop+30-temp 'make left wheel go backward
end if

if distance>bestdistance then 'check distance of object compared to ideal distance
temp=distance-bestdistance
else
temp=bestdistance-distance
end if

if temp>50 then 'if distance is not within 50 of ideal
temp=temp-50/10 'calculate speed, slow as object is aproached
if distance>bestdistance then 'if distance is too close
rightspeed=rightspeed-temp 'adjust right motor speed to reverse
leftspeed=leftspeed+temp 'adjust left motor speed to reverse
else 'if not too close then too far away
rightspeed=rightspeed+temp 'adjust right motor speed to move forward
leftspeed=leftspeed-temp 'adjust left motor speed to move forward
end if
endif
return}

EdgeDetection:
{
' .---------------------------------------------------------------------------.
' | Check edge detectors |
' `---------------------------------------------------------------------------'

pinsc=%00001111 'turn on edge detection LEDs
edgesensors=pins and %11110000 'read digital inputs
pinsc=%00000000 'turn off edge detection LEDs
if edgesensors<>240 then 'if an edge is detected
temp=edgesensors >> 4 xor %00001111 'right shift 4 bits and invert for LED output
pinsC=temp 'turn on LED to indicate which corner
else
pinsC = lightChaser 'if no edge is detected then turn on chaser LED
end if

edge = edgesensors and 32 'read rear left sensor
if edge =0 and leftspeed>leftmotorstop then 'if edge detected and left motor is reversing
leftspeed=leftmotorstop 'stop left motor
end if

edge = edgesensors and 64 'read front left sensor
if edge =0 and leftspeed<leftmotorstop then 'if edge detected and left motor is moving forward
leftspeed=leftmotorstop 'stop left motor
end if

edge = edgesensors and 16 'read rear right sensor
if edge =0 and rightspeed<rightmotorstop then 'if edge detected and right motor is reversing
rightspeed=rightmotorstop 'stop right motor
end if

edge = edgesensors and 128 'read front right sensor
if edge =0 and rightspeed>rightmotorstop then 'if edge detected and right motor is moving forward
rightspeed=rightmotorstop 'stop right motor
end if

return
}
partie 4

ObjectDetection:
{
' .---------------------------------------------------------------------------.
' | Check object detectors |
' `---------------------------------------------------------------------------'

pinsc=%00001111 'turn on edge detection LEDs
edgesensors=pins and %11110000 'read digital inputs
pinsc=%00000000 'turn off edge detection LEDs
temp=edgesensors >> 4 'rightshift edgesensor data 4bits for LED output
if edgesensors=0 then 'if no object is detected
pinsC=lightchaser 'display light chaser pattern
else 'if object is detected
pinsC=temp 'turn on indicator lights
end if

edge = edgesensors and 32 'read rear left sensor
if edge =32 and leftspeed>leftmotorstop then 'if edge detected and left motor is reversing
leftspeed=leftmotorstop 'stop left motor
end if

edge = edgesensors and 64 'read front left sensor
if edge =64 and leftspeed<leftmotorstop then 'if edge detected and left motor is moving forward
leftspeed=leftmotorstop 'stop left motor
end if

edge = edgesensors and 16 'read rear right sensor
if edge =16 and rightspeed<rightmotorstop then 'if edge detected and right motor is reversing
rightspeed=rightmotorstop 'stop right motor
end if

edge = edgesensors and 128 'read front right sensor
if edge =128 and rightspeed>rightmotorstop then 'if edge detected and right motor is moving forward
rightspeed=rightmotorstop 'stop right motor
end if

return
}

LightChase:
{
' .---------------------------------------------------------------------------.
' | Light Chaser routine |
' `---------------------------------------------------------------------------'

lightchaser = lightchaser >> 1 'right shift pattern 1 bit
If lightchaser = 0 Then 'If none selected ...
lightchaser = 8 'Select first LED
End If
Return
}

Bored:
{
' .---------------------------------------------------------------------------.
' | Boredom subroutine |
' `---------------------------------------------------------------------------'

select case boredom 'look for an object
case 4 'rotate counter clockwise
if edgesensors=240 then
leftspeed=leftmotorstop+20
rightspeed=rightmotorstop+20
return
end if

case 8 'rotate clockwise
if edgesensors=240 then
leftspeed=leftmotorstop-20
rightspeed=rightmotorstop-20
return
end if

case >8
w0=0

endselect
return}
 

dje8269

Senior Member
Waouhhhh !!!!

Bonsoir escarpins ,

Je suis débutant moi aussi, et je crois qu'il va falloir revoir votre demande, je doute fort que quelqu'un vous réponde dans ces conditions.
Comprenez que les experts se trouvant sur ce forum, ne peuvent déchiffrer comme ca brut de fonderie un programme , surtout de cette taille, et sans explication .

Débuter en programmation et/ou avec des picaxes, sur un programme comme ca , je pense n'est pas très indiqué
 

jojojo

Senior Member
Ben ... Moi, je continue de lire le code. Je commenterais après. (Mais, ça n'a pas l'air sot, pour l'instant).
 

PieM

Senior Member
Ben ... Moi, je continue de lire le code. Je commenterais après. (Mais, ça n'a pas l'air sot, pour l'instant).
Bien sûr que c'est pas sot. C'est visiblement un robot avec détection de son environnement par US monté sur deux servos, mais ça a été (mal) pompé quelque part. La moindre des chose dans ce cas est de donner ses sources et de dire ce qui ne fonctionne pas.
Personnellement je ne joue pas avec ce genre de devinette...
 
Top