Pages: [1] 2   Go Down
Author Topic: PLEASE HELP!  (Read 1077 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey there,i would like to ask if are you guys able to help me with my
> coding?i have already a code that is able to use a gaming joystick to
> control 6 servo motors.the thing is i need to make it wirelessly,is
> there anyway you can help?firstly i need to initialize python servo.py
> then multijoystick.py and it will work.But i need to do it wirelessly
> via xBees.how do i do it?do i need to break the codes into 2 parts?i
> can send you guys the codes if you need!THANKS!
Logged

Offline Offline
Edison Member
*
Karma: 17
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Post what code you have already (use [ code ] tags by clicking on the #).

You're going to need to have two arduinos and two programs. Are you planning on:
joystick -> computer OS -> python -> usb -> arduino -> xbee -> xbee -> arduino -> servos

You can cut out the first arduino by using something like this:
http://www.sparkfun.com/products/8687

Your program running on the computer won't have anything to do with servos, so you won't need any python libraries for them - it will just be sending the raw values of the joystick to the arduino with the servos.

Try just getting the first section to work -- the computer reading the joystick and sending the values over xbee to an arduino which prints the values on the serial monitor. Once you have that working, you can easily modify your current code to use the numbers transmitted over xbee.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is arduino code


Code:

/*


 * MultipleServos
 * --------------
 * Arduino servo control from a PC
 *
 * Created: 2 April 2008
 * Author:  Brian D. Wendt
 *   http://principialabs.com/
 * License: GPLv3, copyleft 2008
 *   http://www.fsf.org/licensing/
 *
 * Adapted from code by Tom Igoe
 *   http://itp.nyu.edu/physcomp/Labs/Servo
 */

/** Adjust these values for your servo and setup, if necessary **/
int pinArray[6] = {11, 10, 9, 5, 6, 3}; // digital pins for the servos
int minPulse = 500;             // minimum servo position
int maxPulse = 2500;            // maximum servo position
int refreshTime =  20;          // time (ms) between pulses (50Hz)

/** The Arduino will calculate these values for you **/
int i;              // iterator
int servoPin;       // control pin for current servo
int userInput[3];   // raw input from serial buffer, 3 bytes
int pulseWidth;     // servo pulse width
int servoPosition;  // commanded servo position, 0-180 degrees
int pulseRange;     // maxPulse - minPulse
int centerServo;    // servo starting point
long lastPulse = 0; // recorded time (ms) of the last pulse
int servo;          // which servo to pulse? 1-4
int servo1[2];      // servo #1 array{pin, pulsewidth}
int servo2[2];      // servo #2 array{pin, pulsewidth}
int servo3[2];      // servo #3 array{pin, pulsewidth}
int servo4[2];      // servo #4 array{pin, pulsewidth}
int servo5[2];      // servo #5 array{pin, pulsewidth}
int servo6[2];      // servo #6 array{pin, pulsewidth}
int pin;            // digital pin for pulse() function
int puls;           // pulsewidth for pulse() function
int startbyte;      // start byte, begin reading input

void setup() {
  // loop through all 4 servo pins
  // and set them as OUTPUT
  for (i=0;i<6;i++) {
    pinMode(pinArray[i], OUTPUT);
  }  
  // servo starting point (center)
  pulseRange  = maxPulse - minPulse;
  centerServo = maxPulse - ((pulseRange)/2);
  pulseWidth  = centerServo;
  // map pins to servos
  servo1[0] = pinArray[0];  // servo #1 is pin 2
  servo2[0] = pinArray[1];  // servo #2 is pin 3
  servo3[0] = pinArray[2];  // servo #3 is pin 4
  servo4[0] = pinArray[3];  // servo #4 is pin 5
  servo5[0] = pinArray[4];  // servo #5 is pin 6
  servo6[0] = pinArray[5];  // servo #6 is pin 7
  // center all servos
  servo1[1] = pulseWidth;
  servo2[1] = pulseWidth;
  servo3[1] = pulseWidth;
  servo4[1] = pulseWidth;
  servo5[1] = pulseWidth;
  servo6[1] = pulseWidth;
 
  // open serial connection
  Serial.begin(9600);
}

void loop() {
  // wait for serial input (min 3 bytes in buffer)
  if (Serial.available() > 2) {
    //read the first byte
    startbyte = Serial.read();
    // if it's really the startbyte (255)
    if (startbyte == 255) {
      // then get the next two bytes
      for (i=0;i<2;i++) {
        userInput[i] = Serial.read();
      }
      // first byte = servo to move?
      servo = userInput[0];
      // second byte = which position?
      servoPosition = userInput[1];
      // packet check
      if (servoPosition == 255) { servo = 255; }
      // compute pulseWidth from servoPosition
      pulseWidth = minPulse + (servoPosition * (pulseRange/180));
      // stop servo pulse at min and max
      if (pulseWidth > maxPulse) { pulseWidth = maxPulse; }
      if (pulseWidth < minPulse) { pulseWidth = minPulse; }
      // assign new pulsewidth to appropriate servo
      switch (servo) {
        case 1:
          servo1[1] = pulseWidth;
          break;
        case 2:
          servo2[1] = pulseWidth;
          break;
        case 3:
          servo3[1] = pulseWidth;
          break;
        case 4:
          servo4[1] = pulseWidth;
          break;
        case 5:
          servo5[1] = pulseWidth;
          break;
        case 6:
          servo6[1] = pulseWidth;
          break;
      }
    }
  }
  // pulse each servo
  if (millis() - lastPulse >= refreshTime) {
    pulse(servo1[0], servo1[1]);
    pulse(servo2[0], servo2[1]);
    pulse(servo3[0], servo3[1]);
    pulse(servo4[0], servo4[1]);
    pulse(servo5[0], servo5[1]);
    pulse(servo6[0], servo6[1]);
    // save the time of the last pulse
    lastPulse = millis();
  }
}

void pulse(int pin, int puls) {
    digitalWrite(pin, HIGH); // start the pulse
    delayMicroseconds(puls); // pulse width
    digitalWrite(pin, LOW);  // stop the pulse
}

Moderator edit: One set of [ code ] tags is enough.
« Last Edit: November 16, 2011, 02:42:17 am by AWOL » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
This is python multijoystick code


#!/usr/bin/env python

################################################################
# Module:   multijoystick.py
# Created:  2 April 2008
# Author:   Brian D. Wendt
#   http://principialabs.com/
# Version:  0.2
# License:  GPLv3
#   http://www.fsf.org/licensing/
'''
Provides four-axis joystick servo control from a PC
using the Arduino "MultipleServos" sketch
and the Python "servo.py" serial abstraction module.

Parts of this code were adapted from:
  http://svn.lee.org/swarm/trunk/mothernode/python/multijoy.py

NOTE: This script requires the following Python modules:
  pyserial - http://pyserial.sourceforge.net/
  pygame   - http://www.pygame.org/
  servo    - http://principialabs.com/
 Win32 users may also need:
  pywin32  - http://sourceforge.net/projects/pywin32/
'''
################################################################

import servo
import pygame

# allow multiple joysticks
joy = []

# handle joystick event
def handleJoyEvent(e):
    if e.type == pygame.JOYAXISMOTION:
        axis = "unknown"
        if (e.dict['axis'] == 0):
            axis = "X"
            print(e.dict['axis']),

        if (e.dict['axis'] == 1):
            axis = "Y"
            print(e.dict['axis']),

        if (e.dict['axis'] == 2):
            axis = "Throttle"
            print(e.dict['axis']),

        if (e.dict['axis'] == 3):
            axis = "Z"
            print(e.dict['axis']),

        if (axis != "unknown"):
            str = "Axis: %s; Value: %f" % (axis, e.dict['value'])
            # uncomment to debug
            #output(str, e.dict['joy'])

            # Arduino joystick-servo hack
            if (axis == "X"):
                pos = e.dict['value']
                
                # convert joystick position to servo increment, 0-180
                move = round(pos * 90, 0)
                if (move < 0):
                    serv = int(60 - abs(move)/2.5)
                    print"Roll Left\n";
                else:
                    serv = int((move/2.5) + 60)
                    print"Roll Right\n";
                # convert position to ASCII character
                servoPosition = serv
                #print(servoPosition);
                # and send to Arduino over serial connection
                servo.move(1, servoPosition)

                 # Arduino joystick-servo hack
            if (axis == "X"):
                pos = e.dict['value']
                
                # convert joystick position to servo increment, 0-180
                move = round(pos * 90, 0)
                if (move > 0):
                    serv = int((-move/2.5) + 90)  
                else:
                    serv = int(90 + abs(move)/2.5)
                # convert position to ASCII character
                servoPosition = serv
                #print(servoPosition);
                # and send to Arduino over serial connection
                servo.move(5, servoPosition)


            # Arduino joystick-servo hack
            if (axis == "Y"):
                pos = e.dict['value']
              
                # convert joystick position to servo increment, 0-180
                move = round(pos * 90, 0)
                if (move < 0):
                    serv = int(56 - abs(move)/2.5)
                    print "Pitch Down\n";
                else:
                    serv = int((move/2.5) + 56)
                    print "Pitch Up\n";
                # convert position to ASCII character
                servoPosition = serv
                #print(servoPosition);
                # and send to Arduino over serial connection
                servo.move(2, servoPosition)
              

                # Arduino joystick-servo hack
            if (axis == "Y"):
                pos = e.dict['value']
              
                # convert joystick position to servo increment, 0-180
                move = round(pos * 90, 0)
                if (move < 0):
                    serv = int((90 - abs(move)*1.2))
                    print "Pitch Down\n";
                else:
                    serv = int(((move/2.5) + 90)*1.2)
                    print "Pitch Up\n";
                # convert position to ASCII character
                servoPosition = serv
                #print(servoPosition);
                # and send to Arduino over serial connection
                servo.move(4, servoPosition)

            # Arduino joystick-servo hack
            if (axis == "Z"):
                pos = e.dict['value']
                # convert joystick position to servo increment, 0-180
                move = round(pos * 90, 0)
                if (move < 0):
                    serv = int(65 - abs(move)/2)
                    print "Yaw Left\n";
                else:
                    serv = int((move/2) + 65)
                    print "Yaw Right\n";
                # convert position to ASCII character
                servoPosition = serv
                #print(servoPosition);
                # and send to Arduino over serial connection
                servo.move(3, servoPosition)
                

                # Arduino joystick-servo hack
            if (axis == "Z"):
                pos = e.dict['value']
                # convert joystick position to servo increment, 0-180
                move = round(pos * 90, 0)
                if (move < 0):
                    serv = int(90 - abs(move)/2)
                    print "Yaw Left\n";
                else:
                    serv = int((move/2) + 90)
                    print "Yaw Right\n";
                # convert position to ASCII character
                servoPosition = serv
                #print(servoPosition);
                # and send to Arduino over serial connection
                servo.move(6, servoPosition)


      


    elif e.type == pygame.JOYBUTTONDOWN:
        str = "Button: %d" % (e.dict['button'])
        # uncomment to debug
        #output(str, e.dict['joy'])
        # Button 0 (trigger) to quit
        if (e.dict['button'] == 0):
            print "Bye!\n"
            quit()
    else:
        pass

# print the joystick position
def output(line, stick):
    print "Joystick: %d; %s" % (stick, line)

# wait for joystick input
def joystickControl():
    while True:
        e = pygame.event.wait()
        if (e.type == pygame.JOYAXISMOTION or e.type == pygame.JOYBUTTONDOWN):
            handleJoyEvent(e)

# main method
def main():
    # initialize pygame
    pygame.joystick.init()
    pygame.display.init()
    if not pygame.joystick.get_count():
        print "\nPlease connect a joystick and run again.\n"
        quit()
    print "\n%d joystick(s) detected." % pygame.joystick.get_count()
    for i in range(pygame.joystick.get_count()):
        myjoy = pygame.joystick.Joystick(i)
        myjoy.init()
        joy.append(myjoy)
        print "Joystick %d: " % (i) + joy[i].get_name()
    print "Depress trigger (button 0) to quit.\n"

    # run joystick listener loop
    joystickControl()

# allow use as a module or standalone script
if __name__ == "__main__":
    main()
« Last Edit: November 15, 2011, 09:43:13 pm by lyovlee » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is python is servo.py code

Code:
#!/usr/bin/env python

################################################
# Module:   servo.py
# Created:  2 April 2008
# Author:   Brian D. Wendt
#   http://principialabs.com/
# Version:  0.2
# License:  GPLv3
#   http://www.fsf.org/licensing/
'''
Provides a serial connection abstraction layer
for use with Arduino "MultipleServos" sketch.
'''
################################################

import serial

usbport = 'COM3'
ser = serial.Serial(usbport, 9600, timeout=1)
#print ser

def move(servo, angle):
    '''Moves the specified servo to the supplied angle.

    Arguments:
        servo
          the servo number to command, an integer from 1-4
        angle
          the desired servo angle, an integer from 0 to 180

    (e.g.) >>> servo.move(2, 90)
           ... # "move servo #2 to 90 degrees"'''

    if (0 <= angle <= 180):
        ser.write(chr(255))
        ser.write(chr(servo))
        ser.write(chr(angle))
    else:
        print "Servo angle must be an integer between 0 and 180.\n"
« Last Edit: November 16, 2011, 02:42:43 am by AWOL » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi i have uploaded the codes and thanks for your reply and yes the way you suggested it to work is what i intend to do,but do you have any idea how to do it?cause i have not much knowledge in wireless and programming thanks!
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24317
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Please read the first line of reply #1, then go back and edit your posts.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Please read the first line of reply #1, then go back and edit your posts.
This is arduino code


Moderator edit: This is now correctly posted Arduin code.

Code:
/*


 * MultipleServos
 * --------------
 * Arduino servo control from a PC
 *
 * Created: 2 April 2008
 * Author:  Brian D. Wendt
 *   http://principialabs.com/
 * License: GPLv3, copyleft 2008
 *   http://www.fsf.org/licensing/
 *
 * Adapted from code by Tom Igoe
 *   http://itp.nyu.edu/physcomp/Labs/Servo
 */

/** Adjust these values for your servo and setup, if necessary **/
int pinArray[6] = {11, 10, 9, 5, 6, 3}; // digital pins for the servos
int minPulse = 500;             // minimum servo position
int maxPulse = 2500;            // maximum servo position
int refreshTime =  20;          // time (ms) between pulses (50Hz)

/** The Arduino will calculate these values for you **/
int i;              // iterator
int servoPin;       // control pin for current servo
int userInput[3];   // raw input from serial buffer, 3 bytes
int pulseWidth;     // servo pulse width
int servoPosition;  // commanded servo position, 0-180 degrees
int pulseRange;     // maxPulse - minPulse
int centerServo;    // servo starting point
long lastPulse = 0; // recorded time (ms) of the last pulse
int servo;          // which servo to pulse? 1-4
int servo1[2];      // servo #1 array{pin, pulsewidth}
int servo2[2];      // servo #2 array{pin, pulsewidth}
int servo3[2];      // servo #3 array{pin, pulsewidth}
int servo4[2];      // servo #4 array{pin, pulsewidth}
int servo5[2];      // servo #5 array{pin, pulsewidth}
int servo6[2];      // servo #6 array{pin, pulsewidth}
int pin;            // digital pin for pulse() function
int puls;           // pulsewidth for pulse() function
int startbyte;      // start byte, begin reading input

void setup() {
  // loop through all 4 servo pins
  // and set them as OUTPUT
  for (i=0;i<6;i++) {
    pinMode(pinArray, OUTPUT);
  }  
  // servo starting point (center)
  pulseRange  = maxPulse - minPulse;
  centerServo = maxPulse - ((pulseRange)/2);
  pulseWidth  = centerServo;
  // map pins to servos
  servo1[0] = pinArray[0];  // servo #1 is pin 2
  servo2[0] = pinArray[1];  // servo #2 is pin 3
  servo3[0] = pinArray[2];  // servo #3 is pin 4
  servo4[0] = pinArray[3];  // servo #4 is pin 5
  servo5[0] = pinArray[4];  // servo #5 is pin 6
  servo6[0] = pinArray[5];  // servo #6 is pin 7
  // center all servos
  servo1[1] = pulseWidth;
  servo2[1] = pulseWidth;
  servo3[1] = pulseWidth;
  servo4[1] = pulseWidth;
  servo5[1] = pulseWidth;
  servo6[1] = pulseWidth;
 
  // open serial connection
  Serial.begin(9600);
}

void loop() {
  // wait for serial input (min 3 bytes in buffer)
  if (Serial.available() > 2) {
    //read the first byte
    startbyte = Serial.read();
    // if it's really the startbyte (255)
    if (startbyte == 255) {
      // then get the next two bytes
      for (i=0;i<2;i++) {
        userInput = Serial.read();
      }
      // first byte = servo to move?
      servo = userInput[0];
      // second byte = which position?
      servoPosition = userInput[1];
      // packet check
      if (servoPosition == 255) { servo = 255; }
      // compute pulseWidth from servoPosition
      pulseWidth = minPulse + (servoPosition * (pulseRange/180));
      // stop servo pulse at min and max
      if (pulseWidth > maxPulse) { pulseWidth = maxPulse; }
      if (pulseWidth < minPulse) { pulseWidth = minPulse; }
      // assign new pulsewidth to appropriate servo
      switch (servo) {
        case 1:
          servo1[1] = pulseWidth;
          break;
        case 2:
          servo2[1] = pulseWidth;
          break;
        case 3:
          servo3[1] = pulseWidth;
          break;
        case 4:
          servo4[1] = pulseWidth;
          break;
        case 5:
          servo5[1] = pulseWidth;
          break;
        case 6:
          servo6[1] = pulseWidth;
          break;
      }
    }
  }
  // pulse each servo
  if (millis() - lastPulse >= refreshTime) {
    pulse(servo1[0], servo1[1]);
    pulse(servo2[0], servo2[1]);
    pulse(servo3[0], servo3[1]);
    pulse(servo4[0], servo4[1]);
    pulse(servo5[0], servo5[1]);
    pulse(servo6[0], servo6[1]);
    // save the time of the last pulse
    lastPulse = millis();
  }
}

void pulse(int pin, int puls) {
    digitalWrite(pin, HIGH); // start the pulse
    delayMicroseconds(puls); // pulse width
    digitalWrite(pin, LOW);  // stop the pulse
}
« Last Edit: November 16, 2011, 02:39:07 am by AWOL » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey there,i would like to ask if are you guys able to help me with my
> coding?i have already a code that is able to use a gaming joystick to
> control 6 servo motors.the thing is i need to make it wirelessly,is
> there anyway you can help?firstly i need to initialize python servo.py
> then multijoystick.py and it will work.But i need to do it wirelessly
> via xBees.how do i do it?do i need to break the codes into 2 parts?i
> can send you guys the codes if you need!THANKS!
This is python multijoystick code

Moderator edit: This is now correctly posted python code.
Code:
#!/usr/bin/env python

################################################################
# Module:   multijoystick.py
# Created:  2 April 2008
# Author:   Brian D. Wendt
#   http://principialabs.com/
# Version:  0.2
# License:  GPLv3
#   http://www.fsf.org/licensing/
'''
Provides four-axis joystick servo control from a PC
using the Arduino "MultipleServos" sketch
and the Python "servo.py" serial abstraction module.

Parts of this code were adapted from:
  http://svn.lee.org/swarm/trunk/mothernode/python/multijoy.py

NOTE: This script requires the following Python modules:
  pyserial - http://pyserial.sourceforge.net/
  pygame   - http://www.pygame.org/
  servo    - http://principialabs.com/
 Win32 users may also need:
  pywin32  - http://sourceforge.net/projects/pywin32/
'''
################################################################

import servo
import pygame

# allow multiple joysticks
joy = []

# handle joystick event
def handleJoyEvent(e):
    if e.type == pygame.JOYAXISMOTION:
        axis = "unknown"
        if (e.dict['axis'] == 0):
            axis = "X"
            print(e.dict['axis']),

        if (e.dict['axis'] == 1):
            axis = "Y"
            print(e.dict['axis']),

        if (e.dict['axis'] == 2):
            axis = "Throttle"
            print(e.dict['axis']),

        if (e.dict['axis'] == 3):
            axis = "Z"
            print(e.dict['axis']),

        if (axis != "unknown"):
            str = "Axis: %s; Value: %f" % (axis, e.dict['value'])
            # uncomment to debug
            #output(str, e.dict['joy'])

            # Arduino joystick-servo hack
            if (axis == "X"):
                pos = e.dict['value']
                
                # convert joystick position to servo increment, 0-180
                move = round(pos * 90, 0)
                if (move < 0):
                    serv = int(60 - abs(move)/2.5)
                    print"Roll Left\n";
                else:
                    serv = int((move/2.5) + 60)
                    print"Roll Right\n";
                # convert position to ASCII character
                servoPosition = serv
                #print(servoPosition);
                # and send to Arduino over serial connection
                servo.move(1, servoPosition)

                 # Arduino joystick-servo hack
            if (axis == "X"):
                pos = e.dict['value']
                
                # convert joystick position to servo increment, 0-180
                move = round(pos * 90, 0)
                if (move > 0):
                    serv = int((-move/2.5) + 90)  
                else:
                    serv = int(90 + abs(move)/2.5)
                # convert position to ASCII character
                servoPosition = serv
                #print(servoPosition);
                # and send to Arduino over serial connection
                servo.move(5, servoPosition)


            # Arduino joystick-servo hack
            if (axis == "Y"):
                pos = e.dict['value']
              
                # convert joystick position to servo increment, 0-180
                move = round(pos * 90, 0)
                if (move < 0):
                    serv = int(56 - abs(move)/2.5)
                    print "Pitch Down\n";
                else:
                    serv = int((move/2.5) + 56)
                    print "Pitch Up\n";
                # convert position to ASCII character
                servoPosition = serv
                #print(servoPosition);
                # and send to Arduino over serial connection
                servo.move(2, servoPosition)
              

                # Arduino joystick-servo hack
            if (axis == "Y"):
                pos = e.dict['value']
              
                # convert joystick position to servo increment, 0-180
                move = round(pos * 90, 0)
                if (move < 0):
                    serv = int((90 - abs(move)*1.2))
                    print "Pitch Down\n";
                else:
                    serv = int(((move/2.5) + 90)*1.2)
                    print "Pitch Up\n";
                # convert position to ASCII character
                servoPosition = serv
                #print(servoPosition);
                # and send to Arduino over serial connection
                servo.move(4, servoPosition)

            # Arduino joystick-servo hack
            if (axis == "Z"):
                pos = e.dict['value']
                # convert joystick position to servo increment, 0-180
                move = round(pos * 90, 0)
                if (move < 0):
                    serv = int(65 - abs(move)/2)
                    print "Yaw Left\n";
                else:
                    serv = int((move/2) + 65)
                    print "Yaw Right\n";
                # convert position to ASCII character
                servoPosition = serv
                #print(servoPosition);
                # and send to Arduino over serial connection
                servo.move(3, servoPosition)
                

                # Arduino joystick-servo hack
            if (axis == "Z"):
                pos = e.dict['value']
                # convert joystick position to servo increment, 0-180
                move = round(pos * 90, 0)
                if (move < 0):
                    serv = int(90 - abs(move)/2)
                    print "Yaw Left\n";
                else:
                    serv = int((move/2) + 90)
                    print "Yaw Right\n";
                # convert position to ASCII character
                servoPosition = serv
                #print(servoPosition);
                # and send to Arduino over serial connection
                servo.move(6, servoPosition)


      


    elif e.type == pygame.JOYBUTTONDOWN:
        str = "Button: %d" % (e.dict['button'])
        # uncomment to debug
        #output(str, e.dict['joy'])
        # Button 0 (trigger) to quit
        if (e.dict['button'] == 0):
            print "Bye!\n"
            quit()
    else:
        pass

# print the joystick position
def output(line, stick):
    print "Joystick: %d; %s" % (stick, line)

# wait for joystick input
def joystickControl():
    while True:
        e = pygame.event.wait()
        if (e.type == pygame.JOYAXISMOTION or e.type == pygame.JOYBUTTONDOWN):
            handleJoyEvent(e)

# main method
def main():
    # initialize pygame
    pygame.joystick.init()
    pygame.display.init()
    if not pygame.joystick.get_count():
        print "\nPlease connect a joystick and run again.\n"
        quit()
    print "\n%d joystick(s) detected." % pygame.joystick.get_count()
    for i in range(pygame.joystick.get_count()):
        myjoy = pygame.joystick.Joystick(i)
        myjoy.init()
        joy.append(myjoy)
        print "Joystick %d: " % (i) + joy.get_name()
    print "Depress trigger (button 0) to quit.\n"

    # run joystick listener loop
    joystickControl()

# allow use as a module or standalone script
if __name__ == "__main__":
    main()
« Last Edit: November 16, 2011, 02:40:11 am by AWOL » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey there,i would like to ask if are you guys able to help me with my
> coding?i have already a code that is able to use a gaming joystick to
> control 6 servo motors.the thing is i need to make it wirelessly,is
> there anyway you can help?firstly i need to initialize python servo.py
> then multijoystick.py and it will work.But i need to do it wirelessly
> via xBees.how do i do it?do i need to break the codes into 2 parts?i
> can send you guys the codes if you need!THANKS!

This is python is servo.py code

Moderator editL This is python...hopefully now, you get the drift.

Code:
#!/usr/bin/env python

################################################
# Module:   servo.py
# Created:  2 April 2008
# Author:   Brian D. Wendt
#   http://principialabs.com/
# Version:  0.2
# License:  GPLv3
#   http://www.fsf.org/licensing/
'''
Provides a serial connection abstraction layer
for use with Arduino "MultipleServos" sketch.
'''
################################################

import serial

usbport = 'COM3'
ser = serial.Serial(usbport, 9600, timeout=1)
#print ser

def move(servo, angle):
    '''Moves the specified servo to the supplied angle.

    Arguments:
        servo
          the servo number to command, an integer from 1-4
        angle
          the desired servo angle, an integer from 0 to 180

    (e.g.) >>> servo.move(2, 90)
           ... # "move servo #2 to 90 degrees"'''

    if (0 <= angle <= 180):
        ser.write(chr(255))
        ser.write(chr(servo))
        ser.write(chr(angle))
    else:
        print "Servo angle must be an integer between 0 and 180.\n"
« Last Edit: November 16, 2011, 02:41:15 am by AWOL » Logged

Offline Offline
Edison Member
*
Karma: 17
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No, go back and hit "modify" on each of your first three posts, highlight the entire section, then move your mouse up to above the text box, above the smilee faces, and find the button that looks like #. Click it, and there should be tags that look like [ code] and [/ code] and when you post it'll look much better (right now it's illegible) and not take up so much space.

Do you understand how the Serial communication works, what exactly an xbee does, besides "wireless"?

I believe that the easy answer is to get the XBee Explorer USB I mentioned earlier and an XBee shield, plug them in, and run your current programs (assuming they work)
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi i only know that i have to determine the xbee's role as a transmitter or a receiver.Using xctu.And i i just place the pair of xBee's onto arduino fio and upload the sketches into the 2 arduino and will work.The concept is there but i do not now how to apply to my current project(with python and using the USB jostick instead of analogue joystick).Do you have like a sample coding for me to use?Cause i am in a rush to finish my project!
Logged

Offline Offline
Edison Member
*
Karma: 17
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What hardware do you have (2 arduinos? 2 xbees? xbee shields?) and how are you setting up the "data flow," (how each thing talks to each other). Something like this:

USB joystick -> Computer (running Windows) -> Xbee explorer -> xbee -> xbee -> arduino fio -> servos
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yeap the way you suggest is workable too.Anyway is possible as long as it's wireless and involves xBees,Arduino and the USB joystick.Do you have a sample code or website?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey there,i would like to ask if are you guys able to help me with my
> coding?i have already a code that is able to use a gaming joystick to

What's with the > signs?

And can you please change the topic to something more than "PLEASE HELP!". Everyone wants help. Specifying what sort of help you want is more likely to get it. In other words, specify what is troubling you, eg. "Need help with joystick, Python, and Xbees".
Logged

Pages: [1] 2   Go Up
Jump to: