Skip to content

Commit 5cf4857

Browse files
committed
2 parents 5faab4a + bd615fd commit 5cf4857

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

pythonbaddiecode/robotpath.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import serial
2+
import time
3+
import matplotlib.pyplot as plt
4+
5+
#velocity at full is 0.8 second per breadboard
6+
#0.5 breadboards per second
7+
# Global variables for joystick position
8+
xpos_data, ypos_data = [], []
9+
dis_trav = 0.0
10+
# Function to update joystick position
11+
def update_joystick_position(xpos, ypos):
12+
global dis_trav
13+
xpos_data.append(xpos/50)
14+
ypos_data.append(ypos/50)
15+
# Limit data to last 100 points for better visualization
16+
if len(xpos_data) > 1:
17+
xpos_data.pop(0)
18+
ypos_data.pop(0)
19+
# Update plot
20+
plt.clf() # Clear the previous plot
21+
plt.plot(xpos_data, ypos_data, 'o-', color='blue') # Plot joystick position
22+
plt.xlim(-1, 1) # Set x-axis limit
23+
plt.ylim(-1, 1) # Set y-axis limit
24+
plt.xlabel('X Position')
25+
plt.ylabel('Y Position')
26+
plt.title('Joystick Position')
27+
plt.grid(True)
28+
29+
# Plot a unit circle
30+
circle = plt.Circle((0, 0), 1, color='lightgray', alpha=0.5)
31+
plt.gca().add_artist(circle)
32+
33+
# Plot the line as a vector coming out from the center
34+
plt.plot([0, xpos/50], [0, ypos/50], color='red')
35+
36+
# Add label showing the y value in the upper right corner with box
37+
plt.text(0.8, 0.8, f"Velocity in Breadboards Per Second: {round((ypos/50)*1.25,2)}", fontsize=10, ha='right', va='top', bbox=dict(facecolor='white', edgecolor='black', boxstyle='round,pad=0.5'))
38+
39+
# Add label showing the x value in the lower left corner with box
40+
plt.text(-0.8, -0.8, f"Angular Velocity Factor: {xpos/10} \n Total Distance Travelled (Breadboards) = {round(dis_trav+ypos/50*1.25*0.3*0.6,2)}", fontsize=10, ha='left', va='bottom', bbox=dict(facecolor='white', edgecolor='black', boxstyle='round,pad=0.5'))
41+
dis_trav += abs(ypos/50*1.25*0.3*0.6)
42+
plt.draw()
43+
plt.pause(0.001)
44+
45+
# Configure the serial port
46+
ser = serial.Serial(
47+
port='COM16',
48+
baudrate=9600,
49+
parity=serial.PARITY_NONE,
50+
stopbits=serial.STOPBITS_TWO,
51+
bytesize=serial.EIGHTBITS
52+
)
53+
54+
plt.ion() # Turn on interactive mode for Matplotlib
55+
56+
# Create a figure
57+
fig = plt.figure()
58+
59+
# Function to handle figure close event
60+
def on_close(event):
61+
plt.close() # Close the plot window immediately
62+
ser.close() # Close the serial port
63+
exit() # Exit the program
64+
65+
# Attach the close event handler to the figure
66+
fig.canvas.mpl_connect('close_event', on_close)
67+
68+
while True:
69+
try:
70+
# Read data from serial port
71+
strin = ser.readline()
72+
73+
if strin:
74+
if len(strin) == 9 and strin[0] != '1':
75+
xpos = int(strin[0:3])
76+
if (xpos > 50):
77+
xpos = 0
78+
ypos = 0 #this handles if it accidentally read the frequency strin
79+
ypos = int(strin[4:])
80+
81+
print("Joystick positions: %d, %d" % (xpos, ypos))
82+
update_joystick_position(xpos, ypos)
83+
elif len(strin) == 11:
84+
mag_freq = float(strin[0:])
85+
print("Magnitude of frequency: %.2f" % mag_freq)
86+
87+
except serial.SerialException as e:
88+
print("Serial communication error:", e)
89+
# Attempt to reopen the serial port
90+
ser.close()
91+
ser.open()
92+
except Exception as e:
93+
print("An error occurred:", e)
94+

0 commit comments

Comments
 (0)