I am trying to take an IMU's data via serial port to press arrow keys to be able to play a game with the use of an accelerometer, gyroscope, and magnetometer.
I am able to print the serial data with roll, pitch, and yaw direction but i want the data to go into if statements to press up, down, right, and left arrow keys if the values of the data are between ranges such as from 0 to 25 pitch would be up and 0 to -25 pitch would be down.
Could I get some help on how to send the serial data into if statements?
Python Code:
I am able to print the serial data with roll, pitch, and yaw direction but i want the data to go into if statements to press up, down, right, and left arrow keys if the values of the data are between ranges such as from 0 to 25 pitch would be up and 0 to -25 pitch would be down.
Could I get some help on how to send the serial data into if statements?
Python Code:
from pynput.keyboard import Key, Controller
from time import *
import numpy as np
import math
import serial
ad=serial.Serial('com11',115200)
sleep(1)
keyboard = Controller()
toRad=2*np.pi/360
toDeg=1/toRad
while (True):
while (ad.inWaiting()==0):
pass
dataPacket=ad.readline()
dataPacket=str(dataPacket,'utf-8')
splitPacket=dataPacket.split(",")
roll=float(splitPacket[0])*toRad
pitch=float(splitPacket[1])*toRad
yaw=float(splitPacket[2])*toRad+np.pi
if pitch <= -5:
print('Down')
break
if pitch >= 5:
print('Up')
break
print("Pitch=",pitch*toDeg," Roll=",roll*toDeg,"Yaw=",yaw*toDeg)
