Created
March 9, 2017 05:35
-
-
Save reefwing/123386d60cfec493dd4e46c6e5794348 to your computer and use it in GitHub Desktop.
Raspberry Pi Python Robot Class - see http://reefwingrobotics.blogspot.com.au/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# RS_Robot.py - Robot Class for the Raspberry Pi | |
# | |
# 4 March 2017 - 1.0 Original Issue | |
# | |
# Reefwing Software | |
# Simplified BSD Licence - see bottom of file. | |
import RPi.GPIO as GPIO | |
import os, signal | |
from time import sleep | |
from enum import Enum, unique | |
from RS_Servo import Servo | |
from RS_Server import Server | |
from RS_MotorControl import MotorControl | |
@unique | |
class Task(Enum): | |
HALT = 1 | |
REMOTE = 2 | |
PATROL = 3 | |
class Robot(): | |
# Robot class - includes motor control, 2 servos and a web server | |
def __init__(self): | |
# Create a new robot instance | |
print("Robot booting...") | |
self.server = Server(self) # start web server for remote control | |
print(self.server) | |
self.pan_servo = Servo(5, 3, 11) # servo which controls ultrasonic pan | |
self.tilt_servo = Servo(6, 2, 11) # servo which controls ultrasonic tilt | |
self.motor_control = MotorControl() # create a new motor control instance | |
self.pan_servo.start() # start servo PWM | |
self.tilt_servo.start() | |
# Command method look up table for parse_command() | |
self.CMD_DICT = {"f" : self.motor_control.forward, | |
"l" : self.motor_control.left, | |
"r" : self.motor_control.right, | |
"b" : self.motor_control.back, | |
"s" : self.motor_control.stop, | |
"+" : self.motor_control.faster, | |
"-" : self.motor_control.slower, | |
"m" : lambda: self.assign_task(Task.REMOTE), | |
"a" : lambda: self.assign_task(Task.PATROL)} | |
def __str__(self): | |
# Return string representation of robot | |
return "Robot Motor Control: base time - {0} sec, duty - {1}%".format(self.motor_control.base_time, self.motor_control.duty) | |
def assign_task(self, TID): | |
# Stub - will assign new robot task | |
pass | |
def parse_command(self, cmd): | |
# Process and action command request | |
print("Received command: ", cmd) | |
try: | |
cmd_method = self.CMD_DICT.get(cmd) | |
except KeyError: | |
print("ERROR:: Unknown Command: ", cmd) | |
cmd_method = lambda: self.assign_task(Task.HALT) | |
cmd_method() | |
def test(self): | |
# Self Test robot and run diagnostics | |
print(self) | |
print("Motor Test...") | |
self.motor_control.speed(20) | |
self.motor_control.right_forward() | |
sleep(1) | |
self.motor_control.stop() | |
sleep(1) | |
self.motor_control.left_forward() | |
sleep(1) | |
self.motor_control.stop() | |
print("Servo Test...") | |
self.pan_servo.scan() | |
self.tilt_servo.scan() | |
sleep(2) | |
# Move the ultrasonic sensor out of the view of the camera | |
print("Moving servos to manual control position") | |
self.pan_servo.centre() | |
self.tilt_servo.max_dc() | |
sleep(1) | |
# Stop servo chattering by turning off PWM | |
self.pan_servo.stop() | |
self.tilt_servo.stop() | |
print("Commence video streaming...") | |
self.server.start() | |
def cleanup(self): | |
self.server.cleanup() | |
self.motor_control.cleanup() | |
self.pan_servo.cleanup() | |
self.tilt_servo.cleanup() | |
def main(): | |
robot = Robot() # create a new robot instance | |
def endProcess(signum = None, frame = None): | |
# Called on process termination. | |
if signum is not None: | |
SIGNAL_NAMES_DICT = dict((getattr(signal, n), n) for n in dir(signal) if n.startswith('SIG') and '_' not in n ) | |
print("signal {} received by process with PID {}".format(SIGNAL_NAMES_DICT[signum], os.getpid())) | |
print("\n-- Terminating program --") | |
print("Cleaning up PWM and GPIO...") | |
robot.cleanup() | |
GPIO.cleanup() | |
print("Done.") | |
exit(0) | |
# Assign handler for process exit | |
signal.signal(signal.SIGTERM, endProcess) | |
signal.signal(signal.SIGINT, endProcess) | |
signal.signal(signal.SIGHUP, endProcess) | |
signal.signal(signal.SIGQUIT, endProcess) | |
robot.test() | |
endProcess() | |
if __name__ == "__main__": | |
# execute only if run as a script | |
main() | |
## Copyright (c) 2017, Reefwing Software | |
## All rights reserved. | |
## | |
## Redistribution and use in source and binary forms, with or without | |
## modification, are permitted provided that the following conditions are met: | |
## | |
## 1. Redistributions of source code must retain the above copyright notice, this | |
## list of conditions and the following disclaimer. | |
## 2. Redistributions in binary form must reproduce the above copyright notice, | |
## this list of conditions and the following disclaimer in the documentation | |
## and/or other materials provided with the distribution. | |
## | |
## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
## ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
## WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
## DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | |
## ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
## (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
## LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
## ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
## SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment