BasicMicro - Forums

www.basicmicro.com
It is currently Mon May 21, 2012 12:54 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: Arc32 and Ps2 controller
PostPosted: Sat Apr 16, 2011 8:14 pm 
Offline
Citizen

Joined: Mon Mar 28, 2011 5:18 pm
Posts: 10
Is there a picture or a tutorial on connecting the Ps2 controller to the Arc?
I bought the controller and the wiring from Lynx.
I'm looking for a real basic code to learn how to control the 32 servos.
I'm told the phoenix code has the Ps2 code built in and I need to look at this code,
But being a Newbie I'm not sure if I will understand it.
I watched the video of the arm being controlled with the Ps2 and Arc,
Is there a sample of this code?
Thanks


Top
 Profile  
 
 Post subject: Re: Arc32 and Ps2 controller
PostPosted: Sun Apr 17, 2011 7:05 am 
Offline
Master

Joined: Tue Nov 21, 2006 9:34 am
Posts: 527
Sorry that my previous answer was not sufficient.

As I mentioned in the other posting that when I use the Arc32, I connect the PS2 receiver to the AUX 2 pins. Which is section (E) in the Arc32 data sheet hardware overview, which is P40-43. This is because the DAT line of the PS2 requires a pull-up resistor and these IO lines on the Arc32 has pull-up resistors installed. The J5-AUX2 diagram in the data sheet shows where these pins are. I plug the DAT line (often brown wire of 4 wire group on PS2 connector) into P40. You need to also plug the controller into a +5v (Vcc) and ground. Not sure what PS2 connector you are using, so not sure of colors. But I think yellow may be the current one. The question will be where to get the Vcc. If you have configured your Arc32 that one of the groups of 8 pins has +5v you can use one of these groups. Or you can use pins 7-8 on Aux1 header to get power/gnd. Or you can for example use an exacto knife or the like to remove the power pin from the 2 pin connector and plug it into one of the Vs pins in the a Vcc pin of the Servo power selection jumpers (labeled C on the hardware block diagram) and plug the ground in somewhere convenient like the other pins on the Aux 2 header...

In the Data section of my Basic Program I have the following defines:
Code:
;[PS2 Controller Constants]
PS2DAT             con P40      ;PS2 Controller DAT (Brown)
PS2CMD             con P41      ;PS2 controller CMD (Orange)
PS2SEL             con P42      ;PS2 Controller SEL (Blue)
PS2CLK             con P43      ;PS2 Controller CLK (White)
PadMode          con $73


As I mentioned in the previous post, I use a slightly striped down PS2 init code that leaves me in the default analog mode, which only returns 6 extra bytes ($73) instead of the code that many of the programs use that returns 18 extra bytes ($79) as most programs don't use the button pressure values returned in the extra bytes.

The code at the start of the PS2 control input for the Phoenix has the following code:
Code:
;--------------------------------------------------------------------
;[Ps2 Controller Variables]
DualShock          var Byte(7)
LastButton          var Byte(2)
DS2Mode          var Byte
PS2Index         var byte
#ifdef DEBUG_PS2
DSPrev            var byte(7)
fDSChanged         var   bit
iDS               var byte
#endif

;--------------------------------------------------------------------
;[InitController] Initialize the PS2 controller
;--------------------------------------------------------------------
InitController:
;PS2 controller

RetryPs2TestInit:
  high PS2CLK
  LastButton(0) = 255
  LastButton(1) = 255

  low PS2SEL
  shiftout PS2CMD,PS2CLK,FASTLSBPRE,[$1\8]
  shiftin PS2DAT,PS2CLK,FASTLSBPOST,[DS2Mode\8]
  high PS2SEL
  pause 1
 
  if DS2Mode <> PadMode THEN
   low PS2SEL
   shiftout PS2CMD,PS2CLK,FASTLSBPRE,[$1\8,$43\8,$0\8,$1\8,$0\8] ;CONFIG_MODE_ENTER
   high PS2SEL
   pause 1

   low PS2SEL
   shiftout PS2CMD,PS2CLK,FASTLSBPRE,[$01\8,$44\8,$00\8,$01\8,$03\8,$00\8,$00\8,$00\8,$00\8] ;SET_MODE_AND_LOCK
   high PS2SEL
   pause 1
#if 0
   low PS2SEL
   shiftout PS2CMD,PS2CLK,FASTLSBPRE,[$01\8,$4F\8,$00\8,$FF\8,$FF\8,$03\8,$00\8,$00\8,$00\8] ;SET_DS2_NATIVE_MODE
   high PS2SEL
   pause 1
#endif
   low PS2SEL
   shiftout PS2CMD,PS2CLK,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 PS2SEL
   pause 1
#if 0
   low PS2SEL
   shiftout PS2CMD,PS2CLK,FASTLSBPRE,[$01\8,$43\8,$00\8,$00\8,$00\8,$00\8,$00\8,$00\8,$00\8] ;CONFIG_MODE_EXIT
   high PS2SEL
