Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
hwii-florescent committed Apr 17, 2024
2 parents 969901f + b91fae4 commit 824087e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
Binary file not shown.
19 changes: 13 additions & 6 deletions electrical_code/drive/movement_2motors/movement_2motors.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}


19 changes: 15 additions & 4 deletions electrical_code/wheels.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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")
Expand Down
13 changes: 10 additions & 3 deletions electrical_code/wheels_test.py
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit 824087e

Please sign in to comment.