voorloopnul

About Me

My name is Ricardo Pascal, I'm a developer and sysadmin who lives in Florianopolis/Brazil. I enjoy work with projects related to Linux, Python, Science, Web, Django and Network.

g+ Vaga de emprego para programador python na lett.com.br O serviço que eles oferecem parece interessante... | "The Horribly Slow Murderer with the Extremely Inefficient Weapon", it's number one in my top 10 worst... | Challenge accepted! One per night til the end of year ( minus weekends )  | TIL: genotoxicity  is not a made up word | +Detectify  i have to say, well done Sirs ! " tl;dr: We uploaded a malicious XML to one of Google’s... | A drug used to treat Cytomegalovirus infections can increase almost four times the survive rate of people... | Physics FTW! | Just to clarify!  | It seems that Brazil actually "chose" the best. | Good tips for dev teams. | it's like our entire body is a baby's soft spot! | Next time I will try with a lighter battery ( < 330g )  | Testing my F330 with multiwii crius | Anyone here who owns a LG 29" 29EA73 know if the full resolution(2560x1080) can be achieved in Ubuntu... | Particularly, I think(as audience) that less slides and more whiteboard is better than no slides. | It would be awesome have something like that in multiwii. #quadrotor | LMFAO!  This is sooooo true! #ComputerNerdProblems   | Good news everyone! Galaxy note 10.1 (n8000) will be updated to android 4.4 http://www.samsungmobi... | In Brazil, cops don't put people in jail. They prefer to beat you in hope to scare you away. | Have you ever wondered how much time you could save by using a 24 cores server to do a #blast that is... |

Controlling a motor using wifi connection and a gamepad

Hobbyking sells a neat wifi receiver to control planes and cars using android or IOS devices. So I bought one and used my computer and a gamepad to control a motor.

Protocol

A description of the receiver protocol can be found at rcgroups forum

"If someone is interested: The HK Wifi Unit can be used with any WIFI enabled device. Though I do not have a unit, I just checked out the code. The HK Wifi unit waits on IP 10.10.100.254 on port 8899. The socket timeout should be set to 1000ms. One message has a length of 11 bytes. The first 4 bytes are always 85-0-11-0. After that, the values of the 4 channels are transmitted in correct order (channels 1-4). The next 2 bytes (9,10) are not used at all (maybe space for another 2 channels in the future). The last byte is reserved as control byte. All 10 data-bytes are added and transmitted as last byte. I hope I could help you. I do not have the time to develope an android app at this very moment, but it should be really easy. Hope to see some in the near future."

Considerations

  • You can find some info about sockets in my previous post: how not to create a network game
  • If you buy the receiver from this link I will earn some cents to use on my next shop.
  • The Gamepad code requires the Pygame library

    sudo apt-get install python-pygame

Code explanation

  • It will run a loop and check for gamepad input. If you press the square buttom the connection to receiver will be stablished.
  • If you press the circle buttom, the power of all channels will down to 0, the connection will be closed and the loop will end.
  • If you move the left analogic stick in the up direction, the motor will gain a power increment.

The output from gamepad is a float between 0 and 1. I have defined the base power as 50, so for each .1 incremment in gamepad input, the motor increase it power in 5 units going to a maximum of 100.

The code

#!/usr/bin/python
# Part of this code was based on http://dl.dropbox.com/u/815267/wifiHK/wifiHK.py
import socket
import time

class HKProtocol(object):
    def __init__(self):
        self.headerData = [85, 0, 11, 0]
        self.header = ''.join([chr(x) for x in self.headerData])
        self.posData = [0, 0, 0, 0, 0, 0]

    def start(self):
        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.s.connect(('10.10.100.254', 8899))

    def test_channels(self):
        for i in range(50):
            self.posData = [60, 60, 60, 60, 0, 0]
            self.send()
            time.sleep(0.01)

    def send(self):
        pos = ''.join([chr(x) for x in self.posData])
        checksum = (sum(self.posData) + sum(self.headerData)) % 256
        data = self.header + pos + chr(checksum)
        self.s.send(data)

    def stop(self):
        self.posData = [0, 0, 0, 0, 0, 0]
        self.send()
        self.s.close()
        del(self.s)

if __name__ == "__main__":
    import pygame
    pygame.init()
    j = pygame.joystick.Joystick(0)
    j.init()
    hk = HKProtocol()
    running = False
    while True:
        pygame.event.pump()
        ch1 = int(50 - (j.get_axis(1) * 50))
        ch2, ch3, ch4 = 0, 0, 0

        # Start engine (square button)
        if j.get_button(3):
            if not running:
                running = True
                hk.start()

        # Stop engine (circle button)
        if j.get_button(1):
            if running:
                hk.stop()
            break

        if running:
            hk.posData = [ch1, ch2, ch3, ch4, 0, 0]
            hk.send()
        time.sleep(0.01)

by Ricardo Pascal on Dec 16, 2012