Exemplo n.º 1
0
 def _run(self):
     # exec in a secure environment the coderbot
     coderbot = CoderBot()
     glbs = {
         '__name__': self._name,
         'program': self,
         'config': Config(),
         'coderbot': coderbot
     }
     if Config().get('program_video_rec', False):
         coderbot.camera.start_recording()
     try:
         exec(self._code, glbs)
     except:
         raise
     finally:
         coderbot.camera.stop_recording()
         coderbot.motors.stop()
         for sensor in coderbot.sensors.itervalues():
             try:
                 sensor._stop()
             except AttributeError:
                 pass
         self._running = False
         self._shutdown = False
Exemplo n.º 2
0
def run_server():
    global bot
    global cam
    global motion
    global audio
    try:
        try:
            app.bot_config = Config.read()
            bot = CoderBot.get_instance(
                servo=(app.bot_config.get("move_motor_mode") == "servo"),
                motor_trim_factor=float(app.bot_config.get("move_motor_trim", 1.0)),
            )
            audio = Audio.get_instance()
            audio.say(app.bot_config.get("sound_start"))
            try:
                cam = Camera.get_instance()
                motion = Motion.get_instance()
            except picamera.exc.PiCameraError:
                logging.error("Camera not present")

            if app.bot_config.get("load_at_start") and len(app.bot_config.get("load_at_start")):
                app.prog = app.prog_engine.load(app.bot_config.get("load_at_start"))
                app.prog.execute()
        except ValueError as e:
            app.bot_config = {}
            logging.error(e)

        bot.set_callback(PIN_PUSHBUTTON, button_pushed, 100)
        app.run(host="0.0.0.0", port=8080, debug=True, use_reloader=False, threaded=True)
    finally:
        if cam:
            cam.exit()
        if bot:
            bot.exit()
        app.shutdown_requested = True
Exemplo n.º 3
0
def run_server():
    bot = None
    cam = None
    try:
        try:
            app.bot_config = Config.read()
            bot = CoderBot.get_instance(motor_trim_factor=float(
                app.bot_config.get('move_motor_trim', 1.0)),
                                        encoder=bool(
                                            app.bot_config.get('encoder')))

            if app.bot_config.get('load_at_start') and app.bot_config.get(
                    'load_at_start'):
                app.prog = app.prog_engine.load(
                    app.bot_config.get('load_at_start'))
                app.prog.execute()
        except ValueError as e:
            app.bot_config = {}
            logging.error(e)

        bot.set_callback(bot.GPIOS.PIN_PUSHBUTTON, button_pushed, 100)

        remove_doreset_file()

        app.run(host="0.0.0.0",
                port=8080,
                debug=False,
                use_reloader=False,
                threaded=True)
    finally:
        if cam:
            cam.exit()
        if bot:
            bot.exit()
        app.shutdown_requested = True
Exemplo n.º 4
0
def run_server():
    global bot
    global cam
    global motion
    global audio
    global cnn
    global conv
    global event
    try:
        try:
            app.bot_config = Config.read()
            bot = CoderBot.get_instance(
                servo=(app.bot_config.get("move_motor_mode") == "servo"),
                motor_trim_factor=float(
                    app.bot_config.get('move_motor_trim', 1.0)))
            audio = Audio.get_instance()
            audio.say(app.bot_config.get("sound_start"))
            try:
                cam = Camera.get_instance()
                motion = Motion.get_instance()
            except picamera.exc.PiCameraError:
                logging.error("Camera not present")

            cnn = CNNManager.get_instance()
            event = EventManager.get_instance("coderbot")
            conv = Conversation.get_instance()

            if app.bot_config.get('load_at_start') and len(
                    app.bot_config.get('load_at_start')):
                app.prog = app.prog_engine.load(
                    app.bot_config.get('load_at_start'))
                app.prog.execute()
        except ValueError as e:
            app.bot_config = {}
            logging.error(e)

        bot.set_callback(PIN_PUSHBUTTON, button_pushed, 100)
        app.run(host="0.0.0.0",
                port=8080,
                debug=True,
                use_reloader=False,
                threaded=True)
    finally:
        if cam:
            cam.exit()
        if bot:
            bot.exit()
        app.shutdown_requested = True
