Skip to content

Commit 2fa064f

Browse files
committed
upadted branch
1 parent 6735a9e commit 2fa064f

File tree

10 files changed

+165
-33
lines changed

10 files changed

+165
-33
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.venv/
2+
*.pyc
3+
__pycache__/

L1/L1_encoder.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
import smbus2
2+
import logging
3+
4+
# Configure logging for debugging
5+
logging.basicConfig(level=logging.DEBUG)
26

37
class Encoder:
48
def __init__(self, i2c_address):
59
self.bus = smbus2.SMBus(1)
610
self.address = i2c_address
711

812
def read_position(self):
9-
# Read encoder position from I2C
10-
data = self.bus.read_i2c_block_data(self.address, 0, 2)
11-
return (data[0] << 8) | data[1]
13+
"""
14+
Read the encoder position from I2C.
15+
"""
16+
try:
17+
data = self.bus.read_i2c_block_data(self.address, 0, 2)
18+
position = (data[0] << 8) | data[1]
19+
logging.debug(f"Encoder position: {position}")
20+
return position
21+
except Exception as e:
22+
logging.error(f"An error occurred while reading encoder position: {e}")
23+
raise

L1/L1_gamepad.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import pygame
2+
import logging
3+
4+
# Configure logging for debugging
5+
logging.basicConfig(level=logging.DEBUG)
26

37
class Gamepad:
48
def __init__(self):
@@ -8,7 +12,15 @@ def __init__(self):
812
self.joystick.init()
913

1014
def get_input(self):
11-
pygame.event.pump()
12-
x_axis = self.joystick.get_axis(0) # Left stick X-axis
13-
y_axis = self.joystick.get_axis(1) # Left stick Y-axis
14-
return x_axis, y_axis
15+
"""
16+
Get input from the gamepad.
17+
"""
18+
try:
19+
pygame.event.pump()
20+
x_axis = self.joystick.get_axis(0) # Left stick X-axis
21+
y_axis = self.joystick.get_axis(1) # Left stick Y-axis
22+
logging.debug(f"Gamepad input: x_axis={x_axis}, y_axis={y_axis}")
23+
return x_axis, y_axis
24+
except Exception as e:
25+
logging.error(f"An error occurred while reading gamepad input: {e}")
26+
raise

L1/L1_lidar.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
import pysicktim as lidar
2+
import logging
3+
4+
# Configure logging for debugging
5+
logging.basicConfig(level=logging.DEBUG)
26

37
class Lidar:
48
def __init__(self):
59
self.lidar = lidar.TiM561()
610

711
def scan(self):
8-
# Get LIDAR scan data
9-
return self.lidar.scan()
12+
"""
13+
Get LIDAR scan data.
14+
"""
15+
try:
16+
scan_data = self.lidar.scan()
17+
logging.debug(f"LIDAR scan data: {scan_data}")
18+
return scan_data
19+
except Exception as e:
20+
logging.error(f"An error occurred while scanning with LIDAR: {e}")
21+
raise

L1/L1_motor.py

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,57 @@
11
import gpiozero
2+
import logging
3+
from gpiozero.pins.pigpio import PiGPIOFactory
4+
from gpiozero import Device
5+
6+
# Configure logging for debugging
7+
logging.basicConfig(level=logging.DEBUG)
8+
9+
# Set the pin factory to pigpio (optional, for sharing GPIO pins between processes)
10+
Device.pin_factory = PiGPIOFactory()
211

312
class MotorController:
413
def __init__(self, pins):
5-
self.motor_a = gpiozero.PWMOutputDevice(pins[0])
6-
self.motor_b = gpiozero.PWMOutputDevice(pins[1])
7-
self.motor_c = gpiozero.PWMOutputDevice(pins[2])
8-
self.motor_d = gpiozero.PWMOutputDevice(pins[3])
14+
"""
15+
Initialize the MotorController with a list of GPIO pins.
16+
Each pin should be unique to avoid conflicts.
17+
"""
18+
# Ensure that no pin conflicts exist and that each pin is used once
19+
if len(set(pins)) != len(pins):
20+
raise ValueError("Pins must be unique. Duplicate pins found.")
21+
22+
logging.debug(f"Initializing MotorController with pins: {pins}")
23+
24+
try:
25+
# Initialize PWMOutputDevice for each motor
26+
self.motor_a = gpiozero.PWMOutputDevice(pins[0])
27+
self.motor_b = gpiozero.PWMOutputDevice(pins[1])
28+
self.motor_c = gpiozero.PWMOutputDevice(pins[2])
29+
self.motor_d = gpiozero.PWMOutputDevice(pins[3])
30+
31+
logging.debug("MotorController initialized successfully.")
32+
except gpiozero.GPIOPinInUse as e:
33+
logging.error(f"GPIO pin conflict detected: {e}")
34+
raise
35+
except Exception as e:
36+
logging.error(f"An error occurred while initializing motors: {e}")
37+
raise
938

