Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
hwii-florescent committed Apr 23, 2024
2 parents 086f84f + 8706476 commit bc26137
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
Binary file modified electrical_code/__pycache__/wheels.cpython-311.pyc
Binary file not shown.
27 changes: 19 additions & 8 deletions electrical_code/drive/movement_2motors/movement_2motors.ino
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ int angle;
int FBdirection;
int dist;
String inputString;
char inputArr[8];



Expand Down Expand Up @@ -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);
Expand All @@ -88,36 +91,44 @@ 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)
// 3 characters - distance
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);
}
}

Expand Down
9 changes: 6 additions & 3 deletions electrical_code/wheels.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down Expand Up @@ -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)
4 changes: 3 additions & 1 deletion electrical_code/wheels_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit bc26137

Please sign in to comment.