diff --git a/electrical_code/__pycache__/wheels.cpython-311.pyc b/electrical_code/__pycache__/wheels.cpython-311.pyc index eaa8aff..cb9d0dc 100644 Binary files a/electrical_code/__pycache__/wheels.cpython-311.pyc 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 0e9ef60..c7c6344 100644 --- a/electrical_code/drive/movement_2motors/movement_2motors.ino +++ b/electrical_code/drive/movement_2motors/movement_2motors.ino @@ -11,6 +11,7 @@ int angle; int FBdirection; int dist; String inputString; +char inputArr[8]; @@ -79,6 +80,8 @@ void turnRight(int delay_time) { void setup() { Serial.begin(115200); // Initialize all motor pins as outputs + pinMode(2, OUTPUT); + pinMode(leftMotor1, OUTPUT); pinMode(leftMotor2, OUTPUT); pinMode(rightMotor1, OUTPUT); @@ -88,12 +91,18 @@ void setup() { } void loop() { + Serial.print("Hello"); while (!Serial.available()); + digitalWrite(2, HIGH); + delay(2000); + digitalWrite(2, LOW); + // Receiving Data from Computer if (Serial.available() > 0) { - inputString = Serial.readStringUntil('\n'); + Serial.readBytes(inputArr, 8); } + inputString = String(inputArr); // 1 character - direction (1 as left and 0 as right) // 3 characters - angle // 1 character - direction (1 as forward and 0 as backward) @@ -101,23 +110,25 @@ void loop() { LRdirection = inputString.substring(0, 1).toInt(); angle = inputString.substring(1, 4).toInt(); 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(); + dist = inputString.substring(5, 8).toInt(); + + Serial.print(dist); if(LRdirection == 1){ + Serial.print("left"); turnLeft(angle * 10); } else{ + Serial.print("right"); turnRight(angle * 10); } if(FBdirection == 1){ - goForward(dist * 10); + Serial.print("forward"); + goForward(dist * 10); } else { - goBackward(dist * 10); + Serial.print("backward"); + goBackward(dist * 10); } } diff --git a/electrical_code/wheels.py b/electrical_code/wheels.py index 74692cb..da4d88b 100644 --- a/electrical_code/wheels.py +++ b/electrical_code/wheels.py @@ -6,6 +6,7 @@ class Wheels: def __init__(self, port_num): self.arduino = serial.Serial(port=port_num, baudrate=115200, timeout=0.1) + self.arduino.flush() def write_serial(self, x): self.arduino.write(bytes(x, 'utf-8')) @@ -33,16 +34,18 @@ def turnLeft(self, angle): def goForward(self, dist): print('hello') command = "00001" + str(dist).zfill(3) + print("forward: " + command) self.write_serial(command) time.sleep(dist*(self.time_divisor)/1000) def goBackward(self, dist): command = "00000" + str(dist).zfill(3) + print("backward: " + command) self.write_serial(command) time.sleep(dist*(self.time_divisor)/1000) -if __name__ == "__main__": - myFirstWheel = Wheels("your_port_number_here") - myFirstWheel.move(direction, angle_value, distance_value) +# if __name__ == "__main__": +# myFirstWheel = Wheels("your_port_number_here") +# myFirstWheel.move(direction, angle_value, distance_value) diff --git a/electrical_code/wheels_test.py b/electrical_code/wheels_test.py index 60d92fe..46c212c 100644 --- a/electrical_code/wheels_test.py +++ b/electrical_code/wheels_test.py @@ -3,9 +3,11 @@ # Instantiate the Wheels class with your port number myFirstWheel = Wheels("COM5") + # Test movement commands +# myFirstWheel.move(1,000,110) myFirstWheel.goForward(100) # Go forward 100 units -# myFirstWheel.goBackward("50") # Go backward 50 units +myFirstWheel.goBackward(111) # Go backward 50 units # myFirstWheel.turnRight("90") # Turn right by 90 degrees # myFirstWheel.turnLeft("45") # Turn left by 45 degrees