#endif   
   pause 1
      
   sound cSOUND,[100\4000, 100\4500, 100\5000]
   
   goto RetryPs2TestInit ;Check if the remote is initialized correctly
  ENDIF
return

;--------------------------------------------------------------------
;[ControlInput] reads the input data from the PS2 controller
;--------------------------------------------------------------------
ControlInput:

  ; In case the controller gets out of whack double check to see if we are still in the right
  high PS2CLK   ;init clk state
  low PS2SEL
  shiftout PS2CMD,PS2CLK,LSBPRE,[$1\8]
  shiftin PS2DAT,PS2CLK,LSBPOST,[DS2Mode\8]
  high PS2SEL
  pause 1
 
  if(DS2Mode <> PadMode)then
   gosub RetryPs2TestInit   ; use secondary name so does not depend on if XBee is also defined...
  endif


  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   


#ifdef DEBUG_PS2
  fDSChanged = 0
  for iDS = 0 to 6
     if DualShock(iDS) <> DSPrev(iDS) then
       DSPrev(iDS) = DualShock(iDS)
       fDSChanged = 1
    endif
  next
  if fDSChanged then
    hserout ["DS: ", hex dsPrev(0), " ",hex dsPrev(1), " ",hex dsPrev(2), " ",hex dsPrev(3), " ",|
          hex dsPrev(4), " ",hex dsPrev(5), " ",hex dsPrev(6), 13]
  endif
#endif   


The above code has an init function, that you can call at the start. The main function ControlInput reads in the data. It checks to make sure we are still in analog mode and if not will reinit the PS2 controller. There is some debug code here that will print out the PS2 data if any of the data changes. After this code completes the array: DualShock has the current PS2 data.

Also as this is a code extract don't be alarmed if there are variables that are undefined... Like I think I use a sound command to a defined pin like cSound: I often plug in a cheap and dirty radioshack speaker into a pin such that I can get sound... It would be better to hook up a full sound circuit, but I only had one of these speakers fail once with me just connecting it directly up to a signal pin and ground...


Top
 Profile  
 
 Post subject: Re: Arc32 and Ps2 controller
PostPosted: Sun Apr 17, 2011 2:06 pm 
Offline
Citizen

Joined: Mon Mar 28, 2011 5:18 pm
Posts: 10
Oh no it was fine, Just being new to this I'm afraid I'll damage the Arc.
I'm using the Wireless Ps2 from Lynx and the ready made cable they sell.
I'm learning but it will be slow and I read everything I can find on the Arc.

I will be able to work on this more tomorrow and Ill work with your example.
Thanks for being patient with me, I really do appreciate all the help I can get
To get my R2D2 running off this board. Being able to get all his functions going will
Be Great!!!!


Top
 Profile  
 
 Post subject: Re: Arc32 and Ps2 controller
PostPosted: Wed Apr 20, 2011 11:18 am 
Offline
Citizen

Joined: Mon Mar 28, 2011 5:18 pm
Posts: 10
The Lynx cable has the brown pin40 (pin2 on the Arc) then orange pin41 (pin4 on the Arc) in a pin set of 4 pins.
But the next color is yellow then blue, Do I need to remove the blue and put it next to the orange?
The blue connects to pin42 (pin6 on the Arc).
The white is in a pin set of 2 with white and black in the same pin and the red next to them.

This is the diagram of the Ps2 cable: http://www.lynxmotion.com/images/index/wiring07.jpg

I'm guessing this cable was not made just for the Arc so the pin sets are different.


Top
 Profile  
 
 Post subject: Re: Arc32 and Ps2 controller
PostPosted: Wed Apr 20, 2011 12:52 pm 
Offline
Citizen

Joined: Mon Mar 28, 2011 5:18 pm
Posts: 10
I was looking through the changes you have made to your code.
And I understand that this will initiate the Ps2 and gets it ready for use. But how do you write code for say pin0 (servo1)
to respond to the status of a Ps2 button or analog stick?
I have been looking at the Phoenix code also and cant find how this is done.
Thanks


Top
 Profile  
 
 Post subject: Re: Arc32 and Ps2 controller
PostPosted: Wed Apr 20, 2011 1:31 pm 
Offline
Citizen

Joined: Mon Mar 28, 2011 5:18 pm
Posts: 10
I was reading another site concerning the Ps2
This is what is in your code:
;[PS2 Controller Constants]
PS2DAT con P40 ;PS2 Controller DAT (Brown)
PS2CMD con P41 ;PS2 controller CMD (Orange)
PS2SEL con P42 ;PS2 Controller SEL (Blue)
PS2CLK con P43 ;PS2 Controller CLK (White)
PadMode con $73

But the site says the blue wire is the Clock and the white wire is unknown.
Is this correct or is this something that can be changed in code?


Top
 Profile  
 
 Post subject: Re: Arc32 and Ps2 controller
