Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add motor funcs #1

Merged
merged 1 commit into from
Feb 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")