Shop OBEX P1 Docs P2 Docs Learn Events
Messy code — Parallax Forums

Messy code

ArchiverArchiver Posts: 46,084
edited 2000-10-19 05:45 in General Discussion
hello all: I have a light seeking robot. currently it has a CDS cell on each
side at a 45 degree angle foward. When I detect light on the left I rotate
the robot towards the light. I use some code to detect the light intensity
differance between the 2 cells,and rotate the robot faster,if it is a higher
value differance between left and right,then he rotates slower as the light
gets more centered,and intensity differential is less.
Currently I use a jillion IF statments to determine how fast to
rotate,based on the light intensity differential, there must be a better
way then to use all these IF statements, I need a large group for left and
right, and hate to waste code space.
Currently I have the following code,it works fine,but is wasting program
space,and it has to be duplicated for the right sensor as well.
I have not filled the Stamps memory yet,but dont want to throw it away
needlessly either<S>
I thought of a lookdown table,but am confused on there useage,they are so
much greek to me. I did read the manual many times
Intensity = leftCDS - RightCDS
if intensity < 300 go to forward 'light is centered so drive forward
if intensity < 500 then leftdelay1
if intensity < 600 then leftdelay2
if intensity < 700 then leftdelay3
etc etc etc many lines of these IF statments

leftdelay1
delay = 1 ' delay speed to be used in servo turn
goto LEFT ' go to the subroutine that turns robot left

leftdelay2
delay = 2
goto LEFT

LEFT
etc etc code, that turns the robot and has a delay associated with turn speed
for x = 1 to 10 * delay
servoleft etc etc
servoright etc etc
next
delay = 0 'reset delay speed

and then of course all the right light checks and IF statments repeated.

thankyou all very much









