mirror of
https://github.com/Wessel/Roommapper.git
synced 2026-06-08 14:07:59 +02:00
Last changes to pytthton drivers for all sensors
This commit is contained in:
@@ -2,48 +2,9 @@ import math
|
|||||||
import py_qmc5883l
|
import py_qmc5883l
|
||||||
import time
|
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)
|
sensor = py_qmc5883l.QMC5883L(output_range=py_qmc5883l.RNG_8G)
|
||||||
|
|
||||||
while True:
|
|
||||||
|
|
||||||
m = sensor.get_bearing()
|
m = sensor.get_bearing()
|
||||||
print(m)
|
print(m)
|
||||||
time.sleep(0.1)
|
|
||||||
@@ -12,14 +12,14 @@ direction1 = 23
|
|||||||
step1 = 22
|
step1 = 22
|
||||||
enable1 = 24
|
enable1 = 24
|
||||||
|
|
||||||
direction2 = 14
|
direction2 = 20
|
||||||
step2 = 15
|
step2 = 26
|
||||||
enable2 = 18
|
enable2 = 21
|
||||||
|
|
||||||
GPIO.setup(enable1, GPIO.OUT, initial=GPIO.HIGH)
|
GPIO.setup(enable1, GPIO.OUT, initial=GPIO.HIGH)
|
||||||
GPIO.setup(enable2, GPIO.OUT, initial=GPIO.HIGH)
|
GPIO.setup(enable2, GPIO.OUT, initial=GPIO.HIGH)
|
||||||
GPIO.output(enable1, GPIO.HIGH)
|
# GPIO.output(enable1, GPIO.HIGH)
|
||||||
GPIO.output(enable2, GPIO.HIGH)
|
# GPIO.output(enable2, GPIO.HIGH)
|
||||||
|
|
||||||
def run_motor1(direction, steps):
|
def run_motor1(direction, steps):
|
||||||
direction = not direction
|
direction = not direction
|
||||||
@@ -61,10 +61,11 @@ def drive(direction, steps):
|
|||||||
thread1.join()
|
thread1.join()
|
||||||
thread2.join()
|
thread2.join()
|
||||||
|
|
||||||
|
# sleep to stop inertia
|
||||||
|
time.sleep(0.5)
|
||||||
|
|
||||||
GPIO.output(enable1, GPIO.HIGH)
|
GPIO.output(enable1, GPIO.HIGH)
|
||||||
GPIO.output(enable2, GPIO.HIGH)
|
GPIO.output(enable2, GPIO.HIGH)
|
||||||
|
|
||||||
GPIO.cleanup()
|
|
||||||
print("Driven succesfully")
|
print("Driven succesfully")
|
||||||
|
|
||||||
def turn(direction, steps):
|
def turn(direction, steps):
|
||||||
@@ -102,10 +103,14 @@ def turn(direction, steps):
|
|||||||
thread1.join()
|
thread1.join()
|
||||||
thread2.join()
|
thread2.join()
|
||||||
|
|
||||||
|
# sleep to stop inertia
|
||||||
|
time.sleep(0.5)
|
||||||
|
|
||||||
GPIO.output(enable1, GPIO.HIGH)
|
GPIO.output(enable1, GPIO.HIGH)
|
||||||
GPIO.output(enable2, GPIO.HIGH)
|
GPIO.output(enable2, GPIO.HIGH)
|
||||||
|
print("Turned succesfully")
|
||||||
|
|
||||||
GPIO.cleanup()
|
# GPIO.cleanup()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
33
src/Client/NewRobotPy/ultrasonic.py
Normal file
33
src/Client/NewRobotPy/ultrasonic.py
Normal 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)
|
||||||
Reference in New Issue
Block a user