some crap with which I’ve been fooling around…

joystickclient.py:

import os
import struct
import array
import socket
import sys

from fcntl import ioctl

try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
    print("Failed to connect")
    sys.exit();

print("Socket Created")

remote_ip = [REDACTED]
port = [REDACTED]

print("IP Address: " + remote_ip)

s.connect((remote_ip, port))

print("Socket Connected to " + remote_ip)

#s.close()

# We'll store the states here.
axis_states = {}
button_states = {}

axis_names = {
    0x00 : 'x',
    0x01 : 'y',
    0x05 : 'z',
    0x06 : 'd',
    0x10 : 'hx',
    0x11 : 'hy',
}

button_names = {
    0x120 : 'tr',
    0x121 : 'th',
    0x122 : 't1',
    0x123 : 't2',
    0x124 : 't3',
    0x125 : 't4',
    0x126 : 'b1',
    0x127 : 'b2',
    0x128 : 'b3',
    0x129 : 'b4',
    0x12a : 'b5',
    0x12b : 'b6',
}

axis_map = []
button_map = []

# Open the joystick device.
js = '/dev/input/js0'
jsdev = open(js, 'rb')

# Get number of axes and buttons.
buf = array.array('B', [0])
ioctl(jsdev, 0x80016a11, buf)
num_axes = buf[0]

buf = array.array('B', [0])
ioctl(jsdev, 0x80016a12, buf)
num_buttons = buf[0]

# Get the axis map.
buf = array.array('B', [0] * 0x40)
ioctl(jsdev, 0x80406a32, buf)

for axis in buf[:num_axes]:
    axis_name = axis_names.get(axis, 'unknown(0x%02x)' % axis)
    axis_map.append(axis_name)
    axis_states[axis_name] = 0.0

# Get the button map.
buf = array.array('H', [0] * 200)
ioctl(jsdev, 0x80406a34, buf)

for btn in buf[:num_buttons]:
    btn_name = button_names.get(btn, 'unknown(0x%03x)' % btn)
    button_map.append(btn_name)
    button_states[btn_name] = 0

# Main event loop
while True:
    evbuf = jsdev.read(8)
    if evbuf:
        time, value, type, number = struct.unpack('IhBB', evbuf)

        if type & 0x01:
            button = button_map[number]
            if button:
                button_states[button] = value
                if value:
                    plaintext = button + ":1|"
                    s.send(plaintext.encode())
                    #print(plaintext[:-1])
                else:
                    plaintext = button + ":0|"
                    s.send(plaintext.encode())
                    #print(plaintext[:-1])

        if type & 0x02:
            axis = axis_map[number]
            if axis:
                axis_states[axis] = value
                plaintext = "%s:%i|" % (axis, value)
                s.send(plaintext.encode())
                #print(plaintext[:-1])

stepperserver.py:


import socket
import sys
import Slush
import RPi.GPIO as GPIO

GPIO.setwarnings(False)

# SlushEngine Setup
SlushEngine = Slush.sBoard()

maxspeed = 900

# Motor for X Axis
mx = Slush.Motor(0)
mx.setMicroSteps(1)
mx.setCurrent(25, 25, 25, 35)
mx.setMaxSpeed(maxspeed)
mx.setAccel(13000)
mx.setDecel(13000)

# Motor for Y Axis
my = Slush.Motor(1)
my.setMicroSteps(1)
my.setCurrent(25, 25, 25, 35)
my.setMaxSpeed(maxspeed)
my.setAccel(13000)
my.setDecel(13000)

host = [REDACTED]
port = [REDACTED]

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

print("socket created")

try:
    s.bind((host, port))
except socket.error:
    print("Binding failed: " + socket.error);
    sys.exit();

print("socket binding successful")

s.listen(10)

print("socket is ready")

conn, addr = s.accept()

print("remote connected using " + addr[0] + ":" + str(addr[1]))

while 1:
    data = conn.recv(16384)
    data = data.decode()
    data = data[:-1]
    if "|" in data:
        #print("BUFFER LOADED --> " + data + " <-- BUFFER LOADED")
        overload = data.split("|")
        for item in overload:
            #print(item)
            if item[0] == "x":
                xdata = item.split(":")
                if int(xdata[1]) > 0:
                    mx.run(1, int(xdata[1]) / 32767 * maxspeed)
                if int(xdata[1]) < 0:
                    mx.run(0, abs(int(xdata[1])) / 32767 * maxspeed)
                if int(xdata[1]) == 0:
                    mx.softStop()
            if item[0] == "y":
                ydata = item.split(":")
                if int(ydata[1]) > 0:
                    my.run(1, int(ydata[1]) / 32767 * maxspeed)
                if int(ydata[1]) < 0:
                    my.run(0, abs(int(ydata[1])) / 32767 * maxspeed)
                if int(ydata[1]) == 0:
                    my.softStop()
    else:
        #print(data)
        if data[0] == "x":
            xdata = data.split(":")
            if int(xdata[1]) > 0:
                mx.run(1, int(xdata[1]) / 32767 * maxspeed)
            if int(xdata[1]) < 0:
                mx.run(0, abs(int(xdata[1])) / 32767 * maxspeed)
            if int(xdata[1]) == 0:
                mx.softStop()
        if data[0] == "y":
            ydata = data.split(":")
            if int(ydata[1]) > 0:
                my.run(1, int(ydata[1]) / 32767 * maxspeed)
            if int(ydata[1]) < 0:
                my.run(0, abs(int(ydata[1])) / 32767 * maxspeed)
            if int(ydata[1]) == 0:
                my.softStop()

    if not data:
        break;

conn.close()
s.close()

hardware:

https://www.amazon.com/Extreme-3D-Pro-Joystick-Windows/dp/B00009OY9U?SubscriptionId=AKIAILSHYYTFIVPWUY6Q&tag=duckduckgo-d-20&linkCode=xm2&camp=2025&creative=165953&creativeASIN=B00009OY9U

https://www.amazon.com/Stepper-Bipolar-Printer-Router-Kelife/dp/B07GNX76VD/ref=sr_1_3?keywords=4pcs+CNC+Stepper+Motor+Nema+17&qid=1556578656&s=gateway&sr=8-3
https://www.amazon.com/Stepping-Motor-Stepper-Bipolar-Printer/dp/B077HSYXZD/ref=sr_1_5?keywords=Stepper+Motor+Nema+17%2C+5pcs+Bipolar&qid=1556578741&s=gateway&sr=8-5

https://roboteurs.com/collections/drivers/products/slushengine-raspberry-pi-hat-stepper-motor-driver

https://www.amazon.com/MENZO-Universal-Regulated-Switching-Computer/dp/B06VWV5YCH/ref=sr_1_fkmrnull_2?keywords=menzo+12v+30a+dc+universal+regulated&qid=1556578814&s=gateway&sr=8-2-fkmrnull

bits and pieces shamelessly plagiarized from:

https://gist.github.com/rdb/8864666

https://github.com/Roboteurs/slushengine/tree/master/examples
https://roboteurs.com/pages/slushengine-advanced-current-driving-of-stepper-motors
https://roboteurs.com/pages/slushengine-moving-a-motor-at-diffrent-speeds
https://roboteurs.com/pages/slushengine-moving-multiple-stepper-motos-at-once-with-raspberry-pi
https://roboteurs.com/pages/slushengine-advance-motor-configuration

Leave a Reply

Your email address will not be published. Required fields are marked *