Sincerely
Kerry
Admin@M...
WWW server hosting
[url=Http://mntnweb.com]Http://mntnweb.com[/url]
Binghamton online Webcam [url=Http://MntnWeb.Com/bing.htm]Http://MntnWeb.Com/bing.htm[/url]
CHRISTMAS Web Page [url=Http://mntnweb.com/xmas.htm]Http://mntnweb.com/xmas.htm[/url]
--So you think you need more memory... the LEM went to the moon on 16K,how
much do you REALLY need?

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2000-10-18 18:12
    Your if's are not separated by a constant amount, but if they were (or could
    be), you might consider this:

    branch (intensity-300)/100,[noparse][[/noparse]forward,leftdelay1,leftdelay2...]

    This will take your intensity and convert it from 300 -> 0, 400 -> 1, 500 ->
    2, etc.

    Then the branch statement will do what you want. If you want to catch, say,
    300 and 400, just use the same label twice:

    branch (intensity-300)/100,[noparse][[/noparse]forward,forward,leftdelay1,....]

    Also put something after the branch in case nothing matches.

    branch ....
    goto top_loop ' or wherever

    How's that?

    Regards,

    Al Williams
    AWC
    * 8 channels of PWM: http://www.al-williams.com/awce/pak5.htm


    >
    Original Message
    > From: Kerry Barlow [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=4TWhneK0gaU3_k8nE869AL-SQNFEyIgLhmqP-fMyXm7mU3wtqZmZa4Ao0Dty_w4qllhVwnq0F1JrjA3u]admin@m...[/url
    > Sent: Wednesday, October 18, 2000 11:52 AM
    > To: basicstamps@egroups.com
    > Subject: [noparse][[/noparse]basicstamps] Messy code
    >
    >
    > hello all: I have a light seeking robot. currently it has a CDS
    > cell on each
    > side at a 45 degree angle foward. When I detect light on the left I rotate
    > the robot towards the light. I use some code to detect the light intensity
    > differance between the 2 cells,and rotate the robot faster,if it
    > is a higher
    > value differance between left and right,then he rotates slower as
    > the light
    > gets more centered,and intensity differential is less.
    > Currently I use a jillion IF statments to determine how fast to
    > rotate,based on the light intensity differential, there must be a better
    > way then to use all these IF statements, I need a large group for left and
    > right, and hate to waste code space.
    > Currently I have the following code,it works fine,but is wasting program
    > space,and it has to be duplicated for the right sensor as well.
    > I have not filled the Stamps memory yet,but dont want to throw it away
    > needlessly either<S>
    > I thought of a lookdown table,but am confused on there useage,they are so
    > much greek to me. I did read the manual many times
    >
    > Intensity = leftCDS - RightCDS
    > if intensity < 300 go to forward 'light is centered so drive forward
    > if intensity < 500 then leftdelay1
    > if intensity < 600 then leftdelay2
    > if intensity < 700 then leftdelay3
    > etc etc etc many lines of these IF statments
    >
    > leftdelay1
    > delay = 1 ' delay speed to be used in servo turn
    > goto LEFT ' go to the subroutine that turns robot left
    >
    > leftdelay2
    > delay = 2
    > goto LEFT
    >
    > LEFT
    > etc etc code, that turns the robot and has a delay associated
    > with turn speed
    > for x = 1 to 10 * delay
    > servoleft etc etc
    > servoright etc etc
    > next
    > delay = 0 'reset delay speed
    >
    > and then of course all the right light checks and IF statments repeated.
    >
    > thankyou all very much
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > Sincerely
    > Kerry
    > Admin@M...
    > WWW server hosting
    > [url=Http://mntnweb.com]Http://mntnweb.com[/url]
    > Binghamton online Webcam [url=Http://MntnWeb.Com/bing.htm]Http://MntnWeb.Com/bing.htm[/url]
    > CHRISTMAS Web Page [url=Http://mntnweb.com/xmas.htm]Http://mntnweb.com/xmas.htm[/url]
    > --So you think you need more memory... the LEM went to the moon
    > on 16K,how
    > much do you REALLY need?
    >
    >
    >
    >
    >
  • ArchiverArchiver Posts: 46,084
    edited 2000-10-18 19:03
    Hello,

    There's not too many ways to make it neater unless a language like
    Visual Basic is used. But try this:

    'With the BSII:
    lookdown intensity,<[noparse][[/noparse]300,400,500,600,etc...],result
    delay=result+1

    Lookdown will return the first position it is in the list that it is
    less than.

    DJ

    --- In basicstamps@egroups.com, Kerry Barlow <admin@m...> wrote:
    > hello all: I have a light seeking robot. currently it has a CDS
    cell on each
    > side at a 45 degree angle foward. When I detect light on the left I
    rotate
    > the robot towards the light. I use some code to detect the light
    intensity
    > differance between the 2 cells,and rotate the robot faster,if it is
    a higher
    > value differance between left and right,then he rotates slower as
    the light
    > gets more centered,and intensity differential is less.
    > Currently I use a jillion IF statments to determine how fast to
    > rotate,based on the light intensity differential, there must be a
    better
    > way then to use all these IF statements, I need a large group for
    left and
    > right, and hate to waste code space.
    > Currently I have the following code,it works fine,but is wasting
    program
    > space,and it has to be duplicated for the right sensor as well.
    > I have not filled the Stamps memory yet,but dont want to throw it
    away
    > needlessly either<S>
    > I thought of a lookdown table,but am confused on there useage,they
    are so
    > much greek to me. I did read the manual many times
    >
    > Intensity = leftCDS - RightCDS
    > if intensity < 300 go to forward 'light is centered so drive
    forward
    > if intensity < 500 then leftdelay1
    > if intensity < 600 then leftdelay2
    > if intensity < 700 then leftdelay3
    > etc etc etc many lines of these IF statments
    >
    > leftdelay1
    > delay = 1 ' delay speed to be used in servo turn
    > goto LEFT ' go to the subroutine that turns robot left
    >
    > leftdelay2
    > delay = 2
    > goto LEFT
    >
    > LEFT
    > etc etc code, that turns the robot and has a delay associated with
    turn speed
    > for x = 1 to 10 * delay
    > servoleft etc etc
    > servoright etc etc
    > next
    > delay = 0 'reset delay speed
    >
    > and then of course all the right light checks and IF statments
    repeated.
    >
    > thankyou all very much
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > Sincerely
    > Kerry
    > Admin@M...
    > WWW server hosting
    > [url=Http://mntnweb.com]Http://mntnweb.com[/url]
    > Binghamton online Webcam [url=Http://MntnWeb.Com/bing.htm]Http://MntnWeb.Com/bing.htm[/url]
    > CHRISTMAS Web Page [url=Http://mntnweb.com/xmas.htm]Http://mntnweb.com/xmas.htm[/url]
    > --So you think you need more memory... the LEM went to the moon on
    16K,how
    > much do you REALLY need?
  • ArchiverArchiver Posts: 46,084
    edited 2000-10-18 19:11
    Hi Kerry,

    Why do you need conditional branching statements at all? Would it work to
    simply use the value from intensity to get delay?

    Something like

    delay = intensity / 100 - 4

    Guess you'd still need one If statment for forward, but it seems to me you
    bot doesn't need to make nearly as many decisions based on intensity as it
    is doing now. It just needs to get a value.

    Greg R.

    Original Message
    From: Kerry Barlow <admin@m...>
    To: <basicstamps@egroups.com>
    Sent: Wednesday, October 18, 2000 12:51 PM
    Subject: [noparse][[/noparse]basicstamps] Messy code


    > hello all: I have a light seeking robot. currently it has a CDS cell on
    each
    > side at a 45 degree angle foward. When I detect light on the left I rotate
    > the robot towards the light. I use some code to detect the light intensity
    > differance between the 2 cells,and rotate the robot faster,if it is a
    higher
    > value differance between left and right,then he rotates slower as the
    light
    > gets more centered,and intensity differential is less.
    > Currently I use a jillion IF statments to determine how fast to
    > rotate,based on the light intensity differential, there must be a better
    > way then to use all these IF statements, I need a large group for left and
    > right, and hate to waste code space.
    > Currently I have the following code,it works fine,but is wasting program
    > space,and it has to be duplicated for the right sensor as well.
    > I have not filled the Stamps memory yet,but dont want to throw it away
    > needlessly either<S>
    > I thought of a lookdown table,but am confused on there useage,they are so
    > much greek to me. I did read the manual many times
    >
    > Intensity = leftCDS - RightCDS
    > if intensity < 300 go to forward 'light is centered so drive forward
    > if intensity < 500 then leftdelay1
    > if intensity < 600 then leftdelay2
    > if intensity < 700 then leftdelay3
    > etc etc etc many lines of these IF statments
    >
    > leftdelay1
    > delay = 1 ' delay speed to be used in servo turn
    > goto LEFT ' go to the subroutine that turns robot left
    >
    > leftdelay2
    > delay = 2
    > goto LEFT
    >
    > LEFT
    > etc etc code, that turns the robot and has a delay associated with turn
    speed
    > for x = 1 to 10 * delay
    > servoleft etc etc
    > servoright etc etc
    > next
    > delay = 0 'reset delay speed
    >
    > and then of course all the right light checks and IF statments repeated.
    >
    > thankyou all very much
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > Sincerely
    > Kerry
    > Admin@M...
    > WWW server hosting
    > [url=Http://mntnweb.com]Http://mntnweb.com[/url]
    > Binghamton online Webcam [url=Http://MntnWeb.Com/bing.htm]Http://MntnWeb.Com/bing.htm[/url]
    > CHRISTMAS Web Page [url=Http://mntnweb.com/xmas.htm]Http://mntnweb.com/xmas.htm[/url]
    > --So you think you need more memory... the LEM went to the moon on
    16K,how
    > much do you REALLY need?
    >
    >
    >
    >
    >
    >
  • ArchiverArchiver Posts: 46,084
    edited 2000-10-19 05:45
    Hi Kerry,

    Here's a short routine that takes care of forward, right and left and
    intensity. The intensity values seem to be equally spaced
    (500,600,700,...?) for different delays, so there is no need for a lookdown
    table. As Greg pointed out, simple mathematical formula will do. The
    direction is determined by the sign of the difference between the two
    photocell signals, and the delay is determined by the absolute value of the
    intensity. There is no need to repeat everything for left and right.

    intensity var word
    sign var bit
    delay var byte

    intensity = leftCDS - RightCDS ' difference, negative if rightCDS>leftCDS
    sign = intensity.bit15 ' direction? sign=0 means left is greater
    intensity = abs intensity ' absolute value of intensity
    if intensity<300 then forward ' light is centered so drive forward
    delay=intensity/100 min 5 - 4 ' formula for delay
    branch sign [noparse][[/noparse]LEFT,RIGHT] ' now have all info for either right or
    left.


    If the intensity values are not evenly spaced, then use a lookdown table in
    place of the formula:
    lookdown intensity,<[noparse][[/noparse]300,550,690,777,926,1987,7257,10460,32768],delay
    ' delay from 1 to 8 (arbitrary table entries)

    The "<" before the table qualifies the relation to be satisfied. The delay
    can never come out =0, because at this point in the program, intensity>300.
    The final 32768 catches all of the highest possible values of intensity
    from 10460 to 32768. On the Stamp, the absolute value of a number will
    fall in the range 0<=intensity<=32767.

    I hope that helps,
    -- Tracy Allen
    electronically monitored ecosystems
    http://www.emesystems.com


    > hello all: I have a light seeking robot. currently it has a CDS cell on
    each
    > side at a 45 degree angle foward. When I detect light on the left I
    rotate
    > the robot towards the light. I use some code to detect the light
    intensity
    > differance between the 2 cells,and rotate the robot faster,if it is a
    higher
    > value differance between left and right,then he rotates slower as the
    light
    > gets more centered,and intensity differential is less.
    > Currently I use a jillion IF statments to determine how fast to
    > rotate,based on the light intensity differential, there must be a better
    > way then to use all these IF statements, I need a large group for left
    and
    > right, and hate to waste code space.
    > Currently I have the following code,it works fine,but is wasting program
    > space,and it has to be duplicated for the right sensor as well.
    > I have not filled the Stamps memory yet,but dont want to throw it away
    > needlessly either<S>
    > I thought of a lookdown table,but am confused on there useage,they are
    so
    > much greek to me. I did read the manual many times
    >
    > ..snip...
    > and then of course all the right light checks and IF statments repeated.
Sign In or Register to comment.