-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsevernew.py
More file actions
76 lines (57 loc) · 2.33 KB
/
severnew.py
File metadata and controls
76 lines (57 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from machine import Pin, PWM, UART
import sys
import time
from random import randint
def degrees_to_duty(degrees):
min_pulse_width = 1000 # Minimum pulse width in microseconds
max_pulse_width = 2000 # Maximum pulse width in microseconds
pulse_width = min_pulse_width + (degrees / 180.0) * (max_pulse_width - min_pulse_width)
return int((pulse_width / 20000.0) * 65535)
uart = UART(1, baudrate=9600, tx=Pin(4), rx=Pin(5))
# Initialize servos on pins
servo1 = PWM(0, freq=50)
servo2 = PWM(3, freq=50)
servo3 = PWM(4, freq=50)
servo4 = PWM(7, freq=50)
servo5 = PWM(8, freq=50)
servo6 = PWM(11, freq=50)
servo7 = PWM(12, freq=50)
servo8 = PWM(15, freq=50)
def listen_serial():
while True: # Infinite loop
if uart.any(): # Check if there's any data available
message = uart.read().decode('utf-8').strip() # Read and decode the data
if message.isdigit(): # Check if the message is a digit
print('Position acquired:', message)
position = int(message) # Convert to integer
# Further processing of 'position' can go here
else:
print('Invalid input, not a digit:', message)
time.sleep(0.1) # Small delay to avoid busy waiting
def move_servos(servo, pos):# + servo1
movement = degrees_to_duty(pos)
servo.duty_u16(movement)
time.sleep(0.01) # Wait for 1 second
print(f"Servo moved")
# Stop all servos
servo.duty_u16(0) # Set duty cycle to 0 to stop the servo
servos = [servo1, servo2, servo3, servo4, servo5, servo6, servo7, servo8]
# # Run the servo movement
# while True:
# # positions = listen_serial() # "30, 35, 120, .... *8"
# # move_servos(pos) # +servo you ant to move)
# try:
# servo_index = int(input("pick which servo to move: ")) -1
# pos = int(input("enter a number for pos: "))
# if 0 <= servo_index < len(servos):
# move_servos(servos[servo_index], pos)
# except ValueError:
# print("Input error, Try a different number")
try:
listen_serial()
except KeyboardInterrupt:
print("Exiting...")
# for i in range(7):
# for values in pos:
# move_servos(servos[i], pos[i])
# print(f'servo {i} is moving to {pos[i]}')