Skip to content

Commit

Permalink
Merge pull request #1 from TheFloatingString/feature/motor-funcs
Browse files Browse the repository at this point in the history
Add motor funcs
  • Loading branch information
TheFloatingString authored Feb 9, 2025
2 parents 77d0867 + 0b7e6c0 commit 130701b
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions server/src/motor_funcs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env python3

# Adds the lib directory to the Python path
import sys
import os
import time
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

import paho.mqtt.client as mqtt
from sshkeyboard import listen_keyboard, stop_listening

# ------------------------------------------------------------------------------------
# Constants & Setup
# ------------------------------------------------------------------------------------
MQTT_BROKER_ADDRESS = "localhost"
MQTT_TOPIC = "robot/drive"

# Create MQTT client
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
client.connect(MQTT_BROKER_ADDRESS)
client.loop_start()

def press(key):
if key.lower() == 'w': # Forward
client.publish(MQTT_TOPIC, "forward")
elif key.lower() == 's': # Backward
client.publish(MQTT_TOPIC, "back")
elif key.lower() == 'a': # Left turn
client.publish(MQTT_TOPIC, "left")
elif key.lower() == 'd': # Right turn
client.publish(MQTT_TOPIC, "right")
elif key.lower() == 'q': # Quit
stop_listening()

def release(key):
# Stop motors when key is released
client.publish(MQTT_TOPIC, "stop")


def motor_forward():
press('a')
time.sleep(2.0)
client.publish(MQTT_TOPIC, "stop")

def motor_backward():
press('d')
time.sleep(2.0)
client.publish(MQTT_TOPIC, "stop")

def motor_left():
press('s')
time.sleep(2.0)
client.publish(MQTT_TOPIC, "stop")

def motor_right():
press('w')
time.sleep(2.0)
client.publish(MQTT_TOPIC, "stop")

0 comments on commit 130701b

Please sign in to comment.