Skip to content

Commit

Permalink
wheel class methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tmei005 committed Apr 12, 2024
1 parent 1ee9c6e commit 54adf61
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
20 changes: 16 additions & 4 deletions electrical_code/drive/movement_2motors/movement_2motors.ino
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ int leftMotor2 = 7;
int rightMotor1 = 6;
int rightMotor2 = 5;

int direction;
int angle;
int dist;
String inputString;
Expand Down Expand Up @@ -92,13 +93,24 @@ void loop() {
if (Serial.available() > 0) {
inputString = Serial.readStringUntil('\n');
}
// 1 character - direction (1 as left and 0 as right)
// 3 characters - angle
// 3 characters - distance
angle = inputString.substring(0, 3).toInt();
dist = inputString.substring(3, 7).toInt();
direction = inputString.substring(0, 1).toInt();
angle = inputString.substring(1, 4).toInt();
String dist_str = inputString.substring(4, 8);
while (dist_str.length() < 3) {
dist_str = "0" + dist_str;
}
dist = dist_str.toInt();

turnRight(angle * 100);
goForward(dist * 100);
if(direction == 1){
turnLeft(angle * 10);
}
else{
turnRight(angle * 10);
}
goForward(dist * 10);

}

Expand Down
2 changes: 1 addition & 1 deletion electrical_code/drive/movement_2motors/test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import serial.tools
import serial
import time
import sys

Expand Down
27 changes: 22 additions & 5 deletions electrical_code/wheels.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,33 @@ class Wheels:
def __init__(self, port_num):
self.arduino = serial.Serial(port=port_num, baudrate=115200, timeout=0.1)

def write_read(self, x):
def write_serial(self, x):
self.arduino.write(bytes(x, 'utf-8'))
time.sleep(0.05)

def move(self, angle, dist):
def move(self, dir, angle, dist):
direction_str = str(dir)
angle_str = str(angle).zfill(3)
dist_str = str(dist).zfill(3)
command = angle_str + dist_str
self.write_read(command)
command = dir + angle_str + dist_str
self.write_serial(command)

def turnRight(self, angle):
command = "0" + str(angle) + "000"
self.write_serial(command)

def turnLeft(self, angle):
command = "1" + str(angle) + "000"
self.write_serial(command)

def goForward(self, dist):
command = "0000" + str(dist) # direction doesn't matter
self.write_serial(command)

def goBackward(self, dist):
command = "0000" + str(dist) # direction doesn't matter
self.write_serial(command)

if __name__ == "__main__":
myFirstWheel = Wheels("your_port_number_here")
myFirstWheel.move(angle_value, distance_value)
myFirstWheel.move(direction, angle_value, distance_value)

0 comments on commit 54adf61

Please sign in to comment.