def _parse_config(self, config): """ Parses config dictionary and registers peripherals. Args: config (dict): Dictionary of config information. """ self.actuators["left"] = config["actuators"][0] self.actuators["right"] = config["actuators"][1] self.motors = HBridge( HBridgeOut(self.actuators["left"]["pinPWM"], self.actuators["left"]["pinHighLow"], self.actuators["right"]["pinPWM"], self.actuators["right"]["pinHighLow"])) self.stop() #Starts wheels wheelEnabler = DigitalOutput(config["wheelEnablerPin"]) wheelEnabler.set_high() #Add color sensors for sensor in config["colorsensors"]: name = sensor["name"] pin = sensor["pin"] self.sensors[name] = ColorSensor(name, pin) #Add analog sensors for sensor in config["analogsensors"]: name = sensor["name"] pin = sensor["pin"] self.sensors[name] = AnalogSensor(name, pin)
def test_pivotleft(self): """ Tests to make sure turns left correctly """ hbridge = HBridge(DigitalOutput(10), PWM(13), DigitalOutput(24), PWM(18)) hbridge.set_speed(0, 1) self.assertEqual(hbridge.left_pin.state, 0) self.assertEqual(hbridge.left_pwm.duty_cycle, 1) self.assertEqual(hbridge.right_pin.state, 0) self.assertEqual(hbridge.right_pwm.duty_cycle, 0)
def test_backward(self): """ Tests to make sure moves backward correctly """ hbridge = HBridge(DigitalOutput(10), PWM(13), DigitalOutput(24), PWM(18)) hbridge.set_speed(-1, -1) self.assertEqual(hbridge.left_pin.state, 1) self.assertEqual(hbridge.left_pwm.duty_cycle, 1) self.assertEqual(hbridge.right_pin.state, 1) self.assertEqual(hbridge.right_pwm.duty_cycle, 1)
def test_initially_stopped(self): """ Test bot is in stopped state """ hbridge = HBridge(DigitalOutput(10), PWM(13), DigitalOutput(24), PWM(18)) hbridge.set_speed(0,0) self.assertEqual(hbridge.left_pin.state, 0) self.assertEqual(hbridge.left_pwm.duty_cycle, 1) self.assertEqual(hbridge.right_pin.state, 0) self.assertEqual(hbridge.right_pwm.duty_cycle, 1)
class Bot(): """ Minibot object class. Keeps track of the BotState (orientation, location, etc.) of the instance of MiniBot. """ def __init__(self, config): """ Constructor for minibot. Args: config (dict): Dictionary of config information for bot hardware. """ self.name = config['name'] self.state = BotState() self.sensors = {} self.actuators = {} self.motors = None self._parse_config(config) # queue for extra unrecognized commands by parser self.extraCMD = Queue() def _parse_config(self, config): """ Parses config dictionary and registers peripherals. Args: config (dict): Dictionary of config information. """ self.actuators["left"] = config["actuators"][0] self.actuators["right"] = config["actuators"][1] self.motors = HBridge( HBridgeOut(self.actuators["left"]["pinPWM"], self.actuators["left"]["pinHighLow"], self.actuators["right"]["pinPWM"], self.actuators["right"]["pinHighLow"])) self.stop() #Starts wheels wheelEnabler = DigitalOutput(config["wheelEnablerPin"]) wheelEnabler.set_high() #Add color sensors for sensor in config["colorsensors"]: name = sensor["name"] pin = sensor["pin"] self.sensors[name] = ColorSensor(name, pin) #Add analog sensors for sensor in config["analogsensors"]: name = sensor["name"] pin = sensor["pin"] self.sensors[name] = AnalogSensor(name, pin) def get_state(self): """ Gets the BotState of the minibot. Returns: BotState of the minibot. """ return self.state def stop(self): """ Moves the bot forward at a percentage of its full power :param power The percentage of the bot's power to use from 0-100 :return True if the action is supported """ self.motors.set_speed(0, 0) def move_forward(self, power): """ Moves the bot forward at a percentage of its full power :param power The percentage of the bot's power to use from 0-100 :return True if the action is supported """ self.motors.set_speed(power, power) def move_backward(self, power): """ Moves the bot backward at a percentage of its full power :param power The percentage of the bot's power to use from 0-100 :return True if the action is supported """ self.motors.set_speed(-power, -power) def turn_clockwise(self, power): """ Moves the bot clockwise at a percentage of its full power :param power The percentage of the bot's power to use from 0-100 :return True if the action is supported """ self.motors.set_speed(power, -power) def turn_counter_clockwise(self, power): """ Moves the bot counter-clockwise at a percentage of its full power :param power The percentage of the bot's power to use from 0-100 :return True if the action is supported """ self.motors.set_speed(-power, power) def set_wheel_power(self, left, right): """ Sets the power of the bot's wheels as a percentage from -100 to 100. If a wheel specified does not exist, the power for that wheel is ignored. :param front_left power to deliver to the front_left wheel :param front_right power to deliver to the front_right wheel :param back_left power to deliver to the back_left wheel :param back_right power to deliver to the back_right wheel :return True if the action is supported """ self.motors.set_speed(left, right) def wait(self, t): """ Waits for a duration in seconds. :param t The duration in seconds """ time.sleep(t) def register_actuator(self, actuator): """ Stores actuator object :param actuator object """ self.actuators[actuator.name] = actuator def register_sensor(self, sensor): """ Stores sensor object :param sensor object """ self.sensors[sensor.name] = sensor def get_actuator_by_name(self, name): """ Returns actuator object :param name name of the actuator """ return self.actuators[name] def get_sensor_by_name(self, name): """ Returns sensor object :param name name of the sensor """ return self.sensors[name] def get_all_actuators(self): """ Returns all actuators in a list """ return self.actuators.values() def get_all_sensors(self): """ Returns all sensors in a list """ return self.sensors.values() def get_motors(self): """ Returns motor object """ return self.motors def poll_sensors(self): """ Reads values from all sensors """ data = {} for sensor in self.sensors: data[sensor] = self.sensors[sensor].read() return data