Putting robotics at your service™

Free shipping on orders over $200

PS2 runs each sequence twice?

Print view Share :
Previous topicNext topic

Page 1 of 1 [ 4 posts ]

User avatar
Rookie ( offline )
Posts: 20
Posted: 2012-03-14 19:36 
I've been working on a Quad robot that is setup with a SSC-32, BBII and BAP28. After searching through forums, manuals and codes I got everything working...kindof...

When I press a button to call a sequence each sequence runs twice. Can anyone tell me why, Ive been trying to figure it out but not sure how to correct this issue.


Code:
;-----------Bot Board Selection----------
;PS2 Controller / BotBoard I
;DAT con P4
;CMD con P5
;SEL con P6
;CLK con P7

;PS2 Controller / BotBoard II
DAT con P12
CMD con P13
SEL con P14
CLK con P15
; ------------------------------------------------------
; Lynxmotion Visual Sequencer SSC-32 'Export' Program
; Sequencer SSC-32 project : 1st attempt
; Date : 3/14/2012 1:03:44 PM
; ------------------------------------------------------
; Format : Basic Atom 24/28 IDE <= V02.2.1.1 or Pro IDE + SSC-32
; Original filename : RB.bas
; Communication with SSC-32 : BotBoard V2 => Pin 08
; ------------------------------------------------------

Steps Wordtable   $04B8, $0B97, $1482, $1ABF, $23C1, $2AC7, $32D3, $3C44, $44BF, $4CD4, $53A2, $5B00, $580F, $580F, |
            $04B8, $0B97, $1482, $1ABF, $23C1, $2AC7, $32D3, $3C44, $44BF, $4CD4, $53A2, $5B00, $5807, $5807, |
            $14BF, $1A6D, $23FF, $2A9E, $4496, $4007, $4007, |
            $0BCB, $14E8, $1A06, $2A24, $32BF, $43F2, $4D63, $5469, $591C, $5807, $5807, |
            $051D, $0B97, $15CA, $29E7, $32D3, $3BA4, $441B, $4CD4, $5346, $5007, $5007, |
            $04B8, $0B1F, $14E8, $29BF, $334E, $3BC4, $4430, $4D3A, $53C0, $59A9, $5807, $5807, |
            $0349, $0BCB, $13F2, $4DB5, $4807, $4807, |
            $03D8, $0B75, $1407, $1AAA, $2392, $2959, $322F, $3B44, $4526, $4E30, $5478, $59FA, $5807, $5807, |
            $047B, $1459, $291C, $4630, $5AEC, $5807, $5807, |
            $3296, $3BD4, $3807, $3807, |
            $1C1B, $23EF, $2B7E, $2807, $2807, |
            $2372, $2007, $2007, |
            $04B8, $0B97, $1482, $1ABF, $23C1, $2AC7, $32D3, $3C44, $44BF, $4CD4, $53A2, $5B00, $5807, $5807, |
            $04BA, $0B98, $147E, $1ABC, $23C0, $2AC6, $32D0, $3C42, $44C4, $4CD8, $53A2, $5B02, $5807, $5807, |
            $04B8, $0B97, $14BF, $1A6D, $23FF, $2A9E, $32D3, $3C44, $4496, $4CD4, $5B00, $5807, $5807, |
            $19F2, $244D, $4D3A, $5459, $591C, $5807, $5807, |
            $4C96, $5355, $5007, $5007, |
            $0546, $0AEB, $322F, $3B94, $4D26, $53FD, $5007, $5007, |
            $04B8, $0AA6, $1110, $242E, $2007, $2007, |
            $0532, $0B63, $0807, $0807, |
            $24EA, $291C, $2807, $2807, |
            $056F, $0AFC, $242E, $326D, $3BC4, $3807, $3807, |
            $0532, $0B0D, $1AD3, $2363, $33C9, $3CB4, $44D4, $4D11, $5383, $59A9, $5807, $5807, |
            $322F, $3B94, $4496, $4007, $4007, |
            $0B30, $33A0, $3C04, $4325, $4007, $4007, |
            $361C, $3D94, $3807, $3807, |
            $04BA, $0B98, $147E, $1A44, $23FF, $2D52, $32D0, $3C42, $44C4, $4CD8, $53A2, $5B02, $5807, $5807, |
            $198B, $249C, $2CEC, $2807, $2807, |
            $19F2, $246D, $2AC6, $2807, $2807, |
            $1ABC, $23C0, $2007, $2007, |
            $04BA, $0B98, $147E, $1ABC, $23C0, $2AC6, $32D0, $3C42, $44C4, $4CD8, $53A2, $5B02, $5807, $5807, |
            $04B8, $0B97, $14BF, $1A1B, $244D, $2A9E, $32D3, $3C74, $4496, $4C30, $5309, $5B00, $5807, $5807, |
            $4CFD, $540D, $5007, $5007, |
            $4B63, $5365, $5C57, $5807, $5807, |
            $0BCB, $14E8, $1A06, $23FF, $2A24, $32BF, $3C44, $43F2, $4A44, $52AD, $5C2F, $5807, $5807, |
            $314E, $3B14, $4563, $4007, $4007, |
            $3206, $3C14, $3807, $3807, |
            $0429, $0B98, $122F, $1C6D, $2363, $291C, $32D0, $3C42, $44C4, $4CD8, $53A2, $5B02, $5807, $5807, |
            $1BA0, $23EF, $2007, $2007, |
            $19F2, $248C, $2AB3, $2807, $2807, |
            $1A1B, $241E, $321B, $3BA4, $3807, $3807, |
            $0321, $0C77, $1ABF, $2343, $31B4, $3B34, $4496, $4007, $4007, |
            $03AF, $14D4, $1007, $1007, |
            $04BA, $0B98, $147E, $1ABC, $23C0, $2AC6, $32D0, $3C42, $44C4, $4007, $4007

