Skip to content

Testing branch #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added bin/pyserial-miniterm.exe
Binary file not shown.
Binary file added bin/pyserial-ports.exe
Binary file not shown.
67 changes: 67 additions & 0 deletions pydexarm/Color.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import cv2
import numpy as np
import os

def close_camera_app():
os.system("taskkill /IM WindowsCamera.exe /F 2>nul") # Closes Camera app without printing errors

def detect_colors(frame):
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
height, width, _ = frame.shape
total_pixels = height * width

# Define improved color ranges in HSV
colors = {
"Red": [(0, 120, 70), (10, 255, 255)], # First range for red
"Red2": [(170, 120, 70), (180, 255, 255)], # Second range for red
"Green": [(35, 50, 50), (85, 255, 255)],
"Blue": [(90, 50, 50), (130, 255, 255)],
"Black": [(0, 0, 0), (180, 50, 50)], # Adjusted black range
}

detected_any = False
color_percentages = {}

for color, (lower, upper) in colors.items():
mask = cv2.inRange(hsv, np.array(lower), np.array(upper))
color_pixels = cv2.countNonZero(mask)
percentage = (color_pixels / total_pixels) * 100

if percentage > 1: # Only show colors with significant presence
color_percentages[color.replace("2", "")] = round(percentage, 2)

contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