PostPosted: Wed Apr 20, 2011 2:26 pm 
Offline
Citizen

Joined: Mon Mar 28, 2011 5:18 pm
Posts: 10
Sorry to post so much, But I'm going to figure this thing out.
I ran the code and had to comment out:
sound cSOUND,[100\4000, 100\4500, 100\5000]
But it found no other errors.
I have the Ps2 wired and the receiver lights are on.
Still not sure about the blue wire.
But with no errors I guess its wired correct.


Top
 Profile  
 
 Post subject: Re: Arc32 and Ps2 controller
PostPosted: Wed Apr 20, 2011 2:58 pm 
Offline
Master

Joined: Tue Nov 21, 2006 9:34 am
Posts: 527
Please note: there are at least 4 versions of the cable out there with different color schemes. But most of them appear to have brown as the DAT line. So if you plug the 4 wire in such that the brown is in P40 on the ARC32, the other three should be correct 41-43. Then the you simply need to plug in power and ground. If it is blinking that implies that you probably have that set. Make sure the power is going to a 5v (Vcc connection) and not a Servo Battery voltage. Note on my arc32, I don't use one of these cables, I use to servo extension cables instead...

Kurt


Top
 Profile  
 
 Post subject: Re: Arc32 and Ps2 controller
PostPosted: Wed Apr 20, 2011 4:49 pm 
Offline
Citizen

Joined: Mon Mar 28, 2011 5:18 pm
Posts: 10
The prebuilt cable from Lynx does work perfectly.
Just plug it in the Aux2 even number pins with the brown wire on pin 2.
Then get power from Aux1, Pin 7 is ground - Pin 8 is power.
Thanks for the help


Top
 Profile  
 
 Post subject: Re: Arc32 and Ps2 controller
PostPosted: Fri Apr 22, 2011 10:43 am 
Offline
Citizen

Joined: Mon Mar 28, 2011 5:18 pm
Posts: 10
To help myself with the learning curve since I'm new at code,
Im using the Parallax basic stamp 2 kit.
I'm not giving up on getting the Arc32 working.
I need this working very bad to control all 32 servos with the Lynx Ps2.
To control my life-size R2D2.
Kurt has been allot of help, but Im too far behind.
When BasicM gets tutorials going, could you include a very basic code for the Ps2 to
Control servos for the beginners.
Thanks


Top
 Profile  
 
 Post subject: Re: Arc32 and Ps2 controller
PostPosted: Fri Apr 22, 2011 2:08 pm 
Offline
Citizen

Joined: Mon Mar 28, 2011 5:18 pm
Posts: 10
I have found a code located here:
http://www.lynxmotion.com/images/html/build024.htm
And it does work the first 2 servos.
But I thought the Hservo had to be at the start of the code.


Top
 Profile  
 
 Post subject: Re: Arc32 and Ps2 controller
PostPosted: Fri Apr 22, 2011 6:03 pm 
Offline
Master

Joined: Tue Nov 21, 2006 9:34 am
Posts: 527
Hi again,

That program uses the non hardware assisted servo commands, which I don't know if they will work on all servo pins on the Arc32 or not. But it is easy to convert the PS2 output to drive some servos..

I don't have the time to write the complete code here, but I can give you a general outline for it. Suppose you wish to take the output of the 4 joysticks to directly drive 4 servos lets say on pins 0-3. Then your code could do something like:
Code:
Main:
   gosub Ps2Input  ; or whatever you call it or it could be inline...

    ; Assume DualShock array has the new values and DSPrev array has previous values.   
    for i = 3 to 6    ; need to define i some where... Joystick values in this part of the array...
        if DualShock(i) <> DSPrev(i) then
            DSPrev(i) = DualShock(i);
            ; Need to convert the joystick (Range 0-255) values into HSERVO values
            ; Assume a value of 128 should give us a 1500us pulse (center) and we wish for a range
            ; of somewhere between 750-2250us.  We need to convert this range into HSERVO values, which are
            ; based on clock tics of the processor instead, with 0 implying 1500us. Which implies that we need a HSERVO
            ; range of +- 15000 (delta of +-750us * 20 (mhz to us))
            ;which if you do the math implies maybe a multiplier of about 118 will do. so something like:
            HSERVO [i-3\(DualShock(i)-128)*118]
        endif
    next
goto main

Again I may have missed something. Also note you could also keep telling the servo to go back to the same place, but I often try to avoid it...

Kurt


Top
 Profile  
 
 Post subject: Re: Arc32 and Ps2 controller
PostPosted: Sat Apr 23, 2011 6:48 pm 
Offline
Citizen

Joined: Mon Mar 28, 2011 5:18 pm
Posts: 10
Thanks Kurt,
I appreciate the help but I'm going to have to learn more before I understand.
I read your reply several times and I just don't know enough.
I'll be back as soon as I know more about basic.
Thanks
Calvin


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

phpBB SEO