- Python 3.11
- No additional requirements
python -m src
Or just press F5 in VSCode.
File: src/car.py
Car()
: Create an instanceconnect()
: Asynchronously connect to the car's WiFi with tcpupdate_state()
: Asynchronously update the car's stateset_speed(...)
: Set the car's speeddistance
: Distance to an obstaclein_road
: Whether the car is on the correct pathhave_obstacle
: Whether there is an obstacle ahead (based on infrared signals)
from src.car import Car
from src.logging_config import setup_logging
import logging
from src.config import Config
import asyncio
logger = logging.getLogger(__name__)
# @attention await sleep some times is necessary to release thread for other coroutine.
async def control_car(car: Car):
car.set_speed(0.5, 0.5)
while True:
print(f"current state: {car.distance}, {car.in_road}, {car.have_obstacle}")
if car.distance is None or car.in_road is None or car.speed is None:
await asyncio.sleep(0.1)
continue
if car.distance < 20:
print("change state")
car.set_speed(0.5, 0.0)
else:
print("change state")
car.set_speed(0.5, 0.5)
await asyncio.sleep(0.1)
async def main():
setup_logging()
car = Car()
await car.connect()
await asyncio.gather(
car.update_state(),
control_car(car)
)
if __name__ == "__main__":
logger.setLevel(Config.LOG_LEVEL)
asyncio.run(main())
print("done")
File: src/robot_craft_car.py
class RobotCraftCar(Car)
: Further encapsulation for car some example
from src.car import Car
from enum import Enum
from typing import Callable
import asyncio
class Direction(Enum):
LEFT = "left"
RIGHT = "right"
class RobotCraftCar(Car):
def __init__(self):
super().__init__()
def straight(self):
self.set_speed(0.2,0.2)
def low_straight(self):
self.set_speed(0.2,0.2)
def turn_left(self):
self.set_speed(-0.1,0.1)
def turn_right(self):
self.set_speed(0.1,-0.1)
async def adjustment_dir(self, dir:Direction, duration:float, is_right_gesture:Callable):
time_left = duration
if (dir == Direction.LEFT):
self.turn_left()
else:
self.turn_right()
while time_left > 0:
if is_right_gesture():
break
time_left -= 0.1
await asyncio.sleep(0.1)
self.straight()
from src.robot_craft_car import RobotCraftCar as Car, Direction
from src.logging_config import setup_logging
import logging
from src.config import Config
import asyncio
logger = logging.getLogger(__name__)
async def control_car(car: Car):
car.set_speed(0.3, 0.3)
while True:
print(f"current state: {car.distance}, {car.in_road}, {car.have_obstacle}")
if car.distance is None or car.in_road is None or car.speed is None:
await asyncio.sleep(0.1)
continue
if not car.in_road:
await car.adjustment_dir(Direction.RIGHT, 1, lambda: car.in_road)
if car.distance < 20:
print("change state")
await car.adjustment_dir(Direction.RIGHT, 1, lambda: car.distance is not None and car.distance > 40)
await asyncio.sleep(0.1)
async def main():
setup_logging()
car = Car()
await car.connect()
await asyncio.gather(
car.update_state(),
control_car(car)
)
if __name__ == "__main__":
logger.setLevel(Config.LOG_LEVEL)
asyncio.run(main())
print("done")