SeqPos Wordtable   $0000, |
               $000E, |
               $007A, |
               $00F4, |
               $0164


;-----------Assign I/O pin from BotBoard to SSC-32----------
SSC32   con p8


;-----------Assign Variables for SEQ----------
SeqIndex var Word
Indexs var Word
LastServo var Byte
TmpServo var Byte
TmpValue var Word
DejaVu var bit

LastServo = 99
DejaVu = 0

;-----------Assign Variables for PS2----------
index var byte
temp var byte(19)
mode var byte
Small_Motor var byte
Large_Motor var byte


;-----------Assign other Variables----------
timing var word


;-----------Call out HOME Sub Routine----------
;gosub Attention


;-----------PS2Init----------
high CLK

low SEL
shiftout CMD,CLK,FASTLSBPRE,[$1\8,$43\8,$0\8,$1\8,$0\8] ;CONFIG_MODE_ENTER
high SEL
pause 1

low SEL
shiftout CMD,CLK,FASTLSBPRE,[$01\8,$44\8,$00\8,$01\8,$03\8,$00\8,$00\8,$00\8,$00\8] ;SET_MODE_AND_LOCK
high SEL
pause 100

low SEL
shiftout CMD,CLK,FASTLSBPRE,[$01\8,$4F\8,$00\8,$FF\8,$FF\8,$03\8,$00\8,$00\8,$00\8] ;SET_DS2_NATIVE_MODE
high SEL
pause 1

low SEL
shiftout CMD,CLK,FASTLSBPRE,[$01\8,$4D\8,$00\8,$00\8,$01\8,$FF\8,$FF\8,$FF\8,$FF\8] ;VIBRATION_ENABLE
high SEL
pause 1

low SEL
shiftout CMD,CLK,FASTLSBPRE,[$01\8,$43\8,$00\8,$00\8,$5A\8,$5A\8,$5A\8,$5A\8,$5A\8] ;CONFIG_MODE_EXIT_DS2_NATIVE
high SEL
pause 1

low SEL
shiftout CMD,CLK,FASTLSBPRE,[$01\8,$43\8,$00\8,$00\8,$00\8,$00\8,$00\8,$00\8,$00\8] ;CONFIG_MODE_EXIT
high SEL
pause 1


main:


