def __init__(self, gps=False, servo_port=SERVO_PORT): # devices self._gps = gps self.windsensor = WindSensor(I2C(WINDSENSOR_I2C_ADDRESS)) self.compass = Compass(I2C(COMPASS_I2C_ADDRESS), I2C(ACCELEROMETER_I2C_ADDRESS)) self.red_led = GpioWriter(17, os) self.green_led = GpioWriter(18, os) # Navigation self.globe = Globe() self.timer = Timer() self.application_logger = self._rotating_logger(APPLICATION_NAME) self.position_logger = self._rotating_logger("position") self.exchange = Exchange(self.application_logger) self.timeshift = TimeShift(self.exchange, self.timer.time) self.event_source = EventSource(self.exchange, self.timer, self.application_logger, CONFIG['event source']) self.sensors = Sensors(self.gps, self.windsensor, self.compass, self.timer.time, self.exchange, self.position_logger, CONFIG['sensors']) self.gps_console_writer = GpsConsoleWriter(self.gps) self.rudder_servo = Servo(serial.Serial(servo_port), RUDDER_SERVO_CHANNEL, RUDDER_MIN_PULSE, RUDDER_MIN_ANGLE, RUDDER_MAX_PULSE, RUDDER_MAX_ANGLE) self.steerer = Steerer(self.rudder_servo, self.application_logger, CONFIG['steerer']) self.helm = Helm(self.exchange, self.sensors, self.steerer, self.application_logger, CONFIG['helm']) self.course_steerer = CourseSteerer(self.sensors, self.helm, self.timer, CONFIG['course steerer']) self.navigator = Navigator(self.sensors, self.globe, self.exchange, self.application_logger, CONFIG['navigator']) self.self_test = SelfTest(self.red_led, self.green_led, self.timer, self.rudder_servo, RUDDER_MIN_ANGLE, RUDDER_MAX_ANGLE) # Tracking self.tracking_logger = self._rotating_logger("track") self.tracking_sensors = Sensors(self.gps, self.windsensor, self.compass, self.timer.time, self.exchange, self.tracking_logger, CONFIG['sensors']) self.tracker = Tracker(self.tracking_logger, self.tracking_sensors, self.timer)
def __init__(self,gps=False,servo_port=SERVO_PORT): # devices self._gps = gps self.windsensor = WindSensor(I2C(WINDSENSOR_I2C_ADDRESS)) self.compass = Compass(I2C(COMPASS_I2C_ADDRESS),I2C(ACCELEROMETER_I2C_ADDRESS)) self.red_led = GpioWriter(17,os) self.green_led = GpioWriter(18,os) # Navigation self.globe = Globe() self.timer = Timer() self.application_logger = self._rotating_logger(APPLICATION_NAME) self.position_logger = self._rotating_logger("position") self.exchange = Exchange(self.application_logger) self.timeshift = TimeShift(self.exchange,self.timer.time) self.event_source = EventSource(self.exchange,self.timer,self.application_logger,CONFIG['event source']) self.sensors = Sensors(self.gps,self.windsensor,self.compass,self.timer.time,self.exchange,self.position_logger,CONFIG['sensors']) self.gps_console_writer = GpsConsoleWriter(self.gps) self.rudder_servo = Servo(serial.Serial(servo_port),RUDDER_SERVO_CHANNEL,RUDDER_MIN_PULSE,RUDDER_MIN_ANGLE,RUDDER_MAX_PULSE,RUDDER_MAX_ANGLE) self.steerer = Steerer(self.rudder_servo,self.application_logger,CONFIG['steerer']) self.helm = Helm(self.exchange,self.sensors,self.steerer,self.application_logger,CONFIG['helm']) self.course_steerer = CourseSteerer(self.sensors,self.helm,self.timer,CONFIG['course steerer']) self.navigator = Navigator(self.sensors,self.globe,self.exchange,self.application_logger,CONFIG['navigator']) self.self_test = SelfTest(self.red_led,self.green_led,self.timer,self.rudder_servo,RUDDER_MIN_ANGLE,RUDDER_MAX_ANGLE) # Tracking self.tracking_logger = self._rotating_logger("track") self.tracking_sensors = Sensors(self.gps,self.windsensor,self.compass,self.timer.time,self.exchange,self.tracking_logger,CONFIG['sensors']) self.tracker = Tracker(self.tracking_logger,self.tracking_sensors,self.timer)
class TestSelfTest(unittest.TestCase): def setUp(self): self.red_led, self.green_led, self.timer, self.rudder = Mock(), Mock( ), Mock(), Mock() self.self_test = SelfTest(self.red_led, self.green_led, self.timer, self.rudder, RUDDER_MIN_ANGLE, RUDDER_MAX_ANGLE) def test_should_blink_red(self): self.self_test.run() self.assertEqual(1, self.red_led.high.call_count) self.assertEqual(1, self.red_led.low.call_count) self.timer.wait_for.assert_any_call(0.5) def test_should_waggle_the_rudder(self): self.rudder.reset_mock() self.self_test.run() self.rudder.set_position.assert_has_calls( [call(RUDDER_MIN_ANGLE), call(RUDDER_MAX_ANGLE), call(0)]) def test_should_signal_green_hopefully_at_the_end(self): self.self_test.run() self.assertEqual(1, self.green_led.high.call_count) self.assertEqual(1, self.green_led.low.call_count) self.timer.wait_for.assert_any_call(5)
class TestSelfTest(unittest.TestCase): def setUp(self): self.red_led,self.green_led,self.timer,self.rudder = Mock(),Mock(),Mock(),Mock() self.self_test = SelfTest(self.red_led,self.green_led,self.timer,self.rudder,RUDDER_MIN_ANGLE,RUDDER_MAX_ANGLE) def test_should_blink_red(self): self.self_test.run() self.assertEqual(1,self.red_led.high.call_count) self.assertEqual(1,self.red_led.low.call_count) self.timer.wait_for.assert_any_call(0.5) def test_should_waggle_the_rudder(self): self.rudder.reset_mock() self.self_test.run() self.rudder.set_position.assert_has_calls([call(RUDDER_MIN_ANGLE),call(RUDDER_MAX_ANGLE),call(0)]) def test_should_signal_green_hopefully_at_the_end(self): self.self_test.run() self.assertEqual(1,self.green_led.high.call_count) self.assertEqual(1,self.green_led.low.call_count) self.timer.wait_for.assert_any_call(5)
def setUp(self): self.red_led, self.green_led, self.timer, self.rudder = Mock(), Mock( ), Mock(), Mock() self.self_test = SelfTest(self.red_led, self.green_led, self.timer, self.rudder, RUDDER_MIN_ANGLE, RUDDER_MAX_ANGLE)
def setUp(self): self.red_led,self.green_led,self.timer,self.rudder = Mock(),Mock(),Mock(),Mock() self.self_test = SelfTest(self.red_led,self.green_led,self.timer,self.rudder,RUDDER_MIN_ANGLE,RUDDER_MAX_ANGLE)
class Wiring(): def __init__(self, gps=False, servo_port=SERVO_PORT): # devices self._gps = gps self.windsensor = WindSensor(I2C(WINDSENSOR_I2C_ADDRESS)) self.compass = Compass(I2C(COMPASS_I2C_ADDRESS), I2C(ACCELEROMETER_I2C_ADDRESS)) self.red_led = GpioWriter(17, os) self.green_led = GpioWriter(18, os) # Navigation self.globe = Globe() self.timer = Timer() self.application_logger = self._rotating_logger(APPLICATION_NAME) self.position_logger = self._rotating_logger("position") self.exchange = Exchange(self.application_logger) self.timeshift = TimeShift(self.exchange, self.timer.time) self.event_source = EventSource(self.exchange, self.timer, self.application_logger, CONFIG['event source']) self.sensors = Sensors(self.gps, self.windsensor, self.compass, self.timer.time, self.exchange, self.position_logger, CONFIG['sensors']) self.gps_console_writer = GpsConsoleWriter(self.gps) self.rudder_servo = Servo(serial.Serial(servo_port), RUDDER_SERVO_CHANNEL, RUDDER_MIN_PULSE, RUDDER_MIN_ANGLE, RUDDER_MAX_PULSE, RUDDER_MAX_ANGLE) self.steerer = Steerer(self.rudder_servo, self.application_logger, CONFIG['steerer']) self.helm = Helm(self.exchange, self.sensors, self.steerer, self.application_logger, CONFIG['helm']) self.course_steerer = CourseSteerer(self.sensors, self.helm, self.timer, CONFIG['course steerer']) self.navigator = Navigator(self.sensors, self.globe, self.exchange, self.application_logger, CONFIG['navigator']) self.self_test = SelfTest(self.red_led, self.green_led, self.timer, self.rudder_servo, RUDDER_MIN_ANGLE, RUDDER_MAX_ANGLE) # Tracking self.tracking_logger = self._rotating_logger("track") self.tracking_sensors = Sensors(self.gps, self.windsensor, self.compass, self.timer.time, self.exchange, self.tracking_logger, CONFIG['sensors']) self.tracker = Tracker(self.tracking_logger, self.tracking_sensors, self.timer) def _rotating_logger(self, appname): logHandler = TimedRotatingFileHandler("/var/log/pi-nav/" + appname, when="midnight", backupCount=30) logHandler.setFormatter(logging.Formatter(LOGGING_FORMAT)) logger = logging.getLogger(appname) logger.addHandler(logHandler) logger.setLevel(CONFIG['wiring']['logging level']) return logger @property def gps(self): if not self._gps: self._gps = GpsReader() self._gps.setDaemon(True) self._gps.start() return self._gps def showgps(self): try: self.timer.call(self.gps_console_writer.write).every(5) except (KeyboardInterrupt, SystemExit): self.gps.running = False self.gps.join() def follow(self, waypoints): self.application_logger.info( '**************************************************************') self.application_logger.info( '*** Pi-Nav starting navigation: ' + datetime.datetime.now().strftime("%Y-%m-%d")) self.application_logger.info( '**************************************************************') self.self_test.run() self.rudder_servo.set_position(0) self.follower = Follower(self.exchange, waypoints, self.application_logger) self.event_source.start() def track(self): self.self_test.run() self.tracker.track(10)
class Wiring(): def __init__(self,gps=False,servo_port=SERVO_PORT): # devices self._gps = gps self.windsensor = WindSensor(I2C(WINDSENSOR_I2C_ADDRESS)) self.compass = Compass(I2C(COMPASS_I2C_ADDRESS),I2C(ACCELEROMETER_I2C_ADDRESS)) self.red_led = GpioWriter(17,os) self.green_led = GpioWriter(18,os) # Navigation self.globe = Globe() self.timer = Timer() self.application_logger = self._rotating_logger(APPLICATION_NAME) self.position_logger = self._rotating_logger("position") self.exchange = Exchange(self.application_logger) self.timeshift = TimeShift(self.exchange,self.timer.time) self.event_source = EventSource(self.exchange,self.timer,self.application_logger,CONFIG['event source']) self.sensors = Sensors(self.gps,self.windsensor,self.compass,self.timer.time,self.exchange,self.position_logger,CONFIG['sensors']) self.gps_console_writer = GpsConsoleWriter(self.gps) self.rudder_servo = Servo(serial.Serial(servo_port),RUDDER_SERVO_CHANNEL,RUDDER_MIN_PULSE,RUDDER_MIN_ANGLE,RUDDER_MAX_PULSE,RUDDER_MAX_ANGLE) self.steerer = Steerer(self.rudder_servo,self.application_logger,CONFIG['steerer']) self.helm = Helm(self.exchange,self.sensors,self.steerer,self.application_logger,CONFIG['helm']) self.course_steerer = CourseSteerer(self.sensors,self.helm,self.timer,CONFIG['course steerer']) self.navigator = Navigator(self.sensors,self.globe,self.exchange,self.application_logger,CONFIG['navigator']) self.self_test = SelfTest(self.red_led,self.green_led,self.timer,self.rudder_servo,RUDDER_MIN_ANGLE,RUDDER_MAX_ANGLE) # Tracking self.tracking_logger = self._rotating_logger("track") self.tracking_sensors = Sensors(self.gps,self.windsensor,self.compass,self.timer.time,self.exchange,self.tracking_logger,CONFIG['sensors']) self.tracker = Tracker(self.tracking_logger,self.tracking_sensors,self.timer) def _rotating_logger(self,appname): logHandler = TimedRotatingFileHandler("/var/log/pi-nav/" + appname,when="midnight",backupCount=30) logHandler.setFormatter(logging.Formatter(LOGGING_FORMAT)) logger = logging.getLogger(appname) logger.addHandler( logHandler ) logger.setLevel( CONFIG['wiring']['logging level']) return logger @property def gps(self): if not self._gps: self._gps = GpsReader() self._gps.setDaemon(True) self._gps.start() return self._gps def showgps(self): try: self.timer.call(self.gps_console_writer.write).every(5) except (KeyboardInterrupt, SystemExit): self.gps.running = False self.gps.join() def follow(self,waypoints): self.application_logger.info('**************************************************************') self.application_logger.info('*** Pi-Nav starting navigation: ' + datetime.datetime.now().strftime("%Y-%m-%d")) self.application_logger.info('**************************************************************') self.self_test.run() self.rudder_servo.set_position(0) self.follower = Follower(self.exchange,waypoints,self.application_logger) self.event_source.start() def track(self): self.self_test.run() self.tracker.track(10)