Exemplo n.º 5
0
    def __init__(self):
        self.bot = CoderBot.get_instance()
        self.cam = Camera.get_instance()
        self.track_len = 2
        self.detect_interval = 5
        self.tracks = []
	self.frame_idx = 0
        self.ts = time()
        self.frame_gray = None
        self.prev_gray = None
        self.running = False
        self.delta_power = 0.0
        self.delta_dist = 0.0
        self.target_dist = 0.0
        self.delta_angle = 0.0
        self.target_angle = 0.0
        cfg = Config.get()
        self.power_angles = [[15, (int(cfg.get("move_power_angle_1")), -1)], [4, (int(cfg.get("move_power_angle_2")), 0.05)], [1, (int(cfg.get("move_power_angle_3")), 0.02)], [0, (0, 0)]]
Exemplo n.º 6
0
def run_server():
  global bot
  global cam
  global motion
  try:
    app.bot_config = Config.read()
    bot = CoderBot.get_instance(servo=(app.bot_config.get("move_motor_mode")=="servo"))
    cam = Camera.get_instance()
    motion = Motion.get_instance()
  except ValueError as e:
    app.bot_config = {}
    logging.error(e)
  if app.bot_config.get('load_at_start') and len(app.bot_config.get('load_at_start')):
    app.prog = app.prog_engine.load(app.bot_config.get('load_at_start'))

  bot.set_callback(PIN_PUSHBUTTON, button_pushed, 100)
  bot.say(app.bot_config.get("sound_start"))
  app.run(host="0.0.0.0", port=8080, debug=True, use_reloader=False, threaded=True)
Exemplo n.º 7
0
    def __init__(self):
        self.bot = CoderBot.get_instance()
        self.cam = Camera.get_instance()
        self.track_len = 2
        self.detect_interval = 5
        self.tracks = []
	self.frame_idx = 0
        self.ts = time()
        self.frame_gray = None
        self.prev_gray = None
        self.running = False
        self.delta_power = 0.0
        self.delta_dist = 0.0
        self.target_dist = 0.0
        self.delta_angle = 0.0
        self.target_angle = 0.0
        cfg = Config.get()
        self.power_angles = [[15, (int(cfg.get("move_power_angle_1")), -1)], [4, (int(cfg.get("move_power_angle_2")), 0.05)], [1, (int(cfg.get("move_power_angle_3")), 0.02)], [0, (0, 0)]]
Exemplo n.º 8
0
def run_server(prog=None):
    app.shutdown_requested = False
    Config().load(DEFAULT_CONFIG_PATH)
    app.bot = CoderBot()
    app.bot.camera.setDCIMpath(Config().get('record_path', RECORD_PATH))

    # TODO : Launch the Config().get('program_launch_at_start', None) program
    if prog is not None:
        prog()

    try:
        app.run(host='0.0.0.0',
                port=Config().get('listen_port', 8080),
                use_reloader=False,
                threaded=True)
    except:
        raise
    finally:
        app.shutdown_requested = True
        app.bot.shutdown()
Exemplo n.º 9
0
def run_server():
    bot = None
    cam = None
    try:
        try:
            app.bot_config = Config.read()
            bot = CoderBot.get_instance(servo=(app.bot_config.get("move_motor_mode") == "servo"),
                                        motor_trim_factor=float(app.bot_config.get('move_motor_trim', 1.0)))
            audio = Audio.get_instance()
            audio.say(app.bot_config.get("sound_start"))
            try:
                cam = Camera.get_instance()
                Motion.get_instance()
            except picamera.exc.PiCameraError:
                logging.error("Camera not present")

            CNNManager.get_instance()
            EventManager.get_instance("coderbot")

            if app.bot_config.get('load_at_start') and app.bot_config.get('load_at_start'):
                app.prog = app.prog_engine.load(app.bot_config.get('load_at_start'))
                app.prog.execute()
        except ValueError as e:
            app.bot_config = {}
            logging.error(e)

        bot.set_callback(PIN_PUSHBUTTON, button_pushed, 100)

        remove_doreset_file()

        app.run(host="0.0.0.0", port=5000, debug=True, use_reloader=False, threaded=True)
    finally:
        if cam:
            cam.exit()
        if bot:
            bot.exit()
        app.shutdown_requested = True
