diff --git a/electrical_code/__pycache__/wheels.cpython-311.pyc b/electrical_code/__pycache__/wheels.cpython-311.pyc new file mode 100644 index 0000000..eaa8aff Binary files /dev/null and b/electrical_code/__pycache__/wheels.cpython-311.pyc differ diff --git a/electrical_code/drive/movement_2motors/movement_2motors.ino b/electrical_code/drive/movement_2motors/movement_2motors.ino index 0a4efb1..0e9ef60 100644 --- a/electrical_code/drive/movement_2motors/movement_2motors.ino +++ b/electrical_code/drive/movement_2motors/movement_2motors.ino @@ -6,8 +6,9 @@ int leftMotor2 = 9; int rightMotor1 = 6; int rightMotor2 = 5; -int direction; +int LRdirection; int angle; +int FBdirection; int dist; String inputString; @@ -95,23 +96,29 @@ void loop() { } // 1 character - direction (1 as left and 0 as right) // 3 characters - angle + // 1 character - direction (1 as forward and 0 as backward) // 3 characters - distance - direction = inputString.substring(0, 1).toInt(); + LRdirection = inputString.substring(0, 1).toInt(); angle = inputString.substring(1, 4).toInt(); - String dist_str = inputString.substring(4, 8); + FBdirection = inputString.substring(4, 5).toInt(); + String dist_str = inputString.substring(5, 8); while (dist_str.length() < 3) { dist_str = "0" + dist_str; } dist = dist_str.toInt(); - if(direction == 1){ + if(LRdirection == 1){ turnLeft(angle * 10); } else{ turnRight(angle * 10); } - goForward(dist * 10); - + if(FBdirection == 1){ + goForward(dist * 10); + } + else { + goBackward(dist * 10); + } } diff --git a/electrical_code/wheels.py b/electrical_code/wheels.py index b807e58..74692cb 100644 --- a/electrical_code/wheels.py +++ b/electrical_code/wheels.py @@ -2,6 +2,8 @@ import time class Wheels: + time_divisor = 10 + def __init__(self, port_num): self.arduino = serial.Serial(port=port_num, baudrate=115200, timeout=0.1) @@ -17,20 +19,29 @@ def move(self, dir, angle, dist): self.write_serial(command) def turnRight(self, angle): - command = "0" + str(angle) + "000" + command = "0" + str(angle).zfill(3) + "0000" self.write_serial(command) + time.sleep(angle*(self.time_divisor)/1000) + def turnLeft(self, angle): - command = "1" + str(angle) + "000" + command = "1" + str(angle).zfill(3) + "0000" self.write_serial(command) + time.sleep(angle*(self.time_divisor)/1000) + def goForward(self, dist): - command = "0000" + str(dist) # direction doesn't matter + print('hello') + command = "00001" + str(dist).zfill(3) self.write_serial(command) + time.sleep(dist*(self.time_divisor)/1000) + def goBackward(self, dist): - command = "0000" + str(dist) # direction doesn't matter + command = "00000" + str(dist).zfill(3) self.write_serial(command) + time.sleep(dist*(self.time_divisor)/1000) + if __name__ == "__main__": myFirstWheel = Wheels("your_port_number_here") diff --git a/electrical_code/wheels_test.py b/electrical_code/wheels_test.py index 00dccb0..60d92fe 100644 --- a/electrical_code/wheels_test.py +++ b/electrical_code/wheels_test.py @@ -1,4 +1,11 @@ -import "./wheels.py" +from wheels import Wheels + +# Instantiate the Wheels class with your port number +myFirstWheel = Wheels("COM5") + +# Test movement commands +myFirstWheel.goForward(100) # Go forward 100 units +# myFirstWheel.goBackward("50") # Go backward 50 units +# myFirstWheel.turnRight("90") # Turn right by 90 degrees +# myFirstWheel.turnLeft("45") # Turn left by 45 degrees -w = Wheels("COM4") -w.move("100100") \ No newline at end of file