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