Skip to content

Commit

Permalink
Integrated basic send video functionality into controller.py
Browse files Browse the repository at this point in the history
Working on making the networking code more resistant to crashing, and capable
of self recovery, but stability after an error is still an issue. Need to a
restart count to the watchdog, to catch this and restart the controller.
  • Loading branch information
Nocturnal42 committed Oct 6, 2018
1 parent 81dbfce commit 3f009e4
Show file tree
Hide file tree
Showing 9 changed files with 519 additions and 81 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ gpio.conf
#ignore (open?) files with ~ in name:
*~
*.pyc
*.swp

#ignore emacs temporary files
\#*\#
Expand Down
2 changes: 1 addition & 1 deletion audio_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

def getAudioDeviceByName(name):

text = subprocess.check_output(['aplay', '-l'])
text = subprocess.check_output(['arecord', '-l'])
lines = text.splitlines()
for line in lines:
if name in line:
Expand Down
65 changes: 50 additions & 15 deletions controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@
print ("Error in letsrobot.conf:", sys.exc_info()[0])
sys.exit()


handlingCommand = False
chat_module = None
move_handler = None
terminate = thread.allocate_lock()

# This is required to allow us to get True / False boolean values from the
# command line
Expand All @@ -55,17 +56,15 @@ def str2bool(v):
# TODO assess these and other options in the config to see which ones are most
# appropriate to be overidden from the command line.
# check the command line for and config file overrides.




parser = argparse.ArgumentParser(description='start robot control program')
parser.add_argument('--robot-id', help='Robot ID', default=robot_config.get('robot', 'robot_id'))
parser.add_argument('--info-server', help="Server that robot will connect to for information about servers and things", default=robot_config.get('misc', 'info_server'))
parser.add_argument('--type', help="Serial or motor_hat or gopigo2 or gopigo3 or l298n or motozero or pololu", default=robot_config.get('robot', 'type'))
parser.add_argument('--video', default=robot_config.get('camera', 'type'))
parser.add_argument('--custom-hardware', type=str2bool, default=robot_config.getboolean('misc', 'custom_hardware'))
parser.add_argument('--custom-tts', type=str2bool, default=robot_config.getboolean('misc', 'custom_tts'))
parser.add_argument('--custom-chat', type=str2bool, default=robot_config.getboolean('misc', 'custom_chat'))
parser.add_argument('--custom-video', type=str2bool, default=robot_config.getboolean('misc', 'custom_video'))
parser.add_argument('--ext-chat-command', type=str2bool, default=robot_config.getboolean('tts', 'ext_chat'))
parser.add_argument('--secure-cert', type=str2bool, default=robot_config.getboolean('misc', 'secure_cert'))
parser.add_argument('--debug-messages', type=str2bool, default=robot_config.getboolean('misc', 'debug_messages'))
Expand Down Expand Up @@ -187,6 +186,11 @@ def handle_command(args):
global handlingCommand
handlingCommand = True

# catch move commands that happen before the controller has fully
# loaded and set a move handler.
if move_handler == None:
return

if 'command' in args and 'robot_id' in args and args['robot_id'] == robotID:

if debug_messages:
Expand Down Expand Up @@ -235,6 +239,11 @@ def auto_wifi_task():
t = Timer(10, auto_wifi_task)
t.daemon = True
t.start()

def restart_controller(command, args):
if extended_command.is_authed(args['name']) == 2: # Owner
terminate.acquire()


# TODO : This really doesn't belong here, should probably be in start script.
# watch dog timer
Expand All @@ -259,8 +268,6 @@ def auto_wifi_task():
# If messenger is enabled, connect a chat socket for return messages
if robot_config.getboolean('messenger', 'enable'):
messengerSocket = networking.setupMessengerSocket()
messengerSocket.emit('chat_message', { 'message': '[Hello Bot] Hello world!', 'robot_id': '60582868', 'robot_name': 'Hello Bot', "secret": "iknowyourelookingatthisthatsfine"})


# If reverse SSH is enabled and if the key file exists, import it and hook it in.
if robot_config.getboolean('misc', 'reverse_ssh') and os.path.isfile(robot_config.get('misc', 'reverse-ssh-key-file')):
Expand Down Expand Up @@ -294,6 +301,36 @@ def auto_wifi_task():
move_handler = module.move