for contour in contours:
if cv2.contourArea(contour) > 2000: # Increased threshold for stability
x, y, w, h = cv2.boundingRect(contour)
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(frame, color.replace("2", ""), (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
detected_any = True

if detected_any:
close_camera_app() # Close camera app when detection happens

# Display color percentages on screen
y_offset = 30
for color, percentage in color_percentages.items():
cv2.putText(frame, f"{color}: {percentage}%", (10, y_offset), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
y_offset += 30

cap = cv2.VideoCapture(0)

while cap.isOpened():
ret, frame = cap.read()
if not ret:
break

frame = cv2.flip(frame, 1) # Flip the camera horizontally
detect_colors(frame)

cv2.imshow("Camera Feed", frame)

if cv2.waitKey(1) & 0xFF == ord('q') or cv2.getWindowProperty("Camera Feed", cv2.WND_PROP_VISIBLE) < 1:
break

cap.release()
cv2.destroyAllWindows()
119 changes: 84 additions & 35 deletions pydexarm/example.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,88 @@
from pydexarm import Dexarm
import time
import tkinter as tk
import threading

'''windows'''
dexarm = Dexarm(port="COM67")
'''mac & linux'''
# device = Dexarm(port="/dev/tty.usbmodem3086337A34381")

dexarm.go_home()

dexarm.move_to(50, 300, 0)
dexarm.move_to(50, 300, -50)
dexarm.air_picker_pick()
dexarm.move_to(50, 300, 0)
dexarm.move_to(-50, 300, 0)
dexarm.move_to(-50, 300, -50)
dexarm.air_picker_place()

dexarm.go_home()

'''DexArm sliding rail Demo'''
'''
dexarm.conveyor_belt_forward(2000)
time.sleep(20)
dexarm.conveyor_belt_backward(2000)
time.sleep(10)
dexarm.conveyor_belt_stop()
'''

'''DexArm sliding rail Demo'''
'''
dexarm.go_home()
dexarm.sliding_rail_init()
dexarm.move_to(None,None,None,0)
dexarm.move_to(None,None,None,100)
dexarm.move_to(None,None,None,50)
dexarm.move_to(None,None,None,200)
'''
# Initialize the Dexarm
dexarm = Dexarm(port="COM10")

# Function to move the arm when the light is red
def move_arm(x, y, z):
# Move arm to the specified position
dexarm.move_to(x, y, z)
# Return the arm to home position
dexarm.go_home()

# Create the traffic light cycle
def cycle_lights(canvas, red_light, yellow_light, green_light, durations):
colors = ["red", "yellow", "green"]
current_color = 0

# Cycle the traffic light indefinitely
def update_lights():
nonlocal current_color

# Reset all lights to gray first
canvas.itemconfig(red_light, fill="gray")
canvas.itemconfig(yellow_light, fill="gray")
canvas.itemconfig(green_light, fill="gray")

# Get current light's properties
light_color = colors[current_color]
light_duration = durations[current_color]

# Change the color based on the current light
if light_color == "red":
canvas.itemconfig(red_light, fill="red")
print("Red light: Moving arm!")
threading.Thread(target=move_arm, args=(10, 410, 0)).start() # Move the arm when red

elif light_color == "yellow":
canvas.itemconfig(yellow_light, fill="yellow")
print("Yellow light: Arm is stationary.")

elif light_color == "green":
canvas.itemconfig(green_light, fill="green")
print("Green light: Moving arm to a green-specific position.")
threading.Thread(target=move_arm, args=(-280, 410, 0)).start()

# Move to the next light
current_color = (current_color + 1) % len(colors)

# Schedule the next light update based on the current light's duration
root.after(light_duration, update_lights)

# Start the update loop
update_lights()

# Setup for the GUI to show the traffic light cycle
def setup_gui():
global root
root = tk.Tk()
root.title("Traffic Light Cycle")

# Create a canvas to draw the traffic light
canvas = tk.Canvas(root, width=200, height=400)
canvas.pack()

# Draw the traffic light (a simple vertical rectangle)
canvas.create_rectangle(50, 50, 150, 350, outline="black", width=2, fill="gold")

# Create three circles to represent the lights (Red, Yellow, Green)
red_light = canvas.create_oval(60, 60, 140, 140, fill="gray") # Start with gray
yellow_light = canvas.create_oval(60, 160, 140, 240, fill="gray")
green_light = canvas.create_oval(60, 260, 140, 340, fill="gray")

# Set durations for each light (in milliseconds)
durations = [10000, 2000, 4000] # Red, Yellow, Green

# Call the function to start cycling the lights
cycle_lights(canvas, red_light, yellow_light, green_light, durations)

root.mainloop()

# Run the GUI setup
setup_gui()

# Close the DexArm connection
dexarm.close()
87 changes: 87 additions & 0 deletions pydexarm/example2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
from pydexarm import Dexarm
import tkinter as tk
import threading

# Initialize the Dexarm
dexarm = Dexarm(port="COM10")

# Function to move the arm when the light is green
def move_arm(x, y, z):
dexarm.move_to(x, y, z) # Move arm to the specified position
dexarm.go_home() # Return the arm to home position

# A wrapper function to run `move_arm` in a separate thread
def move_arm_async(x, y, z):
threading.Thread(target=move_arm, args=(x, y, z), daemon=True).start()

# Create the traffic light cycle
def cycle_lights(canvas, red_light, yellow_light, green_light):
# Reversed order: Green → Yellow → Red
colors = ["green", "yellow", "red"]
durations = [4000, 2000, 5000] # Durations in milliseconds for each light (green, yellow, red)
current_color = 0

# Cycle the traffic light indefinitely
def update_lights():
nonlocal current_color

# Reset all lights to gray first
canvas.itemconfig(red_light, fill="gray")
canvas.itemconfig(yellow_light, fill="gray")
canvas.itemconfig(green_light, fill="gray")

# Get the current light's properties
light_color = colors[current_color]
duration = durations[current_color]

# Change the light color and perform actions for each light
if light_color == "green":
canvas.itemconfig(green_light, fill="green")
print("Green light: Moving arm to a green-specific position.")
move_arm_async(10, 410, 0) # Move the arm asynchronously during the green light

elif light_color == "yellow":
canvas.itemconfig(yellow_light, fill="yellow")
print("Yellow light: Arm is stationary.")

elif light_color == "red":
canvas.itemconfig(red_light, fill="red")
print("Red light: Moving arm to a red-specific position.")

# Move to the next light
current_color = (current_color + 1) % len(colors)

# Schedule the next light update based on the duration
root.after(duration, update_lights)

# Start the update loop
update_lights()

# Setup for the GUI to show the traffic light cycle
def setup_gui():
global root
root = tk.Tk()
root.title("Traffic Light Cycle")

# Create a canvas to draw the traffic light
canvas = tk.Canvas(root, width=200, height=400)
canvas.pack()

# Draw the traffic light (a simple vertical rectangle)
canvas.create_rectangle(50, 50, 150, 350, outline="black", width=2, fill="gold")

# Create three circles to represent the lights (Red, Yellow, Green)
red_light = canvas.create_oval(60, 60, 140, 140, fill="gray") # Start with gray, will change
yellow_light = canvas.create_oval(60, 160, 140, 240, fill="gray")
green_light = canvas.create_oval(60, 260, 140, 340, fill="gray")

# Call the function to start cycling the lights
cycle_lights(canvas, red_light, yellow_light, green_light)

root.mainloop()

# Run the GUI setup
setup_gui()

# Close the DexArm connection
dexarm.close()
2 changes: 1 addition & 1 deletion pydexarm/pydexarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,4 @@ def close(self):
"""
Release the serial port.
"""
self.ser.close()
self.ser.close()
95 changes: 95 additions & 0 deletions pydexarm/soundtest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
from pydexarm import Dexarm
import tkinter as tk
import threading
import winsound # Add winsound module for sound effects

# Initialize the Dexarm
dexarm = Dexarm(port="COM10")

# Function to play sound
def play_sound(frequency, duration):
winsound.Beep(frequency, duration)

# Function to move the arm when the light is green
def move_arm(x, y, z):
dexarm.move_to(x, y, z) # Move arm to the specified position
dexarm.go_home() # Return the arm to home position

# A wrapper function to run `move_arm` in a separate thread
def move_arm_async(x, y, z):
threading.Thread(target=move_arm, args=(x, y, z), daemon=True).start()

# Create the traffic light cycle
def cycle_lights(canvas, red_light, yellow_light, green_light):
# Reversed order: Green → Yellow → Red
colors = ["green", "yellow", "red"]
durations = [4000, 2000, 5000] # Durations in milliseconds for each light (green, yellow, red)
current_color = 0

# Cycle the traffic light indefinitely
def update_lights():
nonlocal current_color

# Reset all lights to gray first
canvas.itemconfig(red_light, fill="gray")
canvas.itemconfig(yellow_light, fill="gray")
canvas.itemconfig(green_light, fill="gray")

# Get the current light's properties
light_color = colors[current_color]
duration = durations[current_color]

# Change the light color and perform actions for each light
if light_color == "green":
canvas.itemconfig(green_light, fill="green")
print("Green light: Moving arm to a green-specific position.")
move_arm_async(10, 410, 0) # Move the arm asynchronously during the green light
play_sound(1000, 500) # Play a high-pitched beep for green light

elif light_color == "yellow":
canvas.itemconfig(yellow_light, fill="yellow")
print("Yellow light: Arm is stationary.")
play_sound(800, 300) # Play a medium-pitched beep for yellow light

elif light_color == "red":
canvas.itemconfig(red_light, fill="red")
print("Red light: Moving arm to a red-specific position.")
play_sound(500, 1000) # Play a low-pitched beep for red light

# Move to the next light
current_color = (current_color + 1) % len(colors)

# Schedule the next light update based on the duration
root.after(duration, update_lights)

# Start the update loop
update_lights()

# Setup for the GUI to show the traffic light cycle
def setup_gui():
global root
root = tk.Tk()
root.title("Traffic Light Cycle")

# Create a canvas to draw the traffic light
canvas = tk.Canvas(root, width=200, height=400)
canvas.pack()

# Draw the traffic light (a simple vertical rectangle)
canvas.create_rectangle(50, 50, 150, 350, outline="black", width=2, fill="gold")

# Create three circles to represent the lights (Red, Yellow, Green)
red_light = canvas.create_oval(60, 60, 140, 140, fill="gray") # Start with gray, will change
yellow_light = canvas.create_oval(60, 160, 140, 240, fill="gray")
green_light = canvas.create_oval(60, 260, 140, 340, fill="gray")

# Call the function to start cycling the lights
cycle_lights(canvas, red_light, yellow_light, green_light)

root.mainloop()

# Run the GUI setup
setup_gui()

# Close the DexArm connection
dexarm.close()
Loading