;-----------PS2 Mode----------
low SEL
; asking "mode" to PS2 controller
shiftout CMD,CLK,FASTLSBPRE,[$1\8]
; reading "mode" from PS2 controller
; 73(hex) is dualshock1 (digital buttons)
; 79(hex) is dualshock2 (analog buttons)
shiftin DAT,CLK,FASTLSBPOST,[mode\8]
high SEL
;-----------------------------


pause 1


;-----------PS2 Data----------
low SEL
; asking data to PS2 controller
shiftout CMD,CLK,FASTLSBPRE,[$1\8,$42\8]
; reading data from controller
; (don't use a "for-next" loop, it's too slow to read PS2 data)
shiftin DAT,CLK,FASTLSBPOST,[temp(0)\8,temp(1)\8,temp(2)\8,temp(3)\8,temp(4)\8,temp(5)\8,temp(6)\8,temp(7)\8,temp(8)\8, |
temp(9)\8,temp(10)\8,temp(11)\8,temp(12)\8,temp(13)\8,temp(14)\8,temp(15)\8,temp(16)\8,temp(17)\8,temp(18)\8]
high SEL
;-----------------------------


pause 200


;-----------Basic Micro IDE terminal----------
; sending carriage return (CR = 13) and the PS2 mode(hex) to the PC
;serout S_OUT,i57600,[13, hex2 mode\2] ; Basic Micro IDE <= 02.2.1.1
;serout S_OUT,I8N1_57600,[13, hex2 mode\2] ; Basic Micro IDE <= 05.3.0.0
;serout S_OUT,i57600,[13, hex2 mode\2] ; Basic Micro Pro IDE <= 08.0.1.7

;for index = 1 to 18 ; temp(0) contains a dummy variable so we don't send it to the PC
; sending all the data to the PC
;serout S_OUT,i57600,[" ", dec3 temp(index)\3] ; Basic Micro IDE <= 02.2.1.1
;serout S_OUT,I8N1_57600,[" ", dec3 temp(index)\3] ; Basic Micro IDE <= 05.3.0.0
;serout S_OUT,i57600,[" ", dec3 temp(index)\3] ; Basic Micro Pro IDE <= 08.0.1.7
;next
;-----------------------------


;---------------------------------------------------BUTTONS ON THE PS2 CONTROLLER TO EXECUTE SUB PROGRAMS------------------------------------


if temp(1)=255 then
   pause 1
   ;gosub Attention

elseif temp(1)=239
   gosub Walk_Forward

elseif temp(1)=223
   gosub Walk_Right

elseif temp(1)=127 
   gosub Walk_left
   
endif

goto MAIN


;----------------------------------------------------------------SUB PROGRAMS TO EXECUTE MOVES-------------------------------------


Attention:
   For SeqIndex = 0 to 0
      For Indexs = SeqPos(SeqIndex) to SeqPos(SeqIndex + 1) - 1
         TmpServo = Steps(Indexs) >> 11
         TmpValue = Steps(Indexs) & 2047
         if LastServo = TmpServo then
            TmpValue = TmpValue << 5
            if DejaVu then
               Pause TmpValue
               LastServo = 99
            else
               serout SSC32,i38400,["T", DEC TmpValue, 13]
               DejaVu = DejaVu ^ 1
            endif
         else
            TmpValue = TmpValue + 500
            serout SSC32,i38400,["#", DEC TmpServo, "P", DEC TmpValue]
            DejaVu = 0
            LastServo = TmpServo
         endif
      next
   next
return


Walk_Forward:
   For SeqIndex = 1 to 1
      For Indexs = SeqPos(SeqIndex) to SeqPos(SeqIndex + 1) - 1
         TmpServo = Steps(Indexs) >> 11
         TmpValue = Steps(Indexs) & 2047
         if LastServo = TmpServo then
            TmpValue = TmpValue << 5
            if DejaVu then
               Pause TmpValue
               LastServo = 99
            else
               serout SSC32,i38400,["T", DEC TmpValue, 13]
               DejaVu = DejaVu ^ 1
            endif
         else
            TmpValue = TmpValue + 500
            serout SSC32,i38400,["#", DEC TmpServo, "P", DEC TmpValue]
            DejaVu = 0
            LastServo = TmpServo
         endif
      next
   next