Exemplo n.º 10
0
def run_server():
    bot = None
    cam = None
    try:
        try:
            app.bot_config = Config.read()
            bot = CoderBot.get_instance(motor_trim_factor=float(app.bot_config.get('move_motor_trim', 1.0)),
                                        encoder=bool(app.bot_config.get('encoder')))
            audio = Audio.get_instance()
            audio.say(app.bot_config.get("sound_start"))
            try:
                cam = Camera.get_instance()
                Motion.get_instance()
            except picamera.exc.PiCameraError:
                logging.error("Camera not present")

            CNNManager.get_instance()
            EventManager.get_instance("coderbot")

            if app.bot_config.get('load_at_start') and app.bot_config.get('load_at_start'):
                app.prog = app.prog_engine.load(app.bot_config.get('load_at_start'))
                app.prog.execute()
        except ValueError as e:
            app.bot_config = {}
            logging.error(e)

        bot.set_callback(bot.GPIOS.PIN_PUSHBUTTON, button_pushed, 100)

        remove_doreset_file()

        app.run(host="0.0.0.0", port=5000, debug=False, use_reloader=False, threaded=True)
    finally:
        if cam:
            cam.exit()
        if bot:
            bot.exit()
        app.shutdown_requested = True
Exemplo n.º 11
0
def run_server():
    global bot
    global cam
    global motion
    try:
        app.bot_config = Config.read()
        bot = CoderBot.get_instance(
            servo=(app.bot_config.get("move_motor_mode") == "servo"))
        cam = Camera.get_instance()
        motion = Motion.get_instance()
    except ValueError as e:
        app.bot_config = {}
        logging.error(e)
    if app.bot_config.get('load_at_start') and len(
            app.bot_config.get('load_at_start')):
        app.prog = app.prog_engine.load(app.bot_config.get('load_at_start'))

    bot.set_callback(PIN_PUSHBUTTON, button_pushed, 100)
    bot.say(app.bot_config.get("sound_start"))
    app.run(host="0.0.0.0",
            port=8080,
            debug=True,
            use_reloader=False,
            threaded=True)
Exemplo n.º 12
0
from coderbot import CoderBot
import sys

DISTANCE = float(sys.argv[1])

c = CoderBot()
c.move(speed=70, distance=DISTANCE)
Exemplo n.º 13
0
"""
    This file defines base tests for CoderBot
    in order to test its functionality

    The function run_test(varargin) lanuches tests
    according to required test from the front-end.
    e.g. varagrin = ["motor_test", "sonar_test"]
    __test_encoder() and __test_sonar() will be launched.

    If something goes wrong a -1 is returned for the correspondent failed test.
    If a test passes for correspondent component, a 1 is returned.
    If no test was executed on that component, 0 is preserved.
"""
from coderbot import CoderBot
c = CoderBot.get_instance()

# Single components tests

# encoder motors
def __test_encoder():
    try:
        # moving both wheels at speed 100 clockwise
        print("moving both wheels at speed 100 clockwise")
        assert(c.speed() == 0)
        c.move(speed=100, elapse=2)
        assert(c.distance() != 0)
        assert (c.speed() == 0)

        # moving both wheels at speed 40 clockwise
        print("moving both wheels at speed 40 clockwise")
        assert(c.speed() == 0)
Exemplo n.º 14
0
from coderbot import CoderBot
import time

bot = CoderBot.get_instance()

for x in range(1,100):
  a = bot.get_sonar_distance(0)
  time.sleep(0.003)
  b = bot.get_sonar_distance(1)
  print "sonar_1: " + str(a) + " sonar_2: " + str(b)
  time.sleep(0.1)

