Python Forum
Taking serial data to perform key.press functions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Taking serial data to perform key.press functions
#1
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:
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)
Reply
#2
Please put your code inside python tags.

Not sure what is happening without any output shown, but I find it odd that you're immediately converting the read data to radians, then you convert back to degrees to print. Any reason why the double conversion?

Also, you are printing the value in degrees, but you are comparing the value in radians. If you have a 10 degree pitch, it will print bigger than 5, but it is much less than 5 radians so won't trigger.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 3,301 Aug-24-2023, 07:56 AM
Last Post: gowb0w
  Help reading data from serial RS485 korenron 8 13,591 Nov-14-2021, 06:49 AM
Last Post: korenron
  get data from 2 async functions korenron 0 1,201 Sep-22-2021, 08:39 AM
Last Post: korenron
  Reading Serial data Moris526 6 5,280 Dec-26-2020, 04:04 PM
Last Post: Moris526
  Put Serial data to list PA3040 5 6,383 Sep-01-2020, 02:34 PM
Last Post: deanhystad
  Reading serial data and saving to a file Mohan 1 7,436 May-25-2020, 04:18 PM
Last Post: pyzyx3qwerty
  Read Data from Serial Port PA3040 3 3,127 Feb-16-2020, 04:54 AM
Last Post: PA3040
  Reading data from serial port as byte array vlad93 1 11,978 May-18-2019, 05:26 AM
Last Post: heiner55
  How can I only receive data from selected serial numbers MasterCATZ 7 3,465 Apr-20-2019, 08:35 AM
Last Post: MasterCATZ
  Receive Serial Data and store in different Variables in Python jenkins43 5 5,481 Dec-28-2018, 01:33 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020