Lesson 3 - Controlling using Joystick

Lesson 3 - Controlling using Joystick

0 comments

PiArm can be controlled using any sensor, the application is up to the user. The joystick is a device which we have used to play games since our childhood. In this lesson, you will learn how joystick works and how you can integrate a joystick with your projects.

You will learn the setup of PiArm using Joystick, and the joystick setup itself.

Prepare the Joystick
Joystick
  • Insert AAA batteries into the joystick you got with the PiArm.

    batteries

  • Plug the dongle into any of the USB port of Raspberry Pi.

    dongle

  • Turn ON the joystick.

    Joystick 1

  • Go to PiArm directory.
  • Run the joystick.py file
Library
  • Installation

    The pygame library is preinstalled on Raspbian distros, you can check it using pip-

     pip3 freeze

    However, you can install it using-

     pip install pygame

    You can also upgrade the preinstalled pygame package using-

     sudo pip3 install --upgrade pygame
  • Joystick module

    Most of the joysticks have a key number for the buttons on the joystick. These numbers are used to identify which key is pressed. The left side keys of joysticks respond with there axis value and the other keys have a numeric value. The values of the keys may vary with the joysticks from various manufacturers. The pygame library has a joystick module to manage the joystick module in computers. Import the pygame module with:

     import pygame
    To Initialise the joystick module and get a list of Joystick instances, use the following code:
     pygame.joystick.init()
    joysticks = [pygame.joystick.Joystick(x) 
    for x in range(pygame.joystick.get_count())]

    The joysticks will generate the following event types:

    JOYAXISMOTION JOYBALLMOTION JOYBUTTONDOWN JOYBUTTONUP JOYHATMOTION

    To Uninitialized the joystick module use:

     pygame.joystick.quit()
    You can find the number of joysticks connected with your system using:
     pygame.joystick.get_count()
    To check the position of the axis of the joystick, use get_axis function which returns a float value between -1 to 1 with a value of 0 which means it is centred:
     pygame.joystick.Joystick.get_axis(axis_number)
    You can use the event module of pygame to track user activity with;
     pygame.event.get()
    Which returns events performed by the user. These events can be compared with the possible state of the joystick as:
    event.type == pygame.JOYBUTTONDOWN
                Or
        event.type == pygame.JOYBUTTONUP
    And hence you can compare the key pressed by the user. In joystick, all the keys have a specific number value which can be used to compare the keypress from the user.
     event.button == 1
    There are a lot of other functions and attributes available in the pygame.joystick module which can be used to add more feature to you PiArm. Read https://www.pygame.org/docs/ref/joystick.html for more details about the joystick module functionalities.

     

  • PiArm with pygame:

    You can use pygame in two ways:

    1. Playing the command from a selected group.
    2. Rotating particular motors of PiArm on Up, Down, Left, and Right key press.

    If you want a particular movement from your PiArm which you have recorded earlier you can play each different command on your key press. Example: If you want to pick an object you can select a group which picks objects for you. If you want to pick an object you press the forward key. The PiArm picks the object if you want to reverse the action you can press the backward key to play the previous command. Follow are the steps to play commands on key press from a group:

    • Read all the files from the ~/PiArm/Example, store it in a list variable.
    • Now read the contents of the file at index one.
    • Make a code which waits for a key press event.
    • Read the event type.
    • Increment the command counter by one and play the current command if the forward button is pressed.
    • Play the previous command if the back button is pressed, and
    • Change the file if Up or Down keys are pressed.

If you want to use your joystick to move in whatever direction you want you can do this by playing with the positions of servo motors of your PiArm. It is available at PiArm GitHub.

  • Initialize the Joystick module.
  • Wait for a keypress event-
    while True:
    for event in pygame.event.get():
  • Read the button type from the event.
    if event.type == pygame.JOYBUTTONDOWN:
  • Perform a specific task on button read.
    if button == 0:
    if self.servo_position[5] - self.step in range(101, 999):
    self.servo_position[5] -= self.step
    if self.servo_position[4] + self.step in range(1, 999):
    self.servo_position[4] += (self.step + 20)
    if self.servo_position[3] - self.step in range(52, 970):
    self.servo_position[3] -= (self.step + 40)
  • Add more controls to PiArm using joystick using pygame module.
Explaining joystick.py
  • Import libraries:

    Import required libraries for setup piarm, re, time, pygame.

    text 1

    About pygame:

    pygame is a module designed for video games. It contains python functions and classes that will allow you to use SDL's support for playing cdroms, audio and video output, and keyboard, mouse and joystick input.

     

  • Initializing joystick:

    o initialize a joystick a function is called “pygame.init()”.

    text 2

    pygame.init() initialize all imported pygame modules to get everything started.

  • Listen for Events:

    After initializing all the modules now listen to events like button is pressed or released.

    • *JOYBUTTONDOWN - to check whether the button is pressed or not.
    • *JOYBUTTONUP - to check whether the button is released or not.
    To get the event “pygame.event.get()” function is called. It gets events from the queue.

     

    text 3

    On receiving the events program the PiArm accordingly.

  • Change servo positions:

    According to the status of the button, program the servo positions i.e programming each button.

    In the case of serial error, make sure that the port is connected to ttyS0.

    text 4

For the whole program go to the following link-

https://github.com/sbcshop/PiArm/blob/master/joystick.py


Lesson 4 - Controlling PiArm without GUI

Lesson 2 - Controlling PiArm with GUI

Leave a comment

Please note, comments need to be approved before they are published.