def __init__(self, servo=False, motor_trim_factor=1.0): self.pi = pigpio.pi('localhost') self.pi.set_mode(PIN_PUSHBUTTON, pigpio.INPUT) self.pi.set_mode(PIN_ENCODER_LEFT, pigpio.INPUT) self.pi.set_mode(PIN_ENCODER_RIGHT, pigpio.INPUT) self._cb = dict() self._cb_last_tick = dict() self._cb_elapse = dict() self._servo = servo self._motor_trim_factor = motor_trim_factor if self._servo: self.motor_control = self._servo_motor else: self.motor_control = self._dc_motor self._cb1 = self.pi.callback(PIN_PUSHBUTTON, pigpio.EITHER_EDGE, coderbot_callback) self._cb2 = self.pi.callback(PIN_ENCODER_LEFT, pigpio.RISING_EDGE, coderbot_callback) self._cb3 = self.pi.callback(PIN_ENCODER_RIGHT, pigpio.RISING_EDGE, coderbot_callback) for pin in self._pin_out: self.pi.set_PWM_frequency(pin, PWM_FREQUENCY) self.pi.set_PWM_range(pin, PWM_RANGE) self.stop() self._is_moving = False self.sonar = [ sonar.Sonar(self.pi, PIN_SONAR_1_TRIGGER, PIN_SONAR_1_ECHO), sonar.Sonar(self.pi, PIN_SONAR_2_TRIGGER, PIN_SONAR_2_ECHO), sonar.Sonar(self.pi, PIN_SONAR_3_TRIGGER, PIN_SONAR_3_ECHO) ] self._encoder_cur_left = 0 self._encoder_cur_right = 0 self._encoder_target_left = -1 self._encoder_target_right = -1
def __init__(self, motor_trim_factor=1.0, encoder=True): try: self._mpu = mpu.AccelGyroMag() self.GPIOS = GPIO_CODERBOT_V_5() logging.info("MPU available") except: logging.info("MPU not available") self.GPIOS = GPIO_CODERBOT_V_4() self._pin_out = [ self.GPIOS.PIN_LEFT_FORWARD, self.GPIOS.PIN_RIGHT_FORWARD, self.GPIOS.PIN_LEFT_BACKWARD, self.GPIOS.PIN_RIGHT_BACKWARD, self.GPIOS.PIN_SERVO_1, self.GPIOS.PIN_SERVO_2 ] self.pi = pigpio.pi('localhost') self.pi.set_mode(self.GPIOS.PIN_PUSHBUTTON, pigpio.INPUT) self._cb = dict() self._cb_last_tick = dict() self._cb_elapse = dict() self._encoder = encoder self._motor_trim_factor = motor_trim_factor self._twin_motors_enc = WheelsAxel( self.pi, enable_pin=self.GPIOS.PIN_MOTOR_ENABLE, left_forward_pin=self.GPIOS.PIN_LEFT_FORWARD, left_backward_pin=self.GPIOS.PIN_LEFT_BACKWARD, left_encoder_feedback_pin_A=self.GPIOS.PIN_ENCODER_LEFT_A, left_encoder_feedback_pin_B=self.GPIOS.PIN_ENCODER_LEFT_B, right_forward_pin=self.GPIOS.PIN_RIGHT_FORWARD, right_backward_pin=self.GPIOS.PIN_RIGHT_BACKWARD, right_encoder_feedback_pin_A=self.GPIOS.PIN_ENCODER_RIGHT_A, right_encoder_feedback_pin_B=self.GPIOS.PIN_ENCODER_RIGHT_B) self.motor_control = self._dc_enc_motor self._cb1 = self.pi.callback(self.GPIOS.PIN_PUSHBUTTON, pigpio.EITHER_EDGE, self._cb_button) for pin in self._pin_out: self.pi.set_PWM_frequency(pin, PWM_FREQUENCY) self.pi.set_PWM_range(pin, PWM_RANGE) self.sonar = [ sonar.Sonar(self.pi, self.GPIOS.PIN_SONAR_1_TRIGGER, self.GPIOS.PIN_SONAR_1_ECHO), sonar.Sonar(self.pi, self.GPIOS.PIN_SONAR_2_TRIGGER, self.GPIOS.PIN_SONAR_2_ECHO), sonar.Sonar(self.pi, self.GPIOS.PIN_SONAR_3_TRIGGER, self.GPIOS.PIN_SONAR_3_ECHO), sonar.Sonar(self.pi, self.GPIOS.PIN_SONAR_4_TRIGGER, self.GPIOS.PIN_SONAR_4_ECHO) ] self._servos = [self.GPIOS.PIN_SERVO_1, self.GPIOS.PIN_SERVO_2] self.stop()
def test_part_two(self): "Test part two example of Sonar object" # 1. Create Sonar object from text myobj = sonar.Sonar(part2=True, text=aoc_01.from_text(PART_TWO_TEXT)) # 2. Check the part two result self.assertEqual(myobj.part_two(verbose=False), PART_TWO_RESULT)
def test_part_one(self): "Test part one example of Sonar object" # 1. Create Sonar object from text myobj = sonar.Sonar(text=aoc_01.from_text(PART_ONE_TEXT)) # 2. Check the part one result self.assertEqual(myobj.part_one(verbose=False), PART_ONE_RESULT)
def test_empty_init(self): "Test the default Sonar creation" # 1. Create default Sonar object myobj = sonar.Sonar() # 2. Make sure it has the default values self.assertEqual(myobj.part2, False) self.assertEqual(myobj.text, None)
def __init__(self, servo=False, motor_trim_factor=1.0): self.pi = pigpio.pi('localhost') self.pi.set_mode(PIN_PUSHBUTTON, pigpio.INPUT) #self.pi.set_mode(PIN_ENCODER_LEFT, pigpio.INPUT) #self.pi.set_mode(PIN_ENCODER_RIGHT, pigpio.INPUT) self._cb = dict() self._cb_last_tick = dict() self._cb_elapse = dict() self._servo = servo self._motor_trim_factor = motor_trim_factor if self._servo: self.motor_control = self._servo_motor else: self.motor_control = self._dc_enc_motor self._cb1 = self.pi.callback(PIN_PUSHBUTTON, pigpio.EITHER_EDGE, self._cb_button) #self._cb2 = self.pi.callback(PIN_ENCODER_LEFT, pigpio.RISING_EDGE, self._cb_enc_left) #self._cb3 = self.pi.callback(PIN_ENCODER_RIGHT, pigpio.RISING_EDGE, self._cb_enc_right) for pin in self._pin_out: self.pi.set_PWM_frequency(pin, PWM_FREQUENCY) self.pi.set_PWM_range(pin, PWM_RANGE) self.sonar = [ sonar.Sonar(self.pi, PIN_SONAR_1_TRIGGER, PIN_SONAR_1_ECHO), sonar.Sonar(self.pi, PIN_SONAR_2_TRIGGER, PIN_SONAR_2_ECHO), sonar.Sonar(self.pi, PIN_SONAR_3_TRIGGER, PIN_SONAR_3_ECHO) ] self._twin_motors_enc = self.TwinMotorsEncoder( self.pi, pin_enable=PIN_MOTOR_ENABLE, pin_forward_left=PIN_LEFT_FORWARD, pin_backward_left=PIN_LEFT_BACKWARD, pin_encoder_left=PIN_ENCODER_LEFT, pin_forward_right=PIN_RIGHT_FORWARD, pin_backward_right=PIN_RIGHT_BACKWARD, pin_encoder_right=PIN_ENCODER_RIGHT) try: self._ag = mpu.AccelGyro() except IOError: logging.info("MPU not available") self.stop() self._is_moving = False
def __init__(self, n_sensor, SENSOR_FOV, SENSOR_MAX_R, robot_co): self.sonar_list = [] self.need_diversion_flag = False self.n_sensor = n_sensor i_pos = list(range(1, int(n_sensor/2) + 1)) i_neg = list(range(int(-n_sensor/2) , 0)) i_pos.reverse() i_neg.reverse() i_pos.extend(i_neg) for i in i_pos:#create a list of individual sonars... self.sonar_list.append(sonar.Sonar(i, SENSOR_FOV, SENSOR_MAX_R, robot_co))
def talker(): pub = rospy.Publisher('chatter', Int32, queue_size=10) rospy.init_node('talker', anonymous=True) rate = rospy.Rate(10) # 10hz sonar_ss = sonar.Sonar(24, 23) while not rospy.is_shutdown(): hello_str = sonar_ss.get_range() rospy.loginfo(hello_str) pub.publish(hello_str) rate.sleep()
def test_text_init(self): "Test the Sonar object creation from text" # 1. Create Sonar object from text myobj = sonar.Sonar(text=aoc_01.from_text(EXAMPLE_TEXT)) # 2. Make sure it has the expected values self.assertEqual(myobj.part2, False) self.assertEqual(len(myobj.text), 10) # 3. Check methods self.assertEqual(myobj.more_than_previous(), 7) self.assertEqual(myobj.do_windows(window=1), 7) self.assertEqual(myobj.do_windows(), 5)
def part_two(args, input_lines): "Process part two of the puzzle" # 1. Create the puzzle solver solver = sonar.Sonar(part2=True, text=input_lines) # 2. Determine the solution for part two solution = solver.part_two(verbose=args.verbose, limit=args.limit) if solution is None: print("There is no solution") else: print("The solution for part two is %s" % (solution)) # 3. Return result return solution is not None
import sonar from pandas import read_csv sn = sonar.Sonar() class TestObject(object): def test_test(self): assert 1 == 1, "Something is wrong with the test" def test_data_loading(self): data = sn.data url = 'sonar/sonar.all-data.csv' test_data = read_csv(url, header=None) assert str(test_data) == str(data), "Data didn't load correctly" def test_validation(self): x = sn.x assert x.size > 0, "self.x is empty" x_t = sn.x_train assert x_t.size > 0, "self.x_train is empty" x_v = sn.x_validation assert x_v.size > 0, "self.x_validation is empty" y = sn.y assert y.size > 0, "self.y is empty" y_t = sn.y_train assert y_t.size > 0, "self.y_train is empty" y_v = sn.y_validation assert y_v.size > 0, "self.y_validation is empty"