# Load a custom chat handler if enabled and exists
if commandArgs.custom_video:
if os.path.exists('video/video_custom.py'):
if (sys.version_info > (3, 0)):
video_module = importlib.import_module('video.video_custom')
else:
video_module = __import__('video.video_custom', fromlist=['video_custom'])
else:
print("Unable to find video/video_custom.py")
if (sys.version_info > (3, 0)):
video_module = importlib.import_module('video.'+commandArgs.video)
else:
video_module = __import__("video."+commandArgs.video, fromlist=[commandArgs.video])
else:
if (sys.version_info > (3, 0)):
video_module = importlib.import_module('video.'+commandArgs.video)
else:
video_module = __import__("video."+commandArgs.video, fromlist=[commandArgs.video])

# Setup the video encoding
video_module.setup(robot_config)
video_module.start()

#load the extended chat commands
if ext_chat:
extended_command.setup(robot_config)
extended_command.move_handler=move_handler
move_handler = extended_command.move_auth
extended_command.add_command('.restart', restart_controller)

# Load the video handler
if commandArgs.custom_chat:
if os.path.exists('chat_custom.py'):
if (sys.version_info > (3, 0)):
Expand All @@ -305,18 +342,16 @@ def auto_wifi_task():

else:
print("Unable to find chat_custom.py")

#load the extended chat commands
if ext_chat:
extended_command.setup(robot_config)
extended_command.move_handler=move_handler
move_handler = extended_command.move_auth

# add auto wifi task
if auto_wifi:
auto_wifi_task()

while True:
import atexit
atexit.register(print, "Attempting to clean up and exit nicely")

while not terminate.locked():
time.sleep(1)
watchdog.watch()


sys.exit()
32 changes: 6 additions & 26 deletions extended_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
dev_mode_mods = False
anon_control = True
owner = None
v4l2_ctl = None
robot_id = None
api_key = None
stationary = None
Expand All @@ -64,13 +63,11 @@

def setup(robot_config):
global owner
global v4l2_ctl
global robot_id
global api_key
global buttons_json

owner = robot_config.get('robot', 'owner')
v4l2_ctl = robot_config.get('misc', 'v4l2-ctl')
robot_id = robot_config.get('robot', 'robot_id')

if robot_config.has_option('robot', 'api_key'):
Expand Down Expand Up @@ -277,24 +274,6 @@ def show_exclusive_handler(command, args):
robot_util.setShowExclusive(True, robot_id, api_key)
return

def brightness(command, args):
if len(command) > 1:
if is_authed(args['name']): # Moderator
os.system(v4l2_ctl + " --set-ctrl brightness=" + command[1])
print("brightness set to " + command[1])

def contrast(command, args):
if len(command) > 1:
if is_authed(args['name']): # Moderator
os.system(v4l2_ctl + " --set-ctrl contrast=" + command[1])
print("contrast set to " + command[1])

def saturation(command, args):
if len(command) > 2:
if is_authed(args['name']): # Moderator
os.system(v4l2_ctl + " --set-ctrl saturation=" + command[1])
print("saturation set to " + command[1])

# This is a dictionary of commands and their handler functions
commands={ '.anon' : anon_handler,
'.ban' : ban_handler,
Expand All @@ -308,18 +287,19 @@ def saturation(command, args):
'.public' : public_mode_handler,
'.show_exclusive': show_exclusive_handler,
'.word_filter': word_filter_handler,
'.brightness' : brightness,
'.contrast' : contrast,
'.saturation' : saturation,
'.stationary' : stationary_handler
}

def handler(args):
command = args['message']
# TODO : This will not work with robot names with spaces, update it to split on ']'
# [1:]
command = command.split(']')[1:][0].split(' ')[1:]
print(command)

try:
command = command.split(']')[1:][0].split(' ')[1:]
print(command)
except IndexError: # catch empty messages
return

if command != None:
if command[0] in commands:
Expand Down
1 change: 1 addition & 0 deletions hardware/l298n.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def setup(robot_config):
mode=GPIO.getmode()
print(" mode ="+str(mode))

GPIO.setwarnings(False)
GPIO.cleanup()

if robot_config.getboolean('tts', 'ext_chat'): #ext_chat enabled, add motor commands
Expand Down
Loading

0 comments on commit 3f009e4

Please sign in to comment.