Shop OBEX P1 Docs P2 Docs Learn Events
Reading from a USB joystick — Parallax Forums

Reading from a USB joystick

alphyalphy Posts: 12
edited 2008-03-31 17:15 in Learn with BlocklyProp
I am writing the code for my school robot
I am very new to Parallax and was wondering how i read from out controller, a USB Joystick.
I am pretty sure that i have to use SERIn and SEROUT, but i could be wrong, any input would be greatly appreciated

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-03-20 00:52
    You can't use a USB joystick directly with a Stamp. The only way you can use a USB device with a Stamp is with some kind of USB host controller like Parallax's Memory Stick Datalogger or Vinculum's VDIP1 or GHI Electronics' USBwiz.

    You use SERIN and SEROUT with the USB host controller and the controller provides the proper signals for the USB device.
  • alphyalphy Posts: 12
    edited 2008-03-20 01:14
    well im going to run it into a laptop then from the laptop to the BASIC Stamp Homework Board we are using.
  • Mike GreenMike Green Posts: 23,101
    edited 2008-03-20 01:21
    That's a very different design. You normally would use SERIN / SEROUT to talk to the laptop. You'll have to develop a program on the laptop that will take the joystick information and send it to the Stamp. Keep in mind that the Stamp can only do one thing at a time. When it's not actually executing a SERIN statement, it will miss any data being transmitted from the PC. Usually, this is managed by having the Stamp ask the PC for the data, then waiting for an answer with a SERIN.
  • alphyalphy Posts: 12
    edited 2008-03-20 01:42
    well, my teacher did it a few months ago but he was just instructed specifically and we ow added a motor and changed the design so we need to completely redo it


    heres the code



    ' {$STAMP BS2}
    ' {$PBASIC 2.5}


    '*** PIN Declarations *** 'Rename the Motors to better fit your ROV
    MOTOR_0_A PIN 2 'Direction PIN A for motor0
    MOTOR_0_B PIN 1 'Direction PIN B for motor0
    MOTOR_0_E PIN 0 'Enable PIN for motor0 (use for PWM)

    MOTOR_1_A PIN 5 'Direction PIN A for motor1
    MOTOR_1_B PIN 4 'Direction PIN A for motor1
    MOTOR_1_E PIN 3 'Enable PIN for motor0 (use for PWM)

    MOTOR_2_A PIN 8 'Direction PIN A for motor2
    MOTOR_2_B PIN 7 'Direction PIN A for motor2
    MOTOR_2_E PIN 6 'Enable PIN for motor0 (use for PWM)

    'SENSOR PIN 15 'Currently not used

    '*** Asign Variables ***
    motor0 VAR Byte 'Forward/Backward movement
    motor1 VAR Byte 'Left/Right movement
    motor2 VAR Byte 'Up/Down movement
    y VAR Byte ' FW/BK dir
    x VAR Byte ' L/R dir
    z VAR Byte ' U/D
    y_word VAR Word ' FW/BK dir
    x_word VAR Word ' L/R dir



    motor0_holder VAR Byte 'Forward/Backward Direction
    motor1_holder VAR Byte 'Left/Right direction
    motor2_holder VAR Byte 'Up/Down direction

    a VAR Word
    b VAR Word


    repeat VAR Word 'repeat variable
    freq VAR Word 'Frequency variable
    freq_holder VAR Byte 'Frequency variable

    results VAR Word 'store raw sensor input
    sensor_data VAR Word 'store calibrated sample



    Main:
    freq_holder=255 'The begining frequency number
    'Communication**************


    HIGH 15
    PAUSE 1
    RCTIME 15,1 , results

    sensor_data= -20 * results + 35048

    sensor_data = sensor_data / 1000

    'sensor_data = results

    SEROUT 16, 16780, [noparse][[/noparse]sensor_data]

    SERIN 16, 16780, [noparse][[/noparse]x] 'serial input=pin 1, 2400, motor0
    SERIN 16, 16780, [noparse][[/noparse]y] 'serial input=pin 1, 2400, motor1
    SERIN 16, 16780, [noparse][[/noparse]z] 'serial input=pin 1, 2400, motor2

    'Sensor Goes Here***********



    'sensor_data = 34
    'SEROUT 16, 16780, [noparse][[/noparse]sensor_data]

    'Motor Manipulation Here****

    'DEBUG SDEC x ,CR
    'DEBUG SDEC y , CR

    IF x//2=0 THEN
    x_Word=x/2
    ELSE
    x_Word=x/2 * -1
    ENDIF

    IF y//2=0 THEN
    y_Word=y/2
    ELSE
    y_Word=y/2 * -1
    ENDIF

    a = x_word + y_word
    b = y_word - x_word

    'DEBUG SDEC x_word ,CR
    'DEBUG SDEC y_word , CR

    'DEBUG SDEC a , CR
    'DEBUG SDEC b , CR

    IF (a < 65409) AND (a > 32767) THEN
    a = -127

    ENDIF
    IF (a > 127) AND (a < 32767) THEN
    a=127
    ENDIF

    IF (b < 65409) AND (b > 32767) THEN
    b= -127

    ENDIF
    IF (b > 127) AND (b < 32767) THEN
    b=127
    ENDIF
    'DEBUG SDEC a , CR
    'DEBUG SDEC b , CR

    IF a > 32767 THEN 'test for odd number
    a = ABS a
    a = a * 2
    a = a + 1 'makes it odd
    ELSE
    a = a * 2
    ENDIF

    IF b > 32767 THEN 'test for odd number
    b = ABS b
    b = b * 2
    b = b + 1 'makes it odd
    ELSE
    b = b * 2
    ENDIF


    'DEBUG DEC a , CR
    'DEBUG DEC b , CR




    motor0_holder = a
    motor1_holder = b
    motor2_holder = z
    'DIRECTION****************** '**** what does "//" do, use help to find out ****
    IF motor0_holder//2=0 THEN 'determines if byte is even or odd for motor0
    HIGH MOTOR_0_A
    LOW MOTOR_0_B
    ELSE
    LOW MOTOR_0_A
    HIGH MOTOR_0_B
    ENDIF 'ends the statement

    IF motor1_holder//2=0 THEN 'determines if byte is even or odd for motor1
    HIGH MOTOR_1_A
    LOW MOTOR_1_B
    ELSE
    LOW MOTOR_1_A
    HIGH MOTOR_1_B
    ENDIF 'ends statement

    IF motor2_holder//2=0 THEN 'determines if byte is even or odd for motor2
    HIGH MOTOR_2_A
    LOW MOTOR_2_B
    ELSE
    LOW MOTOR_2_A
    HIGH MOTOR_2_B
    ENDIF 'ends statement



    'SPEED******** 'This loop controls the PWM to the motor
    repeat=5 'Sets variable 'repeat' at 1
    Repeat_loop: 'Starts main loop
    motor0=motor0_holder/10 'The next four lines are equations that takes the byte
    motor1=motor1_holder/10 'sent down and divides it by 10, this lowers the
    motor2=motor2_holder/10 'resolution but speeds up the loop. Adjust as needed.
    freq=freq_holder/10

    ' The following 3 If test are used to set the Dead band of the
    ' controller (point the control must pass before it turns on).
    ' increase number to increase deadband.
    IF motor0 > 3 THEN HIGH MOTOR_0_E 'This causes pin 3 to go high, turning on the motor
    IF motor1 > 3 THEN HIGH MOTOR_1_E 'This causes pin 4 to go high, turning on the motor
    IF motor2 > 3 THEN HIGH MOTOR_2_E 'This causes pin 5 to go high, turning on the motor

    Frequency_Loop: 'Starts PWM loop
    IF motor0 = 0 THEN 'determines if byte equals zero(0)
    LOW MOTOR_0_E 'if byte is 0, set emable pin low, turning off the motor,
    'if not 0, pin 3 remains high
    ENDIF 'ends statement

    IF motor1 = 0 THEN 'determines if byte equals zero(0)
    LOW MOTOR_1_E 'if byte is 0, set emable pin low, turning off the motor,
    'if not 0, pin 4 remains high
    ENDIF 'ends statement

    IF motor2 = 0 THEN 'determines if byte equals zero(0)
    LOW MOTOR_2_E 'if byte is 0, set emable pin low, turning off the motor,
    'if not 0, pin 5 remains high
    ENDIF 'ends statement

    motor0 = motor0 - 3 'subtracts varialbe by 1 within PWM loop
    motor1 = motor1 - 3 'subtracts varialbe by 1 within PWM loop
    motor2 = motor2 - 3 'subtracts varialbe by 1 within PWM loop
    freq = freq - 3 'subtracts varialbe by 1 within PWM loop

    IF freq <>0 THEN
    GOTO Frequency_Loop 'as long as freq does not equal zero, the PWM loop repeats
    ENDIF
    repeat=repeat-1 'subtracts varialbe by 1 after freq=0 and PWM loop stops
    IF repeat<> 0 THEN
    GOTO Repeat_loop 'as long as repeat does not equal zero, the reapeat loop repeats
    ENDIF
    GOTO Main 'go back to run again
    END 'Ends the program
  • Mike GreenMike Green Posts: 23,101
    edited 2008-03-20 02:01
    Do you have a question now?
  • alphyalphy Posts: 12
    edited 2008-03-20 18:33
    yea how would i do that? i dont know how to talk to the joystick
  • Mike GreenMike Green Posts: 23,101
    edited 2008-03-20 18:59
    You'll have to talk to your teacher about what was done before.

    You're asking for a simple answer for something that would take weeks to teach someone.
    So much depends on what tools you have on hand like programming languages, compilers,
    etc. You'd be much better off if you had some kind of joystick with just some switches and
    variable resistors in it that a Stamp very easily could access directly. USB introduces a whole
    other level of complexity.
  • crgwbrcrgwbr Posts: 614
    edited 2008-03-21 02:43
    I have to agree with mike here. USB is a very complex protocol working at speeds up to 480Mbps. The Stamp tops out at about 38.4 Kbps. The best chance you have is connecting it to the USBwiz chip. The FTDI Vinculum would also work, but from what I've heard, the USBwiz has better HID support.But even then, your going to have a lot of work to do for the interface. To be honest, I'm not really sure that one BS2 could do it; you may have to look into using a few SX's or a Prop. Long story short, you can't just plug in the RX and TX lines of the USB connector and expect it to work.

    Anyway though, you should certainly try to get the project working.

    Regards,
    Craig

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    My system: 1.6 GHz AMD Turion64 X2, 4GB DDR2, 256MB ATI Radeon Graphics card, 15.4" Widescreen HD Screen

    I have a duel boot of Ubuntu Linux and Windows Vista. Vista, because it came with the PC, Ubuntu because I like software that works.

    "Failure is not an option -- it comes bundled with Windows."

    Use The Best...
    Linux for Servers
    Mac for Graphics
    Palm for Mobility
    Windows for Solitaire
  • alphyalphy Posts: 12
    edited 2008-03-27 13:49
    The code up there they used before, and it worked. But i cannot figure out why, like what is port 16?
  • Mike GreenMike Green Posts: 23,101
    edited 2008-03-27 14:38
    Port 16 is the programming port on the Stamp and is the serial port used by the DEBUG and DEBUGIN statements. The DEBUG statement is just a special case of the SEROUT statement with a fixed Baud and using port 16. There are some limitations with port 16. For example, anything sent to the Stamp is echoed back to the sender. Also, if the DTR handshake line is toggled off and on (like when the serial port is opened on the PC side), the Stamp will reset.

    There must be some kind of program running on the PC that uses the joystick and communicates with the Stamp over the serial port.
    Again, you need more information from your teacher.
  • alphyalphy Posts: 12
    edited 2008-03-28 23:50
    Wow, taht was EXTREMELY helpful, thank you
    now we just need to igure the rest of it out
    is there a way to have parallax access a progrom?
    i have a program that does that, just no idea how to use it.

    Post Edited (alphy) : 3/29/2008 12:04:01 AM GMT
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2008-03-29 04:22
    alphy stated...
    i have a program that does that, just no idea how to use it.
    And what program would that be?
  • alphyalphy Posts: 12
    edited 2008-03-29 13:04
    a program that the people that run the competition gate us, its called MATE ROV control
  • Mike GreenMike Green Posts: 23,101
    edited 2008-03-29 14:54
    You need to ask your teacher for help in finding the manual or documentation for the program.
    I don't think we can help you here with that.
  • alphyalphy Posts: 12
    edited 2008-03-31 17:15
    Thanks for all your help, you have been very helpful.
Sign In or Register to comment.