-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautonomous_command_publisher.py
43 lines (34 loc) · 1.15 KB
/
autonomous_command_publisher.py
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
from publishers import CommandPublisher
from subscribers import SensorSubscriber
import logging
import json
class AutonomousCommandPublisher(CommandPublisher):
def __init__(self):
super().__init__()
self.sensorSubscriber = SensorSubscriber()
def process_sensor_readings(self):
sensorReadings = self.sensorSubscriber.receive_readings()
if not sensorReadings:
return
logging.info(f"Received sensor readings: {json.dumps(sensorReadings)}")
commands = []
if sensorReadings["center"]:
commands.append("stop_turning")
commands.append("move_forward")
elif sensorReadings["left"]:
commands.append("stop_moving")
commands.append("turn_left")
elif sensorReadings["right"]:
commands.append("stop_moving")
commands.append("turn_right")
else:
commands.append("stop_turning")
commands.append("stop_moving")
message = ",".join(commands)
logging.info(f"Commanding: {message}")
self.publish_message(message)
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
publisher = AutonomousCommandPublisher()
while True:
publisher.process_sensor_readings()