Shop OBEX P1 Docs P2 Docs Learn Events
trying to put color pal data into program — Parallax Forums

trying to put color pal data into program

antwayneantwayne Posts: 5
edited 2013-12-11 13:57 in Accessories
thanks to Phil i was able to get my color pal to work

now i need some help putting the data to work in a program
i have six colors that i am attempting to sort into different bins
i have come up with the this so far and now am trying to work in to a program to send each color off to the right address
Please do not laugh to hard at my beginning attempt..

if w11 160 to 185 then goto ;salmon if w11 110 to 145 then goto ;blue
if w11 80 to 105 then goto ;green
if w11 60 to 75 then goto ;puprle
if w11 40 to 55 then goto ;yellow
if w11 20 to 35 then goto ;red


' Modified by:
' Revison date: 20131511






DATA 0, ("A",1,"H",127,"C",1,"D",1,"O",0,"A",1, "P",165,"D",1,"C",1,"A",1,"P",120,"D",1,"T",3)

symbol HOME_IN = pinC.0 'Home position input pin
symbol outbuf = b0 'Byte for output buffering, directly maps to output pins
symbol unusedbit0 = bit0 'Unused output bit <pin0>
symbol ARM = b.1 'Bit for arm: 0 = Down, 1 = Up <pin1>
symbol unusedbit2 = bit2 'Unused output bit <pin2>
symbol so = b24 'S servo oprn output bit <pin3>
symbol sc = b25 'G servo closed output bit <pin3>
symbol COIL_A = bit4 'Bit for driving stepper coil output A - Leave 0!
symbol COIL_B = bit5 'Bit for driving stepper coil output B - Leave 0!
symbol COIL_C = bit6 'Bit for driving stepper coil output C - Leave 0!
symbol COIL_D = bit7 'Bit for driving stepper coil output D - Leave 0!
symbol IN_USE = b1 'Make sure that b1 doesn't get used by accident!
symbol dir = bit8 'Bit determining direction 0 = CW, 1 = CCW
'symbol stepper = b2 'stepper keeps track of what step we are in
symbol pointer = b3 'Pointer in command list
symbol cmd = b4 'Command identifier
symbol arg = b5 'Argument value
symbol position = b6 'Target position
symbol steps = b7 'Number of steps this movement
symbol counter = b8 'Counter variable
symbol my_pos = b9 'My current position variable - 127 is center
symbol stepbits = b11 'Stepper bits - high nibble drives output coils, low nibble = 0
symbol tempword = w6 'Temporary storage word (16bits)
symbol tempbyte0 = b12 'Temporary byte0 - do not use at same time as tempword!
symbol tempbyte1 = b13 'Temporary byte1 - do not use at same time as tempword!
outbuf = 0 'Set output buffer to 0, clear all outputs!
stepbits = $f0 'Set the stepper bits to all on - draw no current.
dirsB = %11110000 'Set the direction of port B
outpinsB = $00 'Set the actual pins to above before we write outputs
'gosub home 'Find home before starting main program.
'goto cmd_stepoff 'Shut the stepper off to keep things cool

servo b.3, 150 ;right finger
servo b.2, 110 ;left finger
servo b.0, 150 ;thumb
'Main loop starts here:
do ' Loop through all commands.
'pointer = 0 '//first position in our Sequence DATA
'//.....first position of our Sequence DATA is where we want to be so.....
'if we are at the end of are list of commands then reset pointer
if cmd = 0 then
pointer = 0
'stepper = 0
endif
'stepper = stepper +1
' Read the value at the pointer location as the command
read pointer, cmd '// read command form DATA at location pointer and place in cmd
' move pointer to the argument for THIS command
inc pointer
' ....and read the value into variable arg
read pointer, arg
' move pointer to the argument for THE NEXT command
inc pointer
'if cmd is past where sequence DATA are written in then.....
'SEROUT 2,t2400_4,(150,8,cmd,154,8,8,8,#arg,157,8,8,#stepper)
'Command parser using if statements
'Compare cmd to ascii values A,S,O,R,L,P,H,T
' and go to appropriate label
if cmd = "A" then cmd_armup
if cmd = "D" then cmd_armdown
if cmd = "C" then cmd_so
if cmd = "G" then cmd_sc
if cmd = "O" then cmd_stepoff
'if cmd = "R" then cmd_right
'if cmd = "L" then cmd_left
if cmd = "P" then cmd_position
if cmd = "H" then cmd_home
if cmd = "T" then cmd_timer
' "T" command to insert a timeout of <arg> tenths of a second
' Used for sequenceing program in auto mode, not used in serial
cmd_timer:
tempword = arg * 100 'Store 100*arg to a 16 bit word
pause tempword 'Pause for <tempword> milliseconds
' The end of the processing loop - set outputs to bufferd stated
cmd_end:
'Set the outpins using previously determined states by
'performing an OR operation on the outbuf, which should
'have only it's low nibble set, and stepbits, which should
'have only the high nibble set.
pause 500



loop ' Return to top of program continously and repeat
' "A" command to control the arm
' 0 = Down, 1 = Up
cmd_armup:
high b.1 'Set ARM equal to value of argument
goto cmd_end 'Jump to end of loop

cmd_armdown:
low b.1
goto cmd_end
' "G""C" command to control the grip

cmd_so:
'pause to alow arm to stop
gosub open_srv

goto cmd_end 'Jump to end of loop
' sc = Close
cmd_sc:
gosub close_srv

goto cmd_end 'Jump to end of loop
' "O" command to turn off stepper motor
open_srv:
PAUSE 1000
servopos b.3, 80
servopos b.2, 180
servopos b.0, 215
return

