From 287aa22d0c9ce906727f158ee5a8ece853ecdeba Mon Sep 17 00:00:00 2001 From: tmei005 Date: Tue, 23 Apr 2024 19:17:25 -0400 Subject: [PATCH] wheels --- .../__pycache__/wheels.cpython-311.pyc | Bin 3580 -> 3506 bytes .../movement_2motors/movement_2motors.ino | 27 ++++++++++++------ electrical_code/wheels.py | 9 ++++-- electrical_code/wheels_test.py | 4 ++- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/electrical_code/__pycache__/wheels.cpython-311.pyc b/electrical_code/__pycache__/wheels.cpython-311.pyc index eaa8affaeabb871e821ecc5b1d263fde00eb8689..cb9d0dc5ec0e4ec55e951ecd69783e4df0024b7f 100644 GIT binary patch delta 479 zcmew(y-8YqIWI340}ybVYowiFWng#=;=lk4l=0bMqPjj?3S%&XCd zt5L!TQ^>%O!cxOj1>`ORimir=GcwdLEMT78$Rs>jp3{brb#e@+qyn4YE!K?GoSghy ztOh_}SR@71SH+o@UsRr0lwzeYxr;N0v3T-#P95R99K0RbGu$uoDPQDJxx%4xfkS1o zG`rMfTP_R6JCpO+eYI+tN`S70ffQzfu87hES!w{Wlq)GQIU8bYkp|EZP4*&bAXOv- zB1|UF=M1iL6_M)*ZOm5UszS2$EJaHv9Eq%`>+_gP0)R-+FL za6*6u#Kk0v1VI*nf>)Ed2o(NBqA-EUzPwUgc0e8|5Q{x07xK!OYBB;%!6ZI{#J*sW H02>bgL9}PF delta 555 zcmZWly-UMD9KCBUHR*>~N`|I>APTi2s6`yaZsMd4ejtb#qd9FbP2`#ibx}~kQMtj@ zNd*^ma`P_`Y7s42-JBF19lT484nFSp?tbsyJMQi#{guv~$Kw&e(ALtiRuKVs;Y|S6 z`}o*D&h#%}!9W`!E|BgJz#&6Cg8*EzYG46jbQ(kg(VuY`H(}{aU~X0oKw!Wbzp~-$ z#n&e3iRmZ6h4e~LX+fHxb!mq7M%xnwY*~&c4loQ7mANREEx1f;avx1Zrr|AJ5+~`k zI0psVC#MuN)i4kEX41nFBufvXLt(xw&C$DPCku|!{@DEv5_50hmzo9M{wI>)hag6t zq-jOnv^C9BDo&Ybn+|a`yIfqsL@Qu|vohfyN|Yc$MtCe%tc;ta+l%VwcsEzd})A?3(CO<&j#DF2A^)nL4d0I4kHKm8jk CL5MH_ diff --git a/electrical_code/drive/movement_2motors/movement_2motors.ino b/electrical_code/drive/movement_2motors/movement_2motors.ino index 87fd693..17b28b1 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