def __init__(self): GPIO.setmode(GPIO.BCM) self.direction = Servo(27, (-45, 45), (72.5, 162.5)) # board pin: 13 self.direction.trim = -1 self.front_motor = DCMotor(20, 6, 13) # board pin: 38, 31, 33 self.rear_motor = DCMotor(21, 19, 26) # board pin: 40, 35, 37 self.speed = 0 self.w_angle = self.direction.angle self.mov = 'quieto' """
def __init__(self, MotorE, MotorA, MotorB, servo_pin, trigger, echo,green,yellow,red,distance_timer): GPIO.setwarnings(False) self.distance_timer = distance_timer self.dcmotor = DCMotor(MotorE, MotorA, MotorB) self.servomotor = ServoMotor(servo_pin) self.distancesensor = DistanceSensor(trigger,echo) self.green = green self.yellow = yellow self.red = red GPIO.setup(self.green,GPIO.OUT) GPIO.setup(self.yellow,GPIO.OUT) GPIO.setup(self.red,GPIO.OUT)
# Complete project details at https://RandomNerdTutorials.com from dcmotor import DCMotor from machine import Pin, PWM from time import sleep frequency = 15000 pin1 = Pin(12, Pin.OUT) pin2 = Pin(14, Pin.OUT) enable = PWM(Pin(13), frequency) dc_motor = DCMotor(pin1, pin2, enable, 350, 1023) dc_motor.forward(50) sleep(2) dc_motor.stop() sleep(3) dc_motor.backwards(100) sleep(2) dc_motor.forward(5) sleep(5) dc_motor.stop()
class Roomba: def __init__(self, MotorE, MotorA, MotorB, servo_pin, trigger, echo,green,yellow,red,distance_timer): GPIO.setwarnings(False) self.distance_timer = distance_timer self.dcmotor = DCMotor(MotorE, MotorA, MotorB) self.servomotor = ServoMotor(servo_pin) self.distancesensor = DistanceSensor(trigger,echo) self.green = green self.yellow = yellow self.red = red GPIO.setup(self.green,GPIO.OUT) GPIO.setup(self.yellow,GPIO.OUT) GPIO.setup(self.red,GPIO.OUT) def red_ON(self): GPIO.output(self.red,GPIO.HIGH) def red_OFF(self): GPIO.output(self.red,GPIO.LOW) def yellow_ON(self): GPIO.output(self.yellow,GPIO.HIGH) def yellow_OFF(self): GPIO.output(self.yellow,GPIO.LOW) def green_ON(self): GPIO.output(self.green,GPIO.HIGH) def green_OFF(self): GPIO.output(self.green,GPIO.LOW) def cleanup(self): GPIO.setup(self.green,GPIO.LOW) GPIO.setup(self.yellow,GPIO.LOW) GPIO.setup(self.red,GPIO.LOW) self.servomotor.cleanup() self.dcmotor.cleanup() self.distancesensor.cleanup() GPIO.cleanup() def reverse_turn(self,direction = 1): self.dcmotor.stop() self.servomotor.turn(direction*50) time.sleep(0.5) while True: print("reverse") self.dcmotor.change_speed(100) self.dcmotor.backward() time.sleep(0.8) self.dcmotor.stop() dist = self.distancesensor.get_distance() if dist > 60: break self.servomotor.turn(0) time.sleep(0.5) self.dcmotor.change_speed(90) self.dcmotor.forward() def random_turn(self): LAG = 0.4 if (random.random() < 0.5): direction = +1 else: direction = -1 print("turning") self.dcmotor.change_speed(90) while True: dist = self.distancesensor.get_distance() if dist > 100: time.sleep(LAG) print("turn complete") self.servomotor.turn(0) self.dcmotor.change_speed(80) print(self.servomotor.angle) break elif 80 < dist <= 100: self.servomotor.turn(direction*10) elif 60 < dist <= 80: self.servomotor.turn(direction*30) elif 40 < dist <= 60: self.servomotor.turn(direction*50) elif dist <= 20: print("stopped while turning") self.dcmotor.stop() self.reverse_turn(direction) break time.sleep(self.distance_timer)
class RCCar(object): def __init__(self): GPIO.setmode(GPIO.BCM) self.direction = Servo(27, (-45, 45), (72.5, 162.5)) # board pin: 13 self.direction.trim = -1 self.front_motor = DCMotor(20, 6, 13) # board pin: 38, 31, 33 self.rear_motor = DCMotor(21, 19, 26) # board pin: 40, 35, 37 self.speed = 0 self.w_angle = self.direction.angle self.mov = 'quieto' """ self.rear_sensor = BatSensor(17, 27, rango=(10, 80), continuous=0.2) # board pin: 11, 13 self.front_left_sensor = BatSensor(22, 10, rango=(10, 80), continuous=0.2) # board pin: 15, 19 self.front_right_sensor = BatSensor(9, 11, rango=(10, 80), continuous=0.2) # board pin: 21, 23 """ def stop(self): self.in_use = False # dejar el coche frenando def turn_left(self): if self.survival_instinct(): return -self.direction.angle print("Izquierda:", self.direction.angle) return -self.direction.update(-5) def turn_right(self): if self.survival_instinct(): return -self.direction.angle print("Derecha:", self.direction.angle) return -self.direction.update(5) def forward(self): self.mov = 'adelante' if self.survival_instinct(): return self.speed if self.speed != 1: self.speed = 1 self.front_motor.forward() self.rear_motor.forward() print("Acelero hacia delante") return self.speed def backward(self): self.mov = 'atras' if self.survival_instinct(): return self.speed if self.speed != -1: self.speed = -1 self.front_motor.backward() self.rear_motor.backward() print("Acelero hacia atras") return self.speed def decelerate(self): if self.mov != 'quieto': print("Desacelero") #self.mov = 'quieto' self.front_motor.release() self.rear_motor.release() def survival_instinct(self): return False def survival_instinct_not_in_use(self): """ esta funcion no es ta en uso """ limit = 0.8 * max(self.front_left_sensor._rango) rear = self.rear_sensor.avg left = self.front_left_sensor.avg right = self.front_right_sensor.avg print("sensores R({}), FL({}), FR({})".format(rear, left, right)) if self.mov == 'atras' and rear < limit: self.brake() return True elif self.mov == 'adelante': if right < limit and left < limit: self.brake() return True elif right < limit: self.brake() return True elif left < limit: self.brake() return True return False def straight(self): if self.survival_instinct(): return -self.direction.angle self.direction.update(-self.direction.angle) return -self.direction.angle def brake(self): print("Freno") self.mov = 'quieto' self.front_motor.brake() self.rear_motor.brake()
def __init__(self, pinVelL, pinDirL, pinVelD, pinDirD): self.name = 'L9110URA' self.motorLeft = DCMotor(pinVelL, pinDirL) self.motorRight = DCMotor(pinVelD, pinDirD)
frequency = 15000 """ #define ENAR 14 --> Enable/speed motors Right GPIO14(D5) #define ENBL 12 --> Enable/speed motors Left GPIO12(D6) #define pin1 15 --> L298N in1 motors Right GPIO15(D8) #define pin2 13 --> L298N in2 motors Right GPIO13(D7) #define pin3 2 --> L298N in3 motors Left GPIO2(D4) #define pin4 0 --> L298N in4 motors Left GPIO0(D3) """ pin1 = Pin(15, Pin.OUT) pin2 = Pin(13, Pin.OUT) enableFB = PWM(Pin(14), frequency) dc_motorFB = DCMotor(pin1, pin2, enableFB, 350, 1023) enableRL = PWM(Pin(12), frequency) pin3 = Pin(2, Pin.OUT) pin4 = Pin(0, Pin.OUT) dc_motorRL = DCMotor(pin3, pin4, enableRL, 350, 1023) def cont(y, x, s): print('h,v,s=', y, x, s) r = 1023 / (2 ** .5) if (-r < x and x < r and y > 0): # forward dc_motorFB.forward(s) dc_motorRL.stop() print("forword")
class L9110URA(DCMotor): def __init__(self, pinVelL, pinDirL, pinVelD, pinDirD): self.name = 'L9110URA' self.motorLeft = DCMotor(pinVelL, pinDirL) self.motorRight = DCMotor(pinVelD, pinDirD) def forward(self): self.motorLeft.direction(1) self.motorLeft.speed(1000) self.motorRight.direction(1) self.motorRight.speed(1000) def backward(self): self.motorLeft.direction(0) self.motorLeft.speed(0) self.motorRight.direction(0) self.motorRight.speed(0) def turnLeft(self): self.motorLeft.direction(0) self.motorLeft.speed(0) self.motorRight.direction(1) self.motorRight.speed(1000) def turnRight(self): self.motorLeft.direction(1) self.motorLeft.speed(1000) self.motorRight.direction(0) self.motorRight.speed(0) def stop(self): self.motorLeft.direction(1) self.motorLeft.speed(0) self.motorRight.direction(1) self.motorRight.speed(0)
from dcmotor import DCMotor from time import sleep motor = DCMotor(20, 21, pwmPin=16) motor.forward(0.2) sleep(1) motor.backward(0.2) sleep(1) motor.stop()
import socket import gc from dcmotor import DCMotor from machine import Pin, Signal, PWM gc.collect() ledLight = Signal(Pin(2, Pin.OUT), invert=True) # GPIO2 ( D4 ) ledLight.on() pwmFrequency = 500 enablePin = PWM(Pin(4), pwmFrequency) # GPIO4 ( D2 ) pin1 = Pin(5, Pin.OUT) # GPIO5 ( D1 ) pin2 = Pin(14, Pin.OUT) # GPIO14 ( D5 ) dcMotorA = DCMotor(pin1, pin2, enablePin) pin3 = Pin(12, Pin.OUT) # GPIO12 ( D6 ) pin4 = Pin(13, Pin.OUT) # GPIO13 ( D7 ) dcMotorB = DCMotor(pin3, pin4, enablePin) forwardSpeed = 100 backwardSpeed = 80 def web_page(): html = """ <html> <head> <style> html {
#!/usr/bin/env python3 # REF: https://www.electronicshub.org from dcmotor import DCMotor G_LOW_SPEED = 4 G_MEDIUM_SPEED = 7 G_HIGH_SPEED = 10 dcMotorA = DCMotor(17, 27) # GPIO17, GPIO27 def printCmdTips(): print('\nPlease enter the following cmd to continue ↓') print('f: Run forward, b: Run backward, h: High speed, s: Stop\n') def go(): printCmdTips() while True: x = input() try: if x == 'f': dcMotorA.runForward(G_MEDIUM_SPEED) elif x == 'b': dcMotorA.runBackward(G_LOW_SPEED) elif x == 'h': dcMotorA.runForward(G_HIGH_SPEED) elif x == 's': dcMotorA.stop() else:
def main(): gc.collect() ledLight = Signal(Pin(2, Pin.OUT), invert=False) # GPIO2 ( D2 ) sleep(0.1) ledLight.on() dcMotorA = DCMotor(4, 5) # GPIO4 ( D4 ), GPIO5 ( D5 ) sleep(0.1) dcMotorA.stop() servoB = CServo(15) # GPIO15 ( D15 ) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(('', 80)) sock.listen(5) while True: try: if gc.mem_free() < G_GC_THRESHOLD: gc.collect() conn, addr = sock.accept() conn.settimeout(3.0) print('Received HTTP GET connection request from %s' % str(addr)) request = conn.recv(1024) conn.settimeout(None) request = str(request) print('GET Rquest Content = %s' % request) car_direction = request.find('/?car_direction') turn_left = request.find('/?turn_left') car_forward = request.find('/?car_forward') turn_right = request.find('/?turn_right') speed_up = request.find('/?speed_up') car_stop = request.find('/?car_stop') slow_down = request.find('/?slow_down') car_backward = request.find('/?car_backward') if car_direction == G_CMD_IDX: endIdx = request.find(' HTTP/') subReq = request[G_CMD_IDX:endIdx] startIdx = subReq.find('=') + 1 virtualAngle = int(subReq[startIdx:]) print('cmd: car_direction, virtualAngle =', virtualAngle) ledLight.on() servoB.carDirection(virtualAngle) elif turn_left == G_CMD_IDX: print('cmd: turn_left') ledLight.on() servoB.turnLeft() elif car_forward == G_CMD_IDX: print('cmd: car_forward') ledLight.on() dcMotorA.forward(G_FORWARD_SPEED) elif turn_right == G_CMD_IDX: print('cmd: turn_right') ledLight.on() servoB.turnRight() elif speed_up == G_CMD_IDX: print('cmd: speed_up') ledLight.on() dcMotorA.speedUp() elif car_stop == G_CMD_IDX: print('cmd: car_stop') ledLight.off() dcMotorA.stop() elif slow_down == G_CMD_IDX: print('cmd: slow_down') ledLight.on() dcMotorA.slowDown() elif car_backward == G_CMD_IDX: print('cmd: car_backward') ledLight.on() dcMotorA.backward(G_BACKWARD_SPEED) resHtml = getWebPage() conn.send('HTTP/1.1 200 OK\n') conn.send('Content-Type: text/html\n') conn.send('Connection: closed.\n\n') conn.sendall(resHtml) conn.close() except OSError as e: conn.close() print('Connection closed. OSError =', e)