Пример #1
0
# 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()
Пример #2
0
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)
Пример #3
0
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()
Пример #4
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()


Пример #5
0
 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_forward = request.find('/?car_forward')
 car_backward = request.find('/?car_backward')
 car_left = request.find('/?car_left')
 car_right = request.find('/?car_right')
 car_stop = request.find('/?car_stop')
 cmdIdx = 6
 if car_forward == cmdIdx:
     print('cmd: car_forward')
     ledLight.on()
     dcMotorA.forward(forwardSpeed)
     dcMotorB.forward(forwardSpeed)
 elif car_backward == cmdIdx:
     print('cmd: car_backward')
     ledLight.on()
     dcMotorA.backward(backwardSpeed)
     dcMotorB.backward(backwardSpeed)
 elif car_left == cmdIdx:
     print('cmd: car_left')
     ledLight.on()
     dcMotorA.backward(backwardSpeed)
     dcMotorB.forward(backwardSpeed)
 elif car_right == cmdIdx:
     print('cmd: car_right ')
     ledLight.on()
     dcMotorA.forward(backwardSpeed)
Пример #6
0
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)