Reading from a USB joystick
alphy
Posts: 12
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
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
You use SERIN and SEROUT with the USB host controller and the controller provides the proper signals for the USB device.
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
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.
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
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.
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
I don't think we can help you here with that.