ch-10 clean
This commit is contained in:
parent
c632222255
commit
b595f3be54
@ -2,11 +2,12 @@ import time
|
|||||||
import robot
|
import robot
|
||||||
import pid
|
import pid
|
||||||
|
|
||||||
|
|
||||||
class FollowObject:
|
class FollowObject:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.max_speed = 0.9
|
self.max_speed = 0.9
|
||||||
self.follow_pid = pid.PID(0.1, 0.1, 0.015, 15)
|
self.follow_pid = pid.PID(0.1, 0.1, 0.015, 15)
|
||||||
self.last_time = time.monotonic_ns()
|
self.last_time = time.monotonic()
|
||||||
self.left_dist = 0
|
self.left_dist = 0
|
||||||
self.pid_output = 0
|
self.pid_output = 0
|
||||||
|
|
||||||
@ -19,7 +20,7 @@ class FollowObject:
|
|||||||
self.left_dist = robot.left_distance.distance
|
self.left_dist = robot.left_distance.distance
|
||||||
|
|
||||||
# calculate time delta
|
# calculate time delta
|
||||||
new_time = time.monotonic_ns()
|
new_time = time.monotonic()
|
||||||
time_delta = new_time - self.last_time
|
time_delta = new_time - self.last_time
|
||||||
self.last_time = new_time
|
self.last_time = new_time
|
||||||
|
|
||||||
@ -37,7 +38,6 @@ class FollowObject:
|
|||||||
|
|
||||||
def main_loop(self):
|
def main_loop(self):
|
||||||
robot.left_distance.start_ranging()
|
robot.left_distance.start_ranging()
|
||||||
self.last_time = time.monotonic()
|
|
||||||
while True:
|
while True:
|
||||||
self.movement_update()
|
self.movement_update()
|
||||||
|
|
||||||
@ -51,4 +51,5 @@ class FollowObject:
|
|||||||
robot.left_distance.clear_interrupt()
|
robot.left_distance.clear_interrupt()
|
||||||
robot.left_distance.stop_ranging()
|
robot.left_distance.stop_ranging()
|
||||||
|
|
||||||
|
|
||||||
FollowObject().start()
|
FollowObject().start()
|
||||||
|
|||||||
@ -54,6 +54,7 @@ send:
|
|||||||
|
|
||||||
assembled = adafruit_pioasm.assemble(program)
|
assembled = adafruit_pioasm.assemble(program)
|
||||||
|
|
||||||
|
|
||||||
class QuadratureEncoder:
|
class QuadratureEncoder:
|
||||||
def __init__(self, first_pin, second_pin, reversed=False):
|
def __init__(self, first_pin, second_pin, reversed=False):
|
||||||
"""Encoder with 2 pins. Must use sequential pins on the board"""
|
"""Encoder with 2 pins. Must use sequential pins on the board"""
|
||||||
@ -62,10 +63,10 @@ class QuadratureEncoder:
|
|||||||
frequency=0,
|
frequency=0,
|
||||||
first_in_pin=first_pin,
|
first_in_pin=first_pin,
|
||||||
jmp_pin=second_pin,
|
jmp_pin=second_pin,
|
||||||
in_pin_count=2
|
in_pin_count=2,
|
||||||
)
|
)
|
||||||
self.reversed = reversed
|
self.reversed = reversed
|
||||||
self._buffer = array.array('i', [0])
|
self._buffer = array.array("i", [0])
|
||||||
|
|
||||||
def read(self):
|
def read(self):
|
||||||
while self.sm.in_waiting:
|
while self.sm.in_waiting:
|
||||||
@ -74,4 +75,3 @@ class QuadratureEncoder:
|
|||||||
return -self._buffer[0]
|
return -self._buffer[0]
|
||||||
else:
|
else:
|
||||||
return self._buffer[0]
|
return self._buffer[0]
|
||||||
|
|
||||||
@ -29,6 +29,7 @@ def stop():
|
|||||||
motor_B1.duty_cycle = 0
|
motor_B1.duty_cycle = 0
|
||||||
motor_B2.duty_cycle = 0
|
motor_B2.duty_cycle = 0
|
||||||
|
|
||||||
|
|
||||||
def set_speed(motor, speed):
|
def set_speed(motor, speed):
|
||||||
# Swap motor pins if we reverse the speed
|
# Swap motor pins if we reverse the speed
|
||||||
if speed < 0:
|
if speed < 0:
|
||||||
@ -37,14 +38,15 @@ def set_speed(motor, speed):
|
|||||||
else:
|
else:
|
||||||
direction = motor
|
direction = motor
|
||||||
speed = min(speed, 1) # limit to 1.0
|
speed = min(speed, 1) # limit to 1.0
|
||||||
max_speed = 2**16-1
|
max_speed = 2 ** 16 - 1
|
||||||
|
|
||||||
|
|
||||||
direction[0].duty_cycle = int(max_speed * speed)
|
direction[0].duty_cycle = int(max_speed * speed)
|
||||||
direction[1].duty_cycle = 0
|
direction[1].duty_cycle = 0
|
||||||
|
|
||||||
|
|
||||||
def set_left(speed):
|
def set_left(speed):
|
||||||
set_speed(left_motor, speed)
|
set_speed(left_motor, speed)
|
||||||
|
|
||||||
|
|
||||||
def set_right(speed):
|
def set_right(speed):
|
||||||
set_speed(right_motor, speed)
|
set_speed(right_motor, speed)
|
||||||
|
|||||||
@ -16,7 +16,7 @@ class FollowObject:
|
|||||||
self.wifi = None
|
self.wifi = None
|
||||||
self.server = None
|
self.server = None
|
||||||
|
|
||||||
self.last_time = time.monotonic_ns()
|
self.last_time = time.monotonic()
|
||||||
self.left_dist = 0
|
self.left_dist = 0
|
||||||
self.pid_output = 0
|
self.pid_output = 0
|
||||||
|
|
||||||
@ -26,10 +26,7 @@ class FollowObject:
|
|||||||
def setup_wifi(self, app):
|
def setup_wifi(self, app):
|
||||||
print("Setting up wifi.")
|
print("Setting up wifi.")
|
||||||
self.wifi, esp = robot_wifi.connect_to_wifi()
|
self.wifi, esp = robot_wifi.connect_to_wifi()
|
||||||
self.server = adafruit_esp32spi_wsgiserver.WSGIServer(
|
self.server = adafruit_esp32spi_wsgiserver.WSGIServer(80, application=app)
|
||||||
80,
|
|
||||||
application=app
|
|
||||||
)
|
|
||||||
adafruit_esp32spi_wsgiserver.set_interface(esp)
|
adafruit_esp32spi_wsgiserver.set_interface(esp)
|
||||||
print("Starting server")
|
print("Starting server")
|
||||||
|
|
||||||
@ -38,13 +35,19 @@ class FollowObject:
|
|||||||
print(f"IP Address is {ip_int}")
|
print(f"IP Address is {ip_int}")
|
||||||
|
|
||||||
def index(self, request):
|
def index(self, request):
|
||||||
return 200, [('Content-Type', 'application/json')], [json.dumps(
|
return (
|
||||||
|
200,
|
||||||
|
[("Content-Type", "application/json")],
|
||||||
|
[
|
||||||
|
json.dumps(
|
||||||
{
|
{
|
||||||
"last_value": self.follow_pid.last_value,
|
"last_value": self.follow_pid.last_value,
|
||||||
"pid_output": self.pid_output,
|
"pid_output": self.pid_output,
|
||||||
"time": self.last_time
|
"time": self.last_time,
|
||||||
}
|
}
|
||||||
)]
|
)
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
def movement_update(self):
|
def movement_update(self):
|
||||||
# do we have data
|
# do we have data
|
||||||
@ -52,7 +55,7 @@ class FollowObject:
|
|||||||
self.left_dist = robot.left_distance.distance
|
self.left_dist = robot.left_distance.distance
|
||||||
|
|
||||||
# calculate time delta
|
# calculate time delta
|
||||||
new_time = time.monotonic_ns()
|
new_time = time.monotonic()
|
||||||
time_delta = new_time - self.last_time
|
time_delta = new_time - self.last_time
|
||||||
self.last_time = new_time
|
self.last_time = new_time
|
||||||
|
|
||||||
@ -69,7 +72,6 @@ class FollowObject:
|
|||||||
|
|
||||||
def main_loop(self):
|
def main_loop(self):
|
||||||
robot.left_distance.start_ranging()
|
robot.left_distance.start_ranging()
|
||||||
self.last_time = time.monotonic()
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
self.movement_update()
|
self.movement_update()
|
||||||
@ -94,4 +96,5 @@ class FollowObject:
|
|||||||
robot.left_distance.clear_interrupt()
|
robot.left_distance.clear_interrupt()
|
||||||
robot.left_distance.stop_ranging()
|
robot.left_distance.stop_ranging()
|
||||||
|
|
||||||
|
|
||||||
FollowObject().start()
|
FollowObject().start()
|
||||||
|
|||||||
@ -54,6 +54,7 @@ send:
|
|||||||
|
|
||||||
assembled = adafruit_pioasm.assemble(program)
|
assembled = adafruit_pioasm.assemble(program)
|
||||||
|
|
||||||
|
|
||||||
class QuadratureEncoder:
|
class QuadratureEncoder:
|
||||||
def __init__(self, first_pin, second_pin, reversed=False):
|
def __init__(self, first_pin, second_pin, reversed=False):
|
||||||
"""Encoder with 2 pins. Must use sequential pins on the board"""
|
"""Encoder with 2 pins. Must use sequential pins on the board"""
|
||||||
@ -62,10 +63,10 @@ class QuadratureEncoder:
|
|||||||
frequency=0,
|
frequency=0,
|
||||||
first_in_pin=first_pin,
|
first_in_pin=first_pin,
|
||||||
jmp_pin=second_pin,
|
jmp_pin=second_pin,
|
||||||
in_pin_count=2
|
in_pin_count=2,
|
||||||
)
|
)
|
||||||
self.reversed = reversed
|
self.reversed = reversed
|
||||||
self._buffer = array.array('i', [0])
|
self._buffer = array.array("i", [0])
|
||||||
|
|
||||||
def read(self):
|
def read(self):
|
||||||
while self.sm.in_waiting:
|
while self.sm.in_waiting:
|
||||||
@ -74,4 +75,3 @@ class QuadratureEncoder:
|
|||||||
return -self._buffer[0]
|
return -self._buffer[0]
|
||||||
else:
|
else:
|
||||||
return self._buffer[0]
|
return self._buffer[0]
|
||||||
|
|
||||||
@ -29,6 +29,7 @@ def stop():
|
|||||||
motor_B1.duty_cycle = 0
|
motor_B1.duty_cycle = 0
|
||||||
motor_B2.duty_cycle = 0
|
motor_B2.duty_cycle = 0
|
||||||
|
|
||||||
|
|
||||||
def set_speed(motor, speed):
|
def set_speed(motor, speed):
|
||||||
# Swap motor pins if we reverse the speed
|
# Swap motor pins if we reverse the speed
|
||||||
if speed < 0:
|
if speed < 0:
|
||||||
@ -37,14 +38,15 @@ def set_speed(motor, speed):
|
|||||||
else:
|
else:
|
||||||
direction = motor
|
direction = motor
|
||||||
speed = min(speed, 1) # limit to 1.0
|
speed = min(speed, 1) # limit to 1.0
|
||||||
max_speed = 2**16-1
|
max_speed = 2 ** 16 - 1
|
||||||
|
|
||||||
|
|
||||||
direction[0].duty_cycle = int(max_speed * speed)
|
direction[0].duty_cycle = int(max_speed * speed)
|
||||||
direction[1].duty_cycle = 0
|
direction[1].duty_cycle = 0
|
||||||
|
|
||||||
|
|
||||||
def set_left(speed):
|
def set_left(speed):
|
||||||
set_speed(left_motor, speed)
|
set_speed(left_motor, speed)
|
||||||
|
|
||||||
|
|
||||||
def set_right(speed):
|
def set_right(speed):
|
||||||
set_speed(right_motor, speed)
|
set_speed(right_motor, speed)
|
||||||
|
|||||||
@ -54,6 +54,7 @@ send:
|
|||||||
|
|
||||||
assembled = adafruit_pioasm.assemble(program)
|
assembled = adafruit_pioasm.assemble(program)
|
||||||
|
|
||||||
|
|
||||||
class QuadratureEncoder:
|
class QuadratureEncoder:
|
||||||
def __init__(self, first_pin, second_pin, reversed=False):
|
def __init__(self, first_pin, second_pin, reversed=False):
|
||||||
"""Encoder with 2 pins. Must use sequential pins on the board"""
|
"""Encoder with 2 pins. Must use sequential pins on the board"""
|
||||||
@ -62,10 +63,10 @@ class QuadratureEncoder:
|
|||||||
frequency=0,
|
frequency=0,
|
||||||
first_in_pin=first_pin,
|
first_in_pin=first_pin,
|
||||||
jmp_pin=second_pin,
|
jmp_pin=second_pin,
|
||||||
in_pin_count=2
|
in_pin_count=2,
|
||||||
)
|
)
|
||||||
self.reversed = reversed
|
self.reversed = reversed
|
||||||
self._buffer = array.array('i', [0])
|
self._buffer = array.array("i", [0])
|
||||||
|
|
||||||
def read(self):
|
def read(self):
|
||||||
while self.sm.in_waiting:
|
while self.sm.in_waiting:
|
||||||
@ -74,4 +75,3 @@ class QuadratureEncoder:
|
|||||||
return -self._buffer[0]
|
return -self._buffer[0]
|
||||||
else:
|
else:
|
||||||
return self._buffer[0]
|
return self._buffer[0]
|
||||||
|
|
||||||
@ -29,6 +29,7 @@ def stop():
|
|||||||
motor_B1.duty_cycle = 0
|
motor_B1.duty_cycle = 0
|
||||||
motor_B2.duty_cycle = 0
|
motor_B2.duty_cycle = 0
|
||||||
|
|
||||||
|
|
||||||
def set_speed(motor, speed):
|
def set_speed(motor, speed):
|
||||||
# Swap motor pins if we reverse the speed
|
# Swap motor pins if we reverse the speed
|
||||||
if speed < 0:
|
if speed < 0:
|
||||||
@ -37,14 +38,15 @@ def set_speed(motor, speed):
|
|||||||
else:
|
else:
|
||||||
direction = motor
|
direction = motor
|
||||||
speed = min(speed, 1) # limit to 1.0
|
speed = min(speed, 1) # limit to 1.0
|
||||||
max_speed = 2**16-1
|
max_speed = 2 ** 16 - 1
|
||||||
|
|
||||||
|
|
||||||
direction[0].duty_cycle = int(max_speed * speed)
|
direction[0].duty_cycle = int(max_speed * speed)
|
||||||
direction[1].duty_cycle = 0
|
direction[1].duty_cycle = 0
|
||||||
|
|
||||||
|
|
||||||
def set_left(speed):
|
def set_left(speed):
|
||||||
set_speed(left_motor, speed)
|
set_speed(left_motor, speed)
|
||||||
|
|
||||||
|
|
||||||
def set_right(speed):
|
def set_right(speed):
|
||||||
set_speed(right_motor, speed)
|
set_speed(right_motor, speed)
|
||||||
|
|||||||
@ -20,7 +20,7 @@ class FollowWallApp:
|
|||||||
self.wifi = None
|
self.wifi = None
|
||||||
self.server = None
|
self.server = None
|
||||||
|
|
||||||
self.last_time = time.monotonic_ns()
|
self.last_time = time.monotonic()
|
||||||
self.left_dist = 0
|
self.left_dist = 0
|
||||||
self.pid_output = 0
|
self.pid_output = 0
|
||||||
|
|
||||||
@ -30,10 +30,7 @@ class FollowWallApp:
|
|||||||
def setup_wifi(self, app):
|
def setup_wifi(self, app):
|
||||||
print("Setting up wifi.")
|
print("Setting up wifi.")
|
||||||
self.wifi, esp = robot_wifi.connect_to_wifi()
|
self.wifi, esp = robot_wifi.connect_to_wifi()
|
||||||
self.server = adafruit_esp32spi_wsgiserver.WSGIServer(
|
self.server = adafruit_esp32spi_wsgiserver.WSGIServer(80, application=app)
|
||||||
80,
|
|
||||||
application=app
|
|
||||||
)
|
|
||||||
adafruit_esp32spi_wsgiserver.set_interface(esp)
|
adafruit_esp32spi_wsgiserver.set_interface(esp)
|
||||||
print("Starting server")
|
print("Starting server")
|
||||||
|
|
||||||
@ -42,13 +39,19 @@ class FollowWallApp:
|
|||||||
print(f"IP Address is {ip_int}")
|
print(f"IP Address is {ip_int}")
|
||||||
|
|
||||||
def index(self, request):
|
def index(self, request):
|
||||||
return 200, [('Content-Type', 'application/json')], [json.dumps(
|
return (
|
||||||
|
200,
|
||||||
|
[("Content-Type", "application/json")],
|
||||||
|
[
|
||||||
|
json.dumps(
|
||||||
{
|
{
|
||||||
"last_value": self.follow_pid.last_value,
|
"last_value": self.follow_pid.last_value,
|
||||||
"pid_output": self.pid_output,
|
"pid_output": self.pid_output,
|
||||||
"time": self.last_time
|
"time": self.last_time,
|
||||||
}
|
}
|
||||||
)]
|
)
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
def movement_update(self):
|
def movement_update(self):
|
||||||
# do we have data
|
# do we have data
|
||||||
@ -56,7 +59,7 @@ class FollowWallApp:
|
|||||||
self.left_dist = robot.left_distance.distance
|
self.left_dist = robot.left_distance.distance
|
||||||
|
|
||||||
# calculate time delta
|
# calculate time delta
|
||||||
new_time = time.monotonic_ns()
|
new_time = time.monotonic()
|
||||||
time_delta = new_time - self.last_time
|
time_delta = new_time - self.last_time
|
||||||
self.last_time = new_time
|
self.last_time = new_time
|
||||||
|
|
||||||
@ -73,7 +76,6 @@ class FollowWallApp:
|
|||||||
|
|
||||||
def main_loop(self):
|
def main_loop(self):
|
||||||
robot.left_distance.start_ranging()
|
robot.left_distance.start_ranging()
|
||||||
self.last_time = time.monotonic()
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
self.movement_update()
|
self.movement_update()
|
||||||
@ -98,4 +100,5 @@ class FollowWallApp:
|
|||||||
robot.left_distance.clear_interrupt()
|
robot.left_distance.clear_interrupt()
|
||||||
robot.left_distance.stop_ranging()
|
robot.left_distance.stop_ranging()
|
||||||
|
|
||||||
|
|
||||||
FollowWallApp().start()
|
FollowWallApp().start()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user