def run(self): self.db = QtSql.QSqlDatabase.addDatabase('QMYSQL') self.db.setHostName("3.34.124.67") self.db.setDatabaseName("16_3") self.db.setUserName("16_3") self.db.setPassword("1234") ok = self.db.open() print(ok) if ok == False: return self.mh = Raspi_MotorHAT(addr=0x6f) self.dcMotor = self.mh.getMotor(2) self.speed = 100 self.adjust = 0 self.moveTime = 0 self.dcMotor.setSpeed(self.speed) self.query = QtSql.QSqlQuery() self.servo = self.mh._pwm self.servo.setPWMFreq(60) self.sense = SenseHat() gyro = self.sense.get_gyroscope() self.prev_roll = gyro["roll"] accel = self.sense.get_accelerometer_raw() self.init_y = accel["y"] while True: time.sleep(0.1) self.getQuery() self.setQuery()
class Robot(object): def __init__(self, motorhat_addr=0x6f): self._mh = Raspi_MotorHAT(addr=motorhat_addr) self.left_motor = self._mh.getMotor(1) self.right_motor = self._mh.getMotor(2) atexit.register(self.stop_motors) def convert_speed(self, speed): mode = Raspi_MotorHAT.RELEASE if speed > 0: mode = Raspi_MotorHAT.FORWARD elif speed < 0: mode = Raspi_MotorHAT.BACKWARD output_speed = (abs(speed) * 255) / 100 return mode, output_speed def set_left(self, speed): mode, output_speed = self.convert_speed(speed) self.left_motor.setSpeed(output_speed) self.left_motor.run(mode) def set_right(self, speed): mode, output_speed = self.convert_speed(speed) self.right_motor.setSpeed(output_speed) self.right_motor.run(mode) def stop_motors(self): self.left_motor.run(Raspi_MotorHAT.RELEASE) self.right_motor.run(Raspi_MotorHAT.RELEASE)
def __init__(self, motorhat_addr=0x6f, drive_enabled=True): # Setup the motorhat with the passed in address self._mh = Raspi_MotorHAT(addr=motorhat_addr) # get local variable for each motor self.left_motor = self._mh.getMotor(1) self.right_motor = self._mh.getMotor(2) self.drive_enabled = drive_enabled # ensure the motors get stopped when the code exits atexit.register(self.stop_all) # Setup the line sensors self.left_line_sensor = LineSensor(23, queue_len=3, pull_up=True) self.right_line_sensor = LineSensor(16, queue_len=3, pull_up=True) # Setup The Distance Sensors self.left_distance_sensor = DistanceSensor(17, 27) self.right_distance_sensor = DistanceSensor(5, 6) # Setup the Encoders EncoderCounter.set_constants(self.wheel_diameter_mm, self.ticks_per_revolution) self.left_encoder = EncoderCounter(4) self.right_encoder = EncoderCounter(26) # Setup the Leds self.leds = leds_8_apa102c.Leds() # Set up servo motors for pan and tilt. self.servos = Servos(addr=motorhat_addr)
def __init__(self, addr=0x6f, left_id=2, right_id=3, left_trim=0, right_trim=0, stop_at_exit=True): """Create an instance of the robot. Can specify the following optional parameters: - addr: The I2C address of the motor HAT, default is 0x6f. - left_id: The ID of the left motor, default is 3. - right_id: The ID of the right motor, default is 2. - left_trim: Amount to offset the speed of the left motor, can be positive or negative and use useful for matching the speed of both motors. Default is 0. - right_trim: Amount to offset the speed of the right motor (see above). - stop_at_exit: Boolean to indicate if the motors should stop on program exit. Default is True (highly recommended to keep this value to prevent damage to the bot on program crash!). """ # Initialize motor HAT and left, right motor. self._mh = Raspi_MotorHAT(addr) self._left = self._mh.getMotor(left_id) self._right = self._mh.getMotor(right_id) self._left_trim = int(left_trim) self._right_trim = int(right_trim) # Start with motors turned off. self._left.run(Raspi_MotorHAT.RELEASE) self._right.run(Raspi_MotorHAT.RELEASE) # Configure all motors to stop at program exit if desired. if stop_at_exit: atexit.register(self.stop)
def __init__(self, motorhat_addr=0x6f): # Setup the motorhat with the passed in address self._mh = Raspi_MotorHAT(addr=motorhat_addr) self.left_motor = self._mh.getMotor(1) self.right_motor = self._mh.getMotor(2) # Setup the Leds self.leds = leds_led_shim.Leds() # Set up servo motors for pan and tilt. self.servos = Servos(addr=motorhat_addr, deflect_90_in_ms=1) # Setup The Distance Sensors self.left_distance_sensor = DistanceSensor(echo=17, trigger=27, queue_len=2) self.right_distance_sensor = DistanceSensor(echo=5, trigger=6, queue_len=2) # Setup the Encoders EncoderCounter.set_constants(self.wheel_diameter_mm, self.ticks_per_revolution) self.left_encoder = EncoderCounter(4) self.right_encoder = EncoderCounter(26) # ensure the motors get stopped when the code exits atexit.register(self.stop_all)
def __init__(self, motorhat_addr=0x6f): self._mh = Raspi_MotorHAT(addr=motorhat_addr) self.left_motor = self._mh.getMotor(1) self.right_motor = self._mh.getMotor(2) atexit.register(self.stop_all) self.left_line_sensor = LineSensor(23, pull_up=True) self.right_line_sensor = LineSensor(16, pull_up=True)
def __init__(self,**kwage): """ :address :SetDefult Address to x60 """ self.ADDRESS=kwage["address"] self.mh=Raspi_MotorHAT(self.ADDRESS) self.mh1=Raspi_MotorHAT(self.ADDRESS) atexit.register(self.set_Motor2zero)
def __init__(self, motorhat_addr=0x6f): # Setup the motorhat with the passed in address self._mh = Raspi_MotorHAT(addr=motorhat_addr) # get local variable for each motor self.left_motor = self._mh.getMotor(1) self.right_motor = self._mh.getMotor(2) # ensure the motors get stopped when the code exits atexit.register(self.stop_motors)
class Robot(object): def __init__(self, motorhat_addr=0x6f): # Setup the motorhat with the passed in address self._mh = Raspi_MotorHAT(addr=motorhat_addr) # get local variable for each motor self.left_motor = self._mh.getMotor(1) self.right_motor = self._mh.getMotor(2) # ensure the motors get stopped when the code exits atexit.register(self.stop_all) # Setup the line sensors self.left_line_sensor = LineSensor(23, queue_len=3, pull_up=True) self.right_line_sensor = LineSensor(16, queue_len=3, pull_up=True) # Setup the Leds self.leds = leds_8_apa102c.Leds() def stop_all(self): self.stop_motors() # Clear any sensor handlers self.left_line_sensor.when_line = None self.left_line_sensor.when_no_line = None self.right_line_sensor.when_line = None self.right_line_sensor.when_no_line = None # Clear the display self.leds.clear() self.leds.show() def convert_speed(self, speed): mode = Raspi_MotorHAT.RELEASE if speed > 0: mode = Raspi_MotorHAT.FORWARD elif speed < 0: mode = Raspi_MotorHAT.BACKWARD output_speed = (abs(speed) * 255) / 100 return mode, int(output_speed) def set_left(self, speed): mode, output_speed = self.convert_speed(speed) self.left_motor.setSpeed(output_speed) self.left_motor.run(mode) def set_right(self, speed): mode, output_speed = self.convert_speed(speed) self.right_motor.setSpeed(output_speed) self.right_motor.run(mode) def stop_motors(self): self.left_motor.run(Raspi_MotorHAT.RELEASE) self.right_motor.run(Raspi_MotorHAT.RELEASE)
def run(self): db = QtSql.QSqlDatabase.addDatabase('QMYSQL') db.setHostName("13.209.64.111") db.setDatabaseName("IoT_data") db.setUserName("ujin") db.setPassword("ujin") ok = db.open() print(ok) self.mh = Raspi_MotorHAT(addr=0x6f) self.myMotor = self.mh.getMotor(2) self.myMotor.setSpeed(100) self.speed_value = 100 self.pwm = PWM(0x6F) self.pwm.setPWMFreq(60) #self.sense=SenseHat() self.BUZZER = 5 self.RIGHT_LED_F = 23 self.LEFT_LED_F = 24 self.RIGHT_LED_R = 8 self.LEFT_LED_R = 7 self.ECHO = 21 self.TRIG = 20 self.timeF = 0 self.camera = picamera.PiCamera() GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(self.BUZZER, GPIO.OUT) GPIO.setup(self.RIGHT_LED_F, GPIO.OUT) GPIO.setup(self.LEFT_LED_F, GPIO.OUT) GPIO.setup(self.RIGHT_LED_R, GPIO.OUT) GPIO.setup(self.LEFT_LED_R, GPIO.OUT) GPIO.setup(self.TRIG, GPIO.OUT) GPIO.setup(self.ECHO, GPIO.IN) while True: if (self.timeF == 10): now = datetime.datetime.now() self.camera.capture('./img/' + now.strftime('%Y%m%d%H%M%S') + '.jpg') self.timeF = 0 time.sleep(0.1) self.getQuery() # self.setQuery() self.timeF += 1
def __init__(self, motorhat_addr=0x6f): # Setup the motorhat with the passed in address self._mh = Raspi_MotorHAT(addr=motorhat_addr) # get local variable for each motor self.left_motor = self._mh.getMotor(1) self.right_motor = self._mh.getMotor(2) # ensure the motors get stopped when the code exits atexit.register(self.stop_all) # Setup the line sensors self.left_line_sensor = LineSensor(23, queue_len=3, pull_up=True) self.right_line_sensor = LineSensor(16, queue_len=3, pull_up=True)
class Robot: def __init__(self, motorhat_addr=0x6f): # Setup the motorhat with the passed in address self._mh = Raspi_MotorHAT(addr=motorhat_addr) # get local variable for each motor self.left_motor = self._mh.getMotor(1) self.right_motor = self._mh.getMotor(2) # Setup The Distance Sensors self.left_distance_sensor = DistanceSensor(echo=17, trigger=27, queue_len=2) self.right_distance_sensor = DistanceSensor(echo=5, trigger=6, queue_len=2) # Setup the Leds self.leds = leds_led_shim.Leds() # ensure the motors get stopped when the code exits atexit.register(self.stop_all) def convert_speed(self, speed): # Choose the running mode mode = Raspi_MotorHAT.RELEASE if speed > 0: mode = Raspi_MotorHAT.FORWARD elif speed < 0: mode = Raspi_MotorHAT.BACKWARD # Scale the speed output_speed = (abs(speed) * 255) // 100 return mode, int(output_speed) def set_left(self, speed): mode, output_speed = self.convert_speed(speed) self.left_motor.setSpeed(output_speed) self.left_motor.run(mode) def set_right(self, speed): mode, output_speed = self.convert_speed(speed) self.right_motor.setSpeed(output_speed) self.right_motor.run(mode) def stop_motors(self): self.left_motor.run(Raspi_MotorHAT.RELEASE) self.right_motor.run(Raspi_MotorHAT.RELEASE) def stop_all(self): self.stop_motors() # Clear the display self.leds.clear() self.leds.show()
def __init__(self, motorhat_addr=0x6f): # set up motorhat with address parameter self._mh = Raspi_MotorHAT(addr=motorhat_addr) # get local variable for each motor self.left_motor_rear = self._mh.getMotor(1) self.left_motor_front = self._mh.getMotor(2) self.right_motor_rear = self._mh.getMotor(4) self.right_motor_front = self._mh.getMotor(3) # make sure motors stop when code exits atexit.register(self.stop_motors) # Set up servo motors for pan and tilt self.servos = Servos(addr=motorhat_addr)
def __init__(self, motorhat_addr=0x6f): self._mh = Raspi_MotorHAT(addr=motorhat_addr) self.left_motor = self._mh.getMotor(1) self.right_motor = self._mh.getMotor(2) atexit.register(self.stop_motors) # Setup the line sensors self.right_line_sensor = LineSensor(23, queue_len=3, pull_up=True) self.left_line_sensor = LineSensor(16, queue_len=3, pull_up=True) self.right_line_sensor_stuck = "" self.left_line_sensor_stuck = "" self.servos = Servos(addr=motorhat_addr)
def __init__(self, motorhat_addr=0x6f): # Setup the motorhat with the passed in address self._mh = Raspi_MotorHAT(addr=motorhat_addr) # get local variable for each motor self.left_motor = self._mh.getMotor(1) self.right_motor = self._mh.getMotor(2) # Setup The Distance Sensors self.left_distance_sensor = DistanceSensor(echo=17, trigger=27, queue_len=2) self.right_distance_sensor = DistanceSensor(echo=5, trigger=6, queue_len=2) # Setup the Leds self.leds = leds_led_shim.Leds() # ensure the motors get stopped when the code exits atexit.register(self.stop_all)
class FireCanon(object): def __init__(self, addr=0x6f, right_id=4, stop_at_exit=True): """Create an instance of the canon. Can specify the following optional parameters: - addr: The I2C address of the motor HAT, default is 0x60. - right_id: The ID of the right motor, hard-coded to motor 4. - stop_at_exit: Boolean to indicate if the motors should stop on program exit. Default is True (highly recommended to keep this value to prevent damage to the bot on program crash!). This controls the plunger motor forward/backward motion that pushes the darts into the canon bore """ # Initialize motor HAT and left, right motor. self._mh = Raspi_MotorHAT(addr) self._right = self._mh.getMotor(right_id) # Start with motors turned off. self._right.run(Raspi_MotorHAT.RELEASE) # Configure all motors to stop at program exit if desired. if stop_at_exit: atexit.register(self.stop) # These motors will always run at full speed def _right_speed(self, speed=254): """Set the speed of the right motor, taking into account its trim offset. """ assert 0 <= speed <= 255, 'Speed must be a value between 0 to 255 inclusive!' speed += 0 # no trim for these motors speed = max(0, min(255, speed)) # Constrain speed to 0-255 after trimming. self._right.setSpeed(speed) def stop(self): """Stop all movement.""" self._right.run(Raspi_MotorHAT.RELEASE) def forward(self, speed=254, seconds=None): """Move forward at the specified speed (0-255). Will start moving forward and return unless a seconds value is specified, in which case the robot will move forward for that amount of time and then stop. """ # Set motor speed and move both forward. self._right_speed(speed) self._right.run(Raspi_MotorHAT.FORWARD) # If an amount of time is specified, move for that time and then stop. if seconds is not None: time.sleep(seconds) self.stop() def backward(self, speed=254, seconds=None): """Move backward at the specified speed (0-255). Will start moving backward and return unless a seconds value is specified, in which case the robot will move backward for that amount of time and then stop. """ # Set motor speed and move both backward. self._right_speed(speed) self._right.run(Raspi_MotorHAT.BACKWARD) # If an amount of time is specified, move for that time and then stop. if seconds is not None: time.sleep(seconds) self.stop()
def __init__(self, addr=0x6f, left_id=3, stop_at_exit=True): """Create an instance of the robot. Can specify the following optional parameters: - addr: The I2C address of the motor HAT, default is 0x60. - left_id: The ID of the left motor, hard-coded to motor 3. - stop_at_exit: Boolean to indicate if the motors should stop on program exit. Default is True (highly recommended to keep this value to prevent damage to the bot on program crash!). """ # Initialize motor HAT and left, right motor. self._mh = Raspi_MotorHAT(addr) self._left = self._mh.getMotor(left_id) # Start with motors turned off. self._left.run(Raspi_MotorHAT.RELEASE) # Configure all motors to stop at program exit if desired. if stop_at_exit: atexit.register(self.stop)
def __init__(self, addr=0x6f, right_id=4, stop_at_exit=True): """Create an instance of the canon. Can specify the following optional parameters: - addr: The I2C address of the motor HAT, default is 0x60. - right_id: The ID of the right motor, hard-coded to motor 4. - stop_at_exit: Boolean to indicate if the motors should stop on program exit. Default is True (highly recommended to keep this value to prevent damage to the bot on program crash!). This controls the plunger motor forward/backward motion that pushes the darts into the canon bore """ # Initialize motor HAT and left, right motor. self._mh = Raspi_MotorHAT(addr) self._right = self._mh.getMotor(right_id) # Start with motors turned off. self._right.run(Raspi_MotorHAT.RELEASE) # Configure all motors to stop at program exit if desired. if stop_at_exit: atexit.register(self.stop)
def run(self): self.db = QtSql.QSqlDatabase.addDatabase('QMYSQL') self.db.setHostName("3.34.124.67") self.db.setDatabaseName("16_3") self.db.setUserName("16_3") self.db.setPassword("1234") ok = self.db.open() print(ok) if ok == False : return self.mh = Raspi_MotorHAT(addr=0x6f) self.myMotor = self.mh.getMotor(1) self.pwm = PWM(0x6F) self.pwm.setPWMFreq(60) self.sense = SenseHat(1) while True: time.sleep(0.1) self.getQuery() self.setQuery()
class Robot(object): def __init__(self, motorhat_addr=0x6f): # Setup the motorhat with the passed in address self._mh = Raspi_MotorHAT(addr=motorhat_addr) # get local variable for each motor self.left_motor = self._mh.getMotor(1) self.right_motor = self._mh.getMotor(2) # ensure the motors get stopped when the code exits atexit.register(self.stop_motors) def convert_speed(self, speed): # Choose the running mode mode = Raspi_MotorHAT.RELEASE if speed > 0: mode = Raspi_MotorHAT.FORWARD elif speed < 0: mode = Raspi_MotorHAT.BACKWARD # Scale the speed output_speed = (abs(speed) * 255) / 100 return mode, int(output_speed) def set_left(self, speed): mode, output_speed = self.convert_speed(speed) self.left_motor.setSpeed(output_speed) self.left_motor.run(mode) def set_right(self, speed): mode, output_speed = self.convert_speed(speed) self.right_motor.setSpeed(output_speed) self.right_motor.run(mode) def stop_motors(self): self.left_motor.run(Raspi_MotorHAT.RELEASE) self.right_motor.run(Raspi_MotorHAT.RELEASE)
def __init__(self): # 모터 설정 self.mh = Raspi_MotorHAT(addr=0x6f) self.dcMotor = self.mh.getMotor(3) # M3단자에 모터 연결 self.speed = 125 # 기본 속도 0~255 self.dcMotor.setSpeed(self.speed) # 서보 설정 self.servo = self.mh._pwm self.servo.setPWMFreq(50) # init constant # jahong self.MIDPWM = 375 self.MINPWM = 140 self.MAXPWM = 450 #self.MAXANGLE = 60 # angle self.cur_pwm = self.MIDPWM self.servo.setPWM(0, 0, self.MIDPWM) # status self.status = "stop" print("car init")
from Raspi_MotorHAT import Raspi_MotorHAT import time servoMin= 70 servoMax= 120 mh = Raspi_MotorHAT(0x6F) # motor-HAT i2c주소 myMotor = mh.getMotor(3) myMotor.setSpeed(150) #myMotor.run() myMotor.run(Raspi_MotorHAT.FORWARD) servo = mh._pwm # pwm frequency 설정하기 서보컨트롤에서는 50Hz 혹은 60Hz가 일반적이다. (여기에서는 60Hz 사용.) servo.setPWMFreq(60) while(True): servo.setPWM(0, 4096, 0) time.sleep(1) servo.setPWM(0, 0, 4096) time.sleep(1)
from Raspi_MotorHAT import Raspi_MotorHAT #import keyboard mh = Raspi_MotorHAT(addr=0x6f) lmotor = mh.getMotor(1) rmotor = mh.getMotor(2) # lmotor.run(Raspi_MotorHAT.FORWARD) # loopend = True def driveBot(buttonInput): if buttonInput == "w": lmotor.run(Raspi_MotorHAT.BACKWARD) rmotor.run(Raspi_MotorHAT.FORWARD) lmotor.setSpeed(100) rmotor.setSpeed(100) elif buttonInput == "s": lmotor.run(Raspi_MotorHAT.FORWARD) rmotor.run(Raspi_MotorHAT.BACKWARD) lmotor.setSpeed(100) rmotor.setSpeed(100) elif buttonInput == "a": lmotor.run(Raspi_MotorHAT.BACKWARD) rmotor.run(Raspi_MotorHAT.BACKWARD) lmotor.setSpeed(100) rmotor.setSpeed(100) elif buttonInput == "d": lmotor.run(Raspi_MotorHAT.FORWARD) rmotor.run(Raspi_MotorHAT.FORWARD) lmotor.setSpeed(100)
# day2_car.py from Raspi_MotorHAT import Raspi_MotorHAT, Raspi_DCMotor from PyQt5 import QtSql from PyQt5.QtCore import * from sense_hat import sense_hat from time import sleep from pyowm import OWM owm = OWM("22f81e736830f16039f7dbf55aeec7f7") mgr = owm.weather_manager() obs = mgr.weather_at_place('Seoul') mh = Raspi_MotorHAT(addr=0x6f) dcMotor = mh.getMotor(3) speed = 80 dcMotor.setSpeed(speed) servo = mh._pwm servo.setPWMFreq(60) sense = sense_hat.SenseHat() w = [150, 150, 150] b = [0, 0, 255] e = [0, 0, 0] image = [] def go(): global image global sense image = [ e, e, e, b, e, e, e, e, e, e, e, b, e, e, e, e, e, e, e, b, e, e, e, e,
#!/usr/bin/python #import Raspi_MotorHAT, Raspi_DCMotor, Raspi_Stepper from Raspi_MotorHAT import Raspi_MotorHAT, Raspi_DCMotor, Raspi_StepperMotor import time import atexit # create a default object, no changes to I2C address or frequency mh = Raspi_MotorHAT(0x6F) # recommended for auto-disabling motors on shutdown! def turnOffMotors(): mh.getMotor(1).run(Raspi_MotorHAT.RELEASE) mh.getMotor(2).run(Raspi_MotorHAT.RELEASE) mh.getMotor(3).run(Raspi_MotorHAT.RELEASE) mh.getMotor(4).run(Raspi_MotorHAT.RELEASE) atexit.register(turnOffMotors) myStepper = mh.getStepper(200, 1) # 200 steps/rev, motor port #1 myStepper.setSpeed(30) # 30 RPM while (True): print("Single coil steps") myStepper.step(200, Raspi_MotorHAT.FORWARD, Raspi_MotorHAT.SINGLE) myStepper.step(200, Raspi_MotorHAT.FORWARD, Raspi_MotorHAT.SINGLE) myStepper.step(100, Raspi_MotorHAT.FORWARD, Raspi_MotorHAT.SINGLE) myStepper.step(200, Raspi_MotorHAT.BACKWARD, Raspi_MotorHAT.SINGLE) myStepper.step(100, Raspi_MotorHAT.FORWARD, Raspi_MotorHAT.SINGLE) myStepper.step(300, Raspi_MotorHAT.FORWARD, Raspi_MotorHAT.SINGLE) myStepper.step(100, Raspi_MotorHAT.BACKWARD, Raspi_MotorHAT.SINGLE)
class Robot(object): def __init__(self, addr=0x6f, left_id=2, right_id=3, left_trim=0, right_trim=0, stop_at_exit=True): """Create an instance of the robot. Can specify the following optional parameters: - addr: The I2C address of the motor HAT, default is 0x6f. - left_id: The ID of the left motor, default is 3. - right_id: The ID of the right motor, default is 2. - left_trim: Amount to offset the speed of the left motor, can be positive or negative and use useful for matching the speed of both motors. Default is 0. - right_trim: Amount to offset the speed of the right motor (see above). - stop_at_exit: Boolean to indicate if the motors should stop on program exit. Default is True (highly recommended to keep this value to prevent damage to the bot on program crash!). """ # Initialize motor HAT and left, right motor. self._mh = Raspi_MotorHAT(addr) self._left = self._mh.getMotor(left_id) self._right = self._mh.getMotor(right_id) self._left_trim = int(left_trim) self._right_trim = int(right_trim) # Start with motors turned off. self._left.run(Raspi_MotorHAT.RELEASE) self._right.run(Raspi_MotorHAT.RELEASE) # Configure all motors to stop at program exit if desired. if stop_at_exit: atexit.register(self.stop) def _left_speed(self, speed): """Set the speed of the left motor, taking into account its trim offset. """ assert 0 <= speed <= 255, 'Speed %d must be a value between 0 to 255 inclusive!' % speed speed += self._left_trim speed = max(0, min(255, speed)) # Constrain speed to 0-255 after trimming. self._left.setSpeed(speed) def _right_speed(self, speed): """Set the speed of the right motor, taking into account its trim offset. """ assert 0 <= speed <= 255, 'Speed %d must be a value between 0 to 255 inclusive!' % speed speed += self._right_trim speed = max(0, min(255, speed)) # Constrain speed to 0-255 after trimming. self._right.setSpeed(speed) def stop(self): """Stop all movement.""" self._left.run(Raspi_MotorHAT.RELEASE) self._right.run(Raspi_MotorHAT.RELEASE) def right(self, speed_percent): """Drive right motor at speed_percent""" # Set motor speed and move both forward. dir = Raspi_MotorHAT.FORWARD if speed_percent >= 0 else Raspi_MotorHAT.BACKWARD self._right.run(dir) self._right_speed(max(0, min(100, int(abs(speed_percent) * 255 / 100)))) def left(self, speed_percent): """Drive left motor at speed_percent""" # Set motor speed and move both forward. dir = Raspi_MotorHAT.FORWARD if speed_percent >= 0 else Raspi_MotorHAT.BACKWARD self._left.run(dir) self._left_speed(max(0, min(100, int(abs(speed_percent) * 255 / 100))))
SWmaster = 33 SW1 = 29 SW2 = 31 SW3 = 36 SW4 = 37 SW = [0, SW1, SW2, SW3, SW4] LED_list = [LED1r, LED1g, LED2r, LED2g, LED3r, LED3g, LED4r, LED4g] switch_list = [SWmaster, SW1, SW2, SW3, SW4] for i in LED_list: GPIO.setup(i, GPIO.OUT) for i in switch_list: GPIO.setup(i, GPIO.IN) # create a default object, no changes to I2C address or frequency mh = Raspi_MotorHAT(addr=0x6f) # recommended for auto-disabling motors on shutdown! def turnOffMotors(): mh.getMotor(1).run(Raspi_MotorHAT.RELEASE) mh.getMotor(2).run(Raspi_MotorHAT.RELEASE) mh.getMotor(3).run(Raspi_MotorHAT.RELEASE) mh.getMotor(4).run(Raspi_MotorHAT.RELEASE) GPIO.output(LED1r, 0) GPIO.output(LED2r, 0) GPIO.output(LED3r, 0) GPIO.output(LED4r, 0) GPIO.output(LED1g, 0) GPIO.output(LED2g, 0) GPIO.output(LED3g, 0)
GPIO.setup(i, GPIO.OUT) # Switches not yet implemented... # SWmaster = 33 # SW1 = 29 # SW2 = 31 # SW3 = 36 # SW4 = 37 # SW = [ 0, SW1, SW2, SW3, SW4 ] # switch_list = [SWmaster, SW1, SW2, SW3, SW4] # for i in switch_list: # GPIO.setup(i, GPIO.IN) # create a default object, no changes to I2C address or frequency mh = Raspi_MotorHAT(addr=0x6f) # recommended for auto-disabling motors on shutdown! def turnOffMotors(): for i in range(1, 4): mh.getMotor(i).run(Raspi_MotorHAT.RELEASE) GPIO.output(LEDr[i], 1) GPIO.output(LEDg[i], 0) MOTORS = 4 MOTOR_SEGMENTS = 4 MOTOR_SPEED = [0, 28, 15, 15, 28] MOTOR_DURATION = [0, 10, 10, 10, 10.5] # MOTOR_DELAY = [ 0, 5, 5, 5, 5 ] # This took 03:55.06, so add 56:04 / 16 = 210.5.
#!/usr/bin/python from Raspi_MotorHAT import Raspi_MotorHAT, Raspi_DCMotor import time import atexit # create a default object, no changes to I2C address or frequency mh = Raspi_MotorHAT(addr=0x6f) # recommended for auto-disabling motors on shutdown! def turnOffMotors(): mh.getMotor(1).run(Raspi_MotorHAT.RELEASE) mh.getMotor(2).run(Raspi_MotorHAT.RELEASE) mh.getMotor(3).run(Raspi_MotorHAT.RELEASE) mh.getMotor(4).run(Raspi_MotorHAT.RELEASE) atexit.register(turnOffMotors) ################################# DC motor test! #myMotor = mh.getMotor(3) FL = mh.getMotor(3) FR = mh.getMotor(4) BL = mh.getMotor(1) BR = mh.getMotor(2) # set the speed to start, from 0 (off) to 255 (max speed) #myMotor.setSpeed(150) #myMotor.run(Raspi_MotorHAT.FORWARD);
#!/usr/bin/python from Raspi_MotorHAT import Raspi_MotorHAT, Raspi_DCMotor import time import atexit # create a default object, no changes to I2C address or frequency mh = Raspi_MotorHAT(addr=0x6f) # recommended for auto-disabling motors on shutdown! def turnOffMotors(): mh.getMotor(1).run(Raspi_MotorHAT.RELEASE) mh.getMotor(2).run(Raspi_MotorHAT.RELEASE) mh.getMotor(3).run(Raspi_MotorHAT.RELEASE) mh.getMotor(4).run(Raspi_MotorHAT.RELEASE) atexit.register(turnOffMotors) ################################# DC motor test! myMotor = mh.getMotor(3) # set the speed to start, from 0 (off) to 255 (max speed) myMotor.setSpeed(150) myMotor.run(Raspi_MotorHAT.FORWARD) # turn on motor myMotor.run(Raspi_MotorHAT.RELEASE) while True: print "Forward! "
#!/usr/bin/python from Raspi_MotorHAT import Raspi_MotorHAT, Raspi_DCMotor import time import atexit # create a default object, no changes to I2C address or frequency mh = Raspi_MotorHAT(addr=0x6f) # recommended for auto-disabling motors on shutdown! def turnOffMotors(): mh.getMotor(1).run(Raspi_MotorHAT.RELEASE) mh.getMotor(2).run(Raspi_MotorHAT.RELEASE) mh.getMotor(3).run(Raspi_MotorHAT.RELEASE) mh.getMotor(4).run(Raspi_MotorHAT.RELEASE) atexit.register(turnOffMotors) ################################# DC motor test! # Motors are stupidly numbered from 1. These numbers match the # numbers printed on the board: M1 M2 M3 M4. myMotor = mh.getMotor(3) # set the speed to start, from 0 (off) to 255 (max speed) myMotor.setSpeed(150) myMotor.run(Raspi_MotorHAT.FORWARD) # turn on motor myMotor.run(Raspi_MotorHAT.RELEASE)
class Robot(object): wheel_diameter_mm = 69.0 ticks_per_revolution = 40.0 wheel_distance_mm = 140.0 def __init__(self, motorhat_addr=0x6f, drive_enabled=True): # Setup the motorhat with the passed in address self._mh = Raspi_MotorHAT(addr=motorhat_addr) # get local variable for each motor self.left_motor = self._mh.getMotor(1) self.right_motor = self._mh.getMotor(2) self.drive_enabled = drive_enabled # ensure the motors get stopped when the code exits atexit.register(self.stop_all) # Setup the line sensors self.left_line_sensor = LineSensor(23, queue_len=3, pull_up=True) self.right_line_sensor = LineSensor(16, queue_len=3, pull_up=True) # Setup The Distance Sensors self.left_distance_sensor = DistanceSensor(17, 27) self.right_distance_sensor = DistanceSensor(5, 6) # Setup the Encoders EncoderCounter.set_constants(self.wheel_diameter_mm, self.ticks_per_revolution) self.left_encoder = EncoderCounter(4) self.right_encoder = EncoderCounter(26) # Setup the Leds self.leds = leds_8_apa102c.Leds() # Set up servo motors for pan and tilt. self.servos = Servos(addr=motorhat_addr) def stop_all(self): self.stop_motors() # Clear any sensor handlers self.left_line_sensor.when_line = None self.left_line_sensor.when_no_line = None self.right_line_sensor.when_line = None self.right_line_sensor.when_no_line = None self.left_encoder.stop() self.right_encoder.stop() # Clear the display self.leds.clear() self.leds.show() # Reset the servos self.servos.stop_all() def convert_speed(self, speed): mode = Raspi_MotorHAT.RELEASE if speed > 0: mode = Raspi_MotorHAT.FORWARD elif speed < 0: mode = Raspi_MotorHAT.BACKWARD output_speed = (abs(speed) * 255) / 100 return mode, output_speed def set_left(self, speed): if not self.drive_enabled: return mode, output_speed = self.convert_speed(speed) self.left_motor.setSpeed(output_speed) self.left_motor.run(mode) def set_right(self, speed): if not self.drive_enabled: return mode, output_speed = self.convert_speed(speed) self.right_motor.setSpeed(output_speed) self.right_motor.run(mode) def stop_motors(self): self.left_motor.run(Raspi_MotorHAT.RELEASE) self.right_motor.run(Raspi_MotorHAT.RELEASE) def set_pan(self, angle): self.servos.set_servo_angle(1, angle) def set_tilt(self, angle): self.servos.set_servo_angle(0, angle)