close_srv:
PAUSE 1000
servopos b.3, 150
servopos b.2, 110
servopos b.0,150
return

' Argument is ignored
cmd_stepoff:
stepbits = $f 'Set buffered bits for stepper to all high
goto cmd_end 'Jump to end of loop
' "R" command to make a relative turn toward the right (CW)
' Argument is the number of steps right (actualy, half-steps)
'cmd_right:
' steps = arg 'Set the number of steps to execute
' gosub step_cw 'Step CW <arg> number of steps
' goto cmd_end 'Jump to end of loop
' "L" command to make a relative turn toward the left (CCW)
' Argument is the number of steps left
'cmd_left:
' steps = arg 'Set the number of steps to execute
' gosub step_ccw 'Step CCW <arg> number of steps
' goto cmd_end 'Jump to end of loop
' "P" command to go to absolute position (127 being home)
' Argument is desired position to goto
cmd_position:
position = arg 'Set position equal to arg
gosub step_goto 'Call routine to goto that point
goto cmd_end 'Jump to end of loop
' "H" command to execute home position search routine
' Argument is ignored
cmd_home:
gosub home 'Call the home subroutine
goto cmd_end 'Jump to end of loop
'Function to go to an absolute position
step_goto:
'If the arm is right of the desired position,
' then skip to goto_ccw label
if position < my_pos then goto_ccw
'Since the arm is currently left of the desired position,
' determine the number of steps right to move by subtracting
' the current position from the desired position.
steps = position - my_pos
goto step_cw 'Skip to step_cw label to step clockwise.
' Label when we need to step ccw when going to an absolute position.
goto_ccw:
'Determine the nuber of steps left to move by subtracting the
'desired position from the current position.
steps = my_pos - position
goto step_ccw 'Skip to step_ccw label to step counterclockwise.
'Function to step clockwise
step_cw:
dir = 0 'Set the direction bit to 0 to increment my_pos
goto step_loop 'Jump to the loop to actually step
'Function to step counter-clockwise
step_ccw:
dir = 1 'Set the direction bit to 1 to deincrement my_pos
'Loop to handle actual stepping function
step_loop:
'Loop while incrementing counter from one to the number of steps
'required in this movement.
for counter = 1 to steps
'SEROUT 2,t2400_4,(161,8,8,8, #my_pos)
pause 25 'Wait 25ms between steps
'Use a nasty little trick to increment or deincrement my_pos:
' To add 1 or -1 based on the direction bit without using
'much memory we do the following:
' 1 = 1 + 0, -1 = 1 + -2
' 1 = 1 + ( 0 * -2 ) -1 = 1 + ( 1 * -2 )
' Let a binary value take the place of the 0 an 1 multipliers
' 1 = 1 + ( dir * -2 ) -1 = 1 + ( dir * -2 )
' when dir = 0 when dir = 1
' Of course, since this version of BASIC doesn't understand
'order of operations, we need to reorder it somewhat:
my_pos = -2 * dir + 1 + my_pos
' Use a temporary bte to hold the remainder from integer division
'of my_pos by 8 using the modulus funciont to determine the
' current step offset for lookup.
tempbyte0 = my_pos % 8
' Use a lookup function to determine the correct output pattern
'for the stepper motor based on the offset in tempbyte0
lookup tempbyte0, ($1,$5,$4,$6,$2,$A,$8,$9), stepbits
' Set the outputs using the same OR opperation as before.


select case b11
case $1
low b.7,b.6,b.5
high b.4
case $5
low b.7,b.5
high b.6,b.4
case $4
low b.7,b.5,b.4
high b.6
case $6
low b.7,b.4
high b.6,b.5
case $2
low b.7,b.6,b.4
high b.5
case $A
low b.6,b.4
high b.7,b.5
case $8
low b.6,b.5,b.4
high b.7
case $9
low b.6,b.5
high b.7,b.4

end select

next counter 'Increment the counter and continue the loop
return 'Return to calling location
home: 'Home position search routine
my_pos = arg 'Set ourselvs to think we're centered now.
steps = 1 ' One step at a time
' Loop through deincremting position from my_pos to 0
for position = my_pos to 0 step -1
' If the home position sensor input is active this step
'then set this to be our new home position, we might jump
'a couple of steps in one direction if we're not at the
'same multiple of 8.
if HOME_IN = 1 then found
gosub step_goto 'Goto the new position
'Continue the CCW loop
next position
'We fell through the end of the last loop meaning
'we couldn't find home in the ccw direction, so try
'cw direction.
for position = 0 to 255
' Same operation as in ccw search, note that the width of
'the interruptor tab will effect exact position of HOME
'which may be slightly different from the left and right.
if HOME_IN = 1 then found
gosub step_goto 'Goto the new position
'Continue the CW loop
next position
'If we fell through this far, we have a problem and should halt
'rather than search endlessly
stop
return
' found the home position
found:' Define my current position to be the HOME position
my_pos = 127
' Set the current position to be my_pos as well
position = my_pos
' Return to calling location
return

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-12-11 11:35
    As you can see, the forum software doesn't preserve indentation.

    Once again Phil to the rescue. Here's a link to his tutorial on posting code.

    attachment.php?attachmentid=78421&d=1297987572
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2013-12-11 11:53
    I was going to mention that but he attached the code too, so I figured that was the better source.
  • antwayneantwayne Posts: 5
    edited 2013-12-11 13:57
    sorry… am new at this…
    even the attachment is the wrong one
Sign In or Register to comment.