Last changes to pytthton drivers for all sensors

This commit is contained in:
Wouter
2024-07-03 17:42:56 +02:00
parent 5b97af3797
commit 35e5665dd9
3 changed files with 49 additions and 50 deletions

View File

@@ -2,48 +2,9 @@ import math
import py_qmc5883l
import time
# def calculate_angle(sensor):
# """
# Calculate the angle from 0 to 360 degrees using the QMC5883L magnetic sensor.
# Args:
# sensor (QMC5883L): An instance of the QMC5883L sensor.
# Returns:
# float: The calculated angle in degrees from 0 to 360.
# """
# # Get the raw magnetic sensor data
# x, y, z = sensor.get_magnet_raw()
# if x is None or y is None:
# return None
# # Calculate the angle in radians
# angle_rad = math.atan2(y, x)
# # Convert the angle to degrees
# # angle_deg = math.degrees(angle_rad)
# # Adjust the angle to the range of 0 to 360 degrees
# # if angle_deg < 0:
# # angle_deg += 360
# return angle_rad
# # Example usage
# if __name__ == "__main__":
# sensor = py_qmc5883l.QMC5883L()
# while True:
# angle = calculate_angle(sensor)
# print("Angle:", angle)
# time.sleep(0.1)
sensor = py_qmc5883l.QMC5883L(output_range=py_qmc5883l.RNG_8G)
while True:
m = sensor.get_bearing()
print(m)
time.sleep(0.1)
m = sensor.get_bearing()
print(m)

View File

@@ -12,14 +12,14 @@ direction1 = 23
step1 = 22
enable1 = 24
direction2 = 14
step2 = 15
enable2 = 18
direction2 = 20
step2 = 26
enable2 = 21
GPIO.setup(enable1, GPIO.OUT, initial=GPIO.HIGH)
GPIO.setup(enable2, GPIO.OUT, initial=GPIO.HIGH)
GPIO.output(enable1, GPIO.HIGH)
GPIO.output(enable2, GPIO.HIGH)
# GPIO.output(enable1, GPIO.HIGH)
# GPIO.output(enable2, GPIO.HIGH)
def run_motor1(direction, steps):
direction = not direction
@@ -60,11 +60,12 @@ def drive(direction, steps):
# Wait for both threads to complete
thread1.join()
thread2.join()
# sleep to stop inertia
time.sleep(0.5)
GPIO.output(enable1, GPIO.HIGH)
GPIO.output(enable2, GPIO.HIGH)
GPIO.cleanup()
print("Driven succesfully")
def turn(direction, steps):
@@ -101,11 +102,15 @@ def turn(direction, steps):
# Wait for both threads to complete
thread1.join()
thread2.join()
# sleep to stop inertia
time.sleep(0.5)
GPIO.output(enable1, GPIO.HIGH)
GPIO.output(enable2, GPIO.HIGH)
print("Turned succesfully")
GPIO.cleanup()
# GPIO.cleanup()
if __name__ == "__main__":

View File

@@ -0,0 +1,33 @@
import serial
import time
def read_distance(serial_port):
# Open serial port
ser = serial.Serial(serial_port, 9600, timeout=1)
time.sleep(2) # Wait for the serial connection to initialize
# Command to read distance
read_distance_cmd = [0x22, 0x00, 0x00, 0x22]
# Send the command
ser.write(bytearray(read_distance_cmd))
# Read the response
response = ser.read(4)
# Close serial port
ser.close()
if len(response) == 4:
distance = (response[1] << 8) + response[2]
return distance
else:
return None
if __name__ == "__main__":
port = '/dev/serial0' # Replace with your actual serial port
distance = read_distance(port)
print(distance)