Exemplo n.º 15
0
"""

import os
import subprocess
import json
import connexion
from tinydb import TinyDB, Query
from tinydb.operations import delete
from cachetools import cached, TTLCache
from coderbot import CoderBot
from program import ProgramEngine, Program
from config import Config

bot_config = Config.get()
bot = CoderBot.get_instance(
    servo=(bot_config.get("move_motor_mode") == "servo"),
    motor_trim_factor=float(bot_config.get("move_motor_trim", 1.0)),
)


def get_serial():
    """
    Extract serial from cpuinfo file
    """
    cpuserial = "0000000000000000"
    try:
        f = open('/proc/cpuinfo', 'r')
        for line in f:
            if line[0:6] == 'Serial':
                cpuserial = line[10:26]
        f.close()
    except Exception:
Exemplo n.º 16
0
def handle_bot():
    """
    Execute a bot command
    """
    bot = CoderBot.get_instance()
    cam = Camera.get_instance()
    motion = Motion.get_instance()
    audio = Audio.get_instance()

    cmd = request.args.get('cmd')
    param1 = request.args.get('param1')
    param2 = request.args.get('param2')
    print('/bot', json.dumps(request.args))
    if cmd == "move":
        bot.move(speed=int(param1), elapse=float(param2))
    elif cmd == "turn":
        bot.turn(speed=int(param1), elapse=float(param2))
    elif cmd == "move_motion":
        motion.move(dist=float(param2))
    elif cmd == "turn_motion":
        motion.turn(angle=float(param2))
    elif cmd == "stop":
        bot.stop()
        try:
            motion.stop()
        except Exception:
            logging.warning("Camera not present")

    elif cmd == "take_photo":
        try:
            cam.photo_take()
            audio.say(app.bot_config.get("sound_shutter"))
        except Exception:
            logging.warning("Camera not present")

    elif cmd == "video_rec":
        try:
            cam.video_rec()
            audio.say(app.bot_config.get("sound_shutter"))
        except Exception:
            logging.warning("Camera not present")

    elif cmd == "video_stop":
        try:
            cam.video_stop()
            audio.say(app.bot_config.get("sound_shutter"))
        except Exception:
            logging.warning("Camera not present")

    elif cmd == "say":
        logging.info("say: " + str(param1) + " in: " + str(get_locale()))
        audio.say(param1, get_locale())

    elif cmd == "halt":
        logging.info("shutting down")
        audio.say(app.bot_config.get("sound_stop"))
        bot.halt()

    elif cmd == "restart":
        logging.info("restarting bot")
        bot.restart()
    elif cmd == "reboot":
        logging.info("rebooting")
        bot.reboot()
    return "ok"
Exemplo n.º 17
0
import json
import connexion
from tinydb import TinyDB, Query
from tinydb.operations import delete
from cachetools import cached, TTLCache
from coderbot import CoderBot
from program import ProgramEngine, Program
from config import Config
from coderbotTestUnit import run_test as runCoderbotTestUnit
import pigpio

BUTTON_PIN = 16

bot_config = Config.get()
bot = CoderBot.get_instance(motor_trim_factor=float(
    bot_config.get("move_motor_trim", 1.0)),
                            encoder=bool(bot_config.get("encoder")))

query = Query()


def get_serial():
    """
    Extract serial from cpuinfo file
    """
    cpuserial = "0000000000000000"
    try:
        f = open('/proc/cpuinfo', 'r')
        for line in f:
            if line[0:6] == 'Serial':
                cpuserial = line[10:26]
Exemplo n.º 18
0
def handle_bot():
    """
    Execute a bot command
    """
    bot = CoderBot.get_instance()
    cam = Camera.get_instance()
    motion = Motion.get_instance()
    audio = Audio.get_instance()

    cmd = request.args.get('cmd')
    param1 = request.args.get('param1')
    param2 = request.args.get('param2')
    print('/bot', json.dumps(request.args))
    if cmd == "move":
        bot.move(speed=int(param1), elapse=float(param2))
    elif cmd == "turn":
        bot.turn(speed=int(param1), elapse=float(param2))
    elif cmd == "move_motion":
        motion.move(dist=float(param2))
    elif cmd == "turn_motion":
        motion.turn(angle=float(param2))
    elif cmd == "stop":
        bot.stop()
        try:
            motion.stop()
        except Exception:
            logging.warning("Camera not present")

    elif cmd == "take_photo":
        try:
            cam.photo_take()
            audio.say(app.bot_config.get("sound_shutter"))
        except Exception:
            logging.warning("Camera not present")

    elif cmd == "video_rec":
        try:
            cam.video_rec()
            audio.say(app.bot_config.get("sound_shutter"))
        except Exception:
            logging.warning("Camera not present")

    elif cmd == "video_stop":
        try:
            cam.video_stop()
            audio.say(app.bot_config.get("sound_shutter"))
        except Exception:
            logging.warning("Camera not present")

    elif cmd == "say":
        logging.info("say: " + str(param1) + " in: " + str(get_locale()))
        audio.say(param1, get_locale())

    elif cmd == "halt":
        logging.info("shutting down")
        audio.say(app.bot_config.get("sound_stop"))
        bot.halt()

    elif cmd == "restart":
        logging.info("restarting bot")
        bot.restart()
    elif cmd == "reboot":
        logging.info("rebooting")
        bot.reboot()
    return "ok"