diff --git a/bouteillederouge.py b/bouteillederouge.py index caf640d..e92afaa 100644 --- a/bouteillederouge.py +++ b/bouteillederouge.py @@ -2,6 +2,7 @@ import sys import requests import argparse +import subprocess from threading import Thread @@ -51,11 +52,10 @@ parser.print_help() sys.exit(1) - from subprocess import call configfile = '/tmp/poppy_config.yaml' - call(['python', 'bootstrap.py', - '--config-path', configfile, - 'localhost', args.creature]) + subprocess.call(['python', 'bootstrap.py', + '--config-path', configfile, + 'localhost', args.creature]) else: configfile = os.path.expanduser('~/.poppy_config.yaml') @@ -210,12 +210,41 @@ def done_updating(): @app.route('/camera', methods=['POST']) def switch_camera(): - checked = request.form['checked']; + checked = request.form['checked'] pm.update_config('robot.camera', True if checked == 'on' else False) flash('Your robot camera is now turned {}!'.format(checked), 'success') return ('', 204) +@app.route('/configure-motors') +def configure_motors(): + # Remove old poppy-configure output to avoid user confusion + try: + os.remove(pm.config.poppy_configure.logfile) + except OSError: + pass + return render_template('motor-configuration.html', motors=pm._get_robot_motor_list()) + + +@app.route('/call_poppy_configure', methods=['POST']) +def call_poppy_configure(): + motor = request.form['motor'] + pm.update_config('robot.motors', motor) + return ('', 204) + +@app.route('/settings/wifi') +def configure_wifi(): + return render_template('wifi.html') + +@app.route('/wifi/list') +def wifi_list(): + return + +@app.route('/configure_hostspot', methods=['POST']) +def hostspot_configuration(): + return + + @app.route('/ready-to-roll') def ready_to_roll(): with open(pm.config.info.logfile) as f: @@ -263,6 +292,16 @@ def update_raw_logs(): return Response(content, mimetype='text/plain') +@app.route('/api/configure_motors_logs') +def poppy_config_logs(): + try: + with open(pm.config.poppy_configure.logfile) as f: + content = f.read() + except IOError: + content = '' + return Response(content, mimetype='text/plain') + + def get_host(): host = pm.config.robot.name host = host if host == 'localhost' else '{}.local'.format(host) diff --git a/default_config.yaml b/default_config.yaml index 1c37779..71a8df1 100644 --- a/default_config.yaml +++ b/default_config.yaml @@ -12,3 +12,13 @@ update: url: https://raw.githubusercontent.com/poppy-project/raspoppy/master/auto-update.sh logfile: $home/.install.log lockfile: /tmp/poppy-update-pid.lock + +poppy_configure: + logfile: /tmp/poppy-config.log + poppup_startup: on + +wifi: + hostspot_conf_file: /boot/hotspot.txt + hostspot_service: rpi-access-point + default_ssid: Poppy Hotspot + default_password: poppyproject diff --git a/puppet_master.py b/puppet_master.py index c09e2af..bda1d9d 100644 --- a/puppet_master.py +++ b/puppet_master.py @@ -1,13 +1,16 @@ import os import time +import re import requests from subprocess import call, check_call from contextlib import closing from threading import Thread +from string import Template from poppyd import PoppyDaemon from config import Config, attrsetter +from pypot.creatures import installed_poppy_creatures class PuppetMaster(object): @@ -21,6 +24,7 @@ def __init__(self, DaemonCls, configfile, pidfile): self.config_handlers = { 'robot.camera': lambda _: self.restart(), 'robot.name': self._change_hostname, + 'robot.motors': self._configure_motors } self._updating = False @@ -85,6 +89,62 @@ def _change_hostname(self, name): call(['sudo', 'systemctl', 'restart', 'avahi-daemon.service']) self.restart() + def _get_robot_motor_list(self): + try: + RobotCls = installed_poppy_creatures[self.config.robot.creature] + return sorted(RobotCls.default_config['motors'].keys()) + except KeyError: + return [''] + + def _configure_motors(self, motor): + self.stop() + creature = self.config.robot.creature.split('poppy-')[1] + f = open(self.config.poppy_configure.logfile,"wb") + check_call(['poppy-configure', creature, motor], stdout=f, stderr=f) + self.start() + + def _set_hostspot_configuration(self, ssid, passphrase, hide_hostname=0): + conf = "ssid=%s\npassphrase=%s\nhide_hostname=%s\n" % (ssid, passphrase, hide_hostname) + with open('/tmp/hotspot.conf', 'w') as f: + f.write(conf) + + os.system('sudo mv /tmp/hotspot.conf %s' % self.config.wifi.hostspot_conf_file) + call(['sudo', 'systemctl', 'restart', self.config.wifi.hostspot_service]) + + def _get_hostspot_configuration(self): + conf='' + with open(self.config.wifi.hostspot_conf_file, 'r') as f: + conf = f.read() + ssid=re.match('s/^ssid=\(.\+\)$/\1/p', conf) + passphrase = re.match('s/^passphrase=\(.\+\)$/\1/p', conf) + hide_hostname = re.match('s/^hide_hostname=\(.\+\)$/\1/p', conf) + return {'ssid'=ssid, 'passphrase'=passphrase, 'hide_hostname'=hide_hostname} + + def _disable_hostpot(self): + conf_path = self.config.wifi.hostspot_conf_file + if os.path.isfile(conf_path): + os.system("sudo mv %s %s" % (conf_path, os.path.join(conf_path, '.backup')) + call(['sudo', 'systemctl', 'restart', self.config.wifi.hostspot_service]) + + def _enable_hostspot(self): + conf_path = self.config.wifi.hostspot_conf_file + if os.path.isfile(os.path.join(conf_path, '.backup')): + cmd = "sudo mv %s %s" % (os.path.join(conf_path, '.backup'), conf_path) + call(['sudo', 'systemctl', 'restart', self.config.wifi.hostspot_service]) + + else: + self._set_hostspot_configuration(self.config.wifi.default_ssid, self.config.wifi.default_password) + + + def _disable_wifi_client(self): + pass + # os.system("sudo mv /etc/wpa-supplicant.conf") + + def _wifi_list(self): + pass + def _wifi connect(self, ssid, password): + pass + def shutdown(self): try: for m in self.get_motors(): @@ -106,6 +166,7 @@ def send_value(self, motor, register, value): r = requests.post(url.format(motor, register), json=value) return r + if __name__ == '__main__': import sys diff --git a/static/img/motor_one_by_one.jpg b/static/img/motor_one_by_one.jpg new file mode 100644 index 0000000..8e8a832 Binary files /dev/null and b/static/img/motor_one_by_one.jpg differ diff --git a/templates/logs.html b/templates/logs.html index 6a2df16..5915164 100644 --- a/templates/logs.html +++ b/templates/logs.html @@ -35,7 +35,7 @@
+ Out of factory motors come all with the same identifiant in their internal memory. To be able to differenciate each motor in the robot, we have to configure it, to change their identifiant.
+
+ To do so,
+ you have to plug ONE motor at a time. It is very important, otherwise, they will all have the same new identifiant and you won't be able to communicate with them.
+
+ No Poppy creature seems to be installed on the robot.
+ {% endif %} + +
+ {{ update_logs_content }}
+
+ Configure your motors when you assemble your robot for the first time.
+