1039
def set_speed(self, duty_cycle):
11-
self.motor_a.value = duty_cycle[0]
12-
self.motor_b.value = duty_cycle[1]
13-
self.motor_c.value = duty_cycle[2]
14-
self.motor_d.value = duty_cycle[3]
40+
"""
41+
Set the speed of all motors using a list of duty cycles.
42+
:param duty_cycle: List of duty cycles for each motor (e.g., [0.5, 0.5, 0.5, 0.5])
43+
"""
44+
if len(duty_cycle) != 4:
45+
raise ValueError("Duty cycle list must contain exactly 4 values.")
46+
47+
try:
48+
# Set the duty cycle for each motor
49+
self.motor_a.value = duty_cycle[0]
50+
self.motor_b.value = duty_cycle[1]
51+
self.motor_c.value = duty_cycle[2]
52+
self.motor_d.value = duty_cycle[3]
53+
54+
logging.debug(f"Motor speeds set to: {duty_cycle}")
55+
except Exception as e:
56+
logging.error(f"An error occurred while setting motor speeds: {e}")
57+
raise

L2/L2_kinematics.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
import numpy as np
2+
import logging
3+
4+
# Configure logging for debugging
5+
logging.basicConfig(level=logging.DEBUG)
26

37
class Kinematics:
48
def __init__(self, wheelbase):
59
self.wheelbase = wheelbase
610

711
def compute_chassis_movement(self, phiL, phiR):
8-
# Compute chassis movement based on wheel speeds
9-
x_dot = (phiL + phiR) / 2
10-
theta_dot = (phiR - phiL) / self.wheelbase
11-
return x_dot, theta_dot
12+
"""
13+
Compute chassis movement based on wheel speeds.
14+
"""
15+
try:
16+
x_dot = (phiL + phiR) / 2
17+
theta_dot = (phiR - phiL) / self.wheelbase
18+
logging.debug(f"Chassis movement: x_dot={x_dot}, theta_dot={theta_dot}")
19+
return x_dot, theta_dot
20+
except Exception as e:
21+
logging.error(f"An error occurred while computing chassis movement: {e}")
22+
raise

L2/L2_speed_control.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
1+
import logging
2+
3+
# Configure logging for debugging
4+
logging.basicConfig(level=logging.DEBUG)
5+
16
class SpeedControl:
27
def __init__(self):
38
self.target_speed = 0.0
49

510
def set_target_speed(self, speed):
11+
"""
12+
Set the target speed for the robot.
13+
"""
614
self.target_speed = speed
15+
logging.debug(f"Target speed set to: {speed}")
716

817
def compute_duty_cycle(self, current_speed):
9-
# Simple proportional control
10-
error = self.target_speed - current_speed
11-
duty_cycle = error * 0.5 # Proportional gain
12-
return duty_cycle
18+
"""
19+
Compute the duty cycle based on the current speed.
20+
"""
21+
try:
22+
error = self.target_speed - current_speed
23+
duty_cycle = error * 0.5 # Proportional gain
24+
logging.debug(f"Duty cycle computed: {duty_cycle}")
25+
return duty_cycle
26+
except Exception as e:
27+
logging.error(f"An error occurred while computing duty cycle: {e}")
28+
raise

L3/L3_drive_mt.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
11
import threading
22
from L1.L1_motor import MotorController
33
from L2.L2_kinematics import Kinematics
4+
import logging
5+
6+
# Configure logging for debugging
7+
logging.basicConfig(level=logging.DEBUG)
48

59
class DriveController:
610
def __init__(self):
7-
self.motor_controller = MotorController([11, 12, 15, 16])
11+
self.motor_controller = MotorController([17, 18, 22, 23])
812
self.kinematics = Kinematics(0.405) # Wheelbase in meters
913

1014
def drive(self, x_dot, theta_dot):
11-
# Compute wheel speeds and set motor duty cycles
12-
phiL = x_dot - theta_dot * self.kinematics.wheelbase / 2
13-
phiR = x_dot + theta_dot * self.kinematics.wheelbase / 2
14-
self.motor_controller.set_speed([phiL, phiR, phiL, phiR])
15+
"""
16+
Drive the robot based on desired x_dot and theta_dot.
17+
"""
18+
try:
19+
phiL = x_dot - theta_dot * self.kinematics.wheelbase / 2
20+
phiR = x_dot + theta_dot * self.kinematics.wheelbase / 2
21+
self.motor_controller.set_speed([phiL, phiR, phiL, phiR])
22+
logging.debug(f"Driving with x_dot={x_dot}, theta_dot={theta_dot}")
23+
except Exception as e:
24+
logging.error(f"An error occurred while driving: {e}")
25+
raise
1526

1627
# Multithreading example
1728
drive_controller = DriveController()

main.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
from L3.L3_mission_control import MissionControl
2+
import logging
3+
4+
# Configure logging for debugging
5+
logging.basicConfig(level=logging.DEBUG)
26

37
def main():
4-
mission_control = MissionControl()
5-
mission_control.start_mission()
8+
try:
9+
mission_control = MissionControl()
10+
mission_control.start_mission()
11+
except Exception as e:
12+
logging.error(f"An error occurred: {e}")
613

714
if __name__ == "__main__":
815
main()

utils/logger.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@ def __init__(self, filename="log.csv"):
88
def log_data(self, data):
99
with open(self.filename, "a") as file:
1010
writer = csv.writer(file)
11-
writer.writerow([datetime.now()] + data)
11+
writer.writerow([datetime.now()] + data)
12+
13+
import logging
14+
15+
def setup_logger():
16+
logging.basicConfig(level=logging.DEBUG)

0 commit comments

Comments
 (0)