I am currently building a submarine using 4 T100 motors and an xbox controller. After looking at several solutions online, this is the code I came up with. The movement of the motors is very erratic, with the motors sometimes not working at all. Here is a link to the Xbox library I’m trying to use. https://tutorials-raspberrypi.com/raspberry-pi-xbox-360-controller-wireless/
import RPi.GPIO as GPIO
import math
import xbox
import time
motor = 7
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(motor, GPIO.OUT)
def angleFromCoords(x,y):
angle = 0.0
if x==0.0 and y==0.0:
angle = 90.0
elif x>=0.0 and y>=0.0:
# first quadrant
angle = math.degrees(math.atan(y/x)) if x!=0.0 else 90.0
elif x<0.0 and y>=0.0:
# second quadrant
angle = math.degrees(math.atan(y/x))
angle += 180.0
elif x<0.0 and y<0.0:
# third quadrant
angle = math.degrees(math.atan(y/x))
angle += 180.0
elif x>=0.0 and y<0.0:
# Fourth quadrant
angle = math.degrees(math.atan(y/x)) if x!=0.0 else -90.0
angle += 360.0
return angle
if __name__ == '__main__':
joy = xbox.Joystick()
pwm = GPIO.PWM(motor, 750)
pwm.start(95)
while not joy.Back():
if joy.A():
signal = 0
else:
signal = 360
GPIO.output(motor, signal)
joy.close()
pwm.stop()