return

Walk_Right:
   For SeqIndex = 2 to 2
      For Indexs = SeqPos(SeqIndex) to SeqPos(SeqIndex + 1) - 1
         TmpServo = Steps(Indexs) >> 11
         TmpValue = Steps(Indexs) & 2047
         if LastServo = TmpServo then
            TmpValue = TmpValue << 5
            if DejaVu then
               Pause TmpValue
               LastServo = 99
            else
               serout SSC32,i38400,["T", DEC TmpValue, 13]
               DejaVu = DejaVu ^ 1
            endif
         else
            TmpValue = TmpValue + 500
            serout SSC32,i38400,["#", DEC TmpServo, "P", DEC TmpValue]
            DejaVu = 0
            LastServo = TmpServo
         endif
      next
   next
return

Walk_Left:
   For SeqIndex = 3 to 3
      For Indexs = SeqPos(SeqIndex) to SeqPos(SeqIndex + 1) - 1
         TmpServo = Steps(Indexs) >> 11
         TmpValue = Steps(Indexs) & 2047
         if LastServo = TmpServo then
            TmpValue = TmpValue << 5
            if DejaVu then
               Pause TmpValue
               LastServo = 99
            else
               serout SSC32,i38400,["T", DEC TmpValue, 13]
               DejaVu = DejaVu ^ 1
            endif
         else
            TmpValue = TmpValue + 500
            serout SSC32,i38400,["#", DEC TmpServo, "P", DEC TmpValue]
            DejaVu = 0
            LastServo = TmpServo
         endif
      next
   next
return


Thanks


User avatar
Guru ( offline )
Posts: 4920
Posted: 2012-03-14 19:55 
I have not looked that carefully at the code, but one thing that I noticed is your code did things like:
Code:
elseif temp(1)=239
   gosub Walk_Forward

The thing to understand is that with a PS2, it will continue to report a button as pressed as long as you are holding the button down. This works great for many things, but when you only want to execute something once you need to add a little additional code. The idea is to check the button state and the button state for the previous time through the code and only do the operation if the new state is down and old state is up... Something like:
Code:
Ps2Input:

  low PS2SEL
  shiftout PS2CMD,PS2CLK,FASTLSBPRE,[$1\8,$42\8]   
  shiftin PS2DAT,PS2CLK,FASTLSBPOST,[DualShock(0)\8, DualShock(1)\8, DualShock(2)\8, DualShock(3)\8, |
     DualShock(4)\8, DualShock(5)\8, DualShock(6)\8]
  high PS2SEL
  pause 10   
 
 
  IF (DualShock(1).bit3 = 0) and LastButton(0).bit3 THEN   ;Start Button test
...

 
  LastButton(0) = DualShock(1)
  LastButton(1) = DualShock(2)



Note: this was extracted from a different program, that uses variable DualShock instead of Temp. It also shows which bit it is testing for versus a decimal value. Then at the end of the PS2 function it saves away the state of the buttons.

So you can see in the above code how we tested the start button to only process when it is first pressed.

Kurt


User avatar
Rookie ( offline )
Posts: 20
Posted: 2012-03-14 20:19 
Thanks for the quick response! I will give it a try!


User avatar
Rookie ( offline )
Posts: 20
Posted: 2012-03-15 00:41 
GOT IT...

I found an example in an old post in the forum. Actually it was posted by Kurte... thanks again!

Code:
if  temp(1)=247 and PrevTemp(1)=255 then
    gosub servo_home
endif
PrevTemp(1) = Temp(1)


Im not to great at programming and the little bit I do know is from using an Arduino(C++)
So what I did was created a variable 'PrevTemp' and gave it an initial value of 255 then at the end of the sub routine I return it to the value 255. At the end of the MAIN I execute the 'PrevTemp(1) = Temp(1)'

Im sure its not a correct method but its working.


 

All times are UTC - 5 hours [ DST ]. It is currently 2014-11-27 05:20
Feedback Form
Feedback Form