Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Mannvika committed Apr 25, 2024
2 parents 8c60b65 + 6a15e91 commit 194204c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
40 changes: 40 additions & 0 deletions electrical_code/arduino_wheels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import serial
import time

arduino = serial.Serial(port='COM5', baudrate=115200, timeout=.1)
time_divisor = 10

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


def turnRight(angle):
command = "0" + str(angle).zfill(3) + "0000"
write_serial(command)
time.sleep(angle * (time_divisor) / 1000)


def turnLeft(angle):
command = "1" + str(angle).zfill(3) + "0000"
write_serial(command)
time.sleep(angle * (time_divisor) / 1000)


def goForward(dist):
print('hello')
command = "00001" + str(dist).zfill(3)
print("forward: " + command)
write_serial(command)
time.sleep(dist * (time_divisor) / 1000)


def goBackward(dist):
command = "00000" + str(dist).zfill(3)
print("backward: " + command)
write_serial(command)
time.sleep(dist * (time_divisor) / 1000)

if __name__ == "__main__":
goForward(100)
time.sleep(2)
1 change: 1 addition & 0 deletions electrical_code/serialSend.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def move(angle, dist):
angle = '0' * (3 - len(angle)) + angle
dist = '0' * (3 - len(dist)) + dist
command = angle + dist
print(command)
write_read(command)
#print(command)

Expand Down
1 change: 1 addition & 0 deletions electrical_code/wheels.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Wheels:
time_divisor = 10

def __init__(self, port_num):
print(port_num)
self.arduino = serial.Serial(port=port_num, baudrate=115200, timeout=0.1)
self.arduino.flush()

Expand Down
21 changes: 12 additions & 9 deletions electrical_code/wheels_test.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from wheels import Wheels
import serial

# Instantiate the Wheels class with your port number
myFirstWheel = Wheels("COM5")
while True:
arduino = serial.Serial(port='COM5', baudrate=115200, timeout=.1)

# 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(111) # Go backward 50 units
# myFirstWheel.turnRight("90") # Turn right by 90 degrees
# myFirstWheel.turnLeft("45") # Turn left by 45 degrees

# Test movement commands
# myFirstWheel.move(1,000,110)
# myFirstWheel.goForward(100) # Go forward 100 units
myFirstWheel.move("", 100, 431)
# 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 194204c

Please sign in to comment.