Quantcast
Channel: Arduino Forum - Latest topics
Viewing all articles
Browse latest Browse all 15374

Controlle Motors via MotoDriver2/ l298n und Raspberry pi pico w

$
0
0

Hello,
I would like to control two motors via the MotoDriver2 and control them via a Raspberry Pi Pico W.

My problem is that the two motors never rotate at the same speed, even though the code says so.

My first thought was that my power source wasn't providing enough voltage.
I read that I can connect up to 35 volts to the MotoDriver.
I tried it with 18 volts (two 9 volt blocks) and it didn't work.

To connect everything to one power source, I connected the Raspberry to the 5V pin of the MotoDriver.
I tried it several times with different MotoDrivers and Raspberries but always either the motors didn't rotate at the same speed or there was another problem such as too much voltage on the 5 volt pin or failure when connecting the Raspberry.

Is this a known problem with this MotoDriver, is it simply not designed for this use, is there a trick and what voltage is the right one?

Thanks!


#raspberry_connected()
#searchforble_devices()

import aioble
import bluetooth
import machine
import uasyncio as asyncio



_REMOTE_UUID = bluetooth.UUID(0x1848)
_ENV_SENSE_UUID = bluetooth.UUID(0x1800) 
_REMOTE_CHARACTERISTICS_UUID = bluetooth.UUID(0x2A6E)
_REMOTE_CHARACTERISTICS2_UUID = bluetooth.UUID(0x787B)

led = machine.Pin("WL_GPIO0", machine.Pin.OUT)
connected = False

IN1 = machine.Pin(1, machine.Pin.OUT)
IN2 = machine.Pin(2, machine.Pin.OUT)
IN3 = machine.Pin(3, machine.Pin.OUT)
IN4 = machine.Pin(4, machine.Pin.OUT)
motor1_pin = 0
motor2_pin = 5
led.value(1)
servo_pin = 6


pwm = machine.PWM(machine.Pin(servo_pin))
pwm.freq(100)
speed1 = machine.PWM(machine.Pin(motor1_pin))
speed1.freq(1000)
speed2 = machine.PWM(machine.Pin(motor2_pin))
speed2.freq(1000)

Licht = 1


async def find_remote():
    
    async with aioble.scan(5000, interval_us=30000, window_us=30000, active=True) as scanner:
        async for result in scanner:

           
            if result.name() == "P1_controll":
                print("Found Controller")
                for item in result.services():
                    print (item)
                if _ENV_SENSE_UUID in result.services():
                    print("Found car Remote Service")
                    return result.device
            
    return None



async def peripheral_task():
    print('starting peripheral task')
    global connected, Licht
    connected = False
    device = await find_remote()
    if not device:
        print("car Remote not found")
        return
    try:
        print("Connecting to", device)
        connection = await device.connect()
        
    except asyncio.TimeoutError:
        print("Timeout during connection")
        return
      
    async with connection:
        print("Connected")
        connected = True
        alive = True
        while True and alive:
            try:
                car_service = await connection.service(_REMOTE_UUID)
                print(car_service)
                control_characteristic = await car_service.characteristic(_REMOTE_CHARACTERISTICS_UUID)
                print(control_characteristic)
                control_characteristic2 = await car_service.characteristic(_REMOTE_CHARACTERISTICS2_UUID)
                print(control_characteristic2)
                

            except asyncio.TimeoutError:
                print("Timeout discovering services/characteristics")
                return
            
            
            while True:
                 
                if control_characteristic != None:
                    try:
                        command = await control_characteristic.read()
                        received_xValue = int.from_bytes(command, 'little')
                        print(f"Received Valuex: {received_xValue}")
                        
                        command2 = await control_characteristic2.read()
                        received_yValue = int.from_bytes(command2, 'little') 
                        print(f"Received Valuey: {received_yValue}")
                        
                        if 30768 <= received_xValue <= 34768:
                            pwm.duty_ns(1500000)
                        else:
                            pwm.duty_ns(received_xValue*30+500000)
                        
                        if 30768 <= received_yValue <= 34768:
                            speed1.duty_u16(0)
                            speed2.duty_u16(0)
                            
                        if received_yValue < 30768:   #ruckwärts
                            speed1.duty_u16((received_yValue-32768)*(-2))
                            speed2.duty_u16((received_yValue-32768)*(-2))
                            #print("rückwärts",speed2.duty_u16())
                            IN1.low()
                            IN2.high()
                            IN3.low()
                            IN4.high()
                            
                        if received_yValue > 34768:    #vorwärts
                            speed1.duty_u16((received_yValue-32768)*2)
                            speed2.duty_u16((received_yValue-32768)*2)
                            #print("vorwärts",speed2.duty_u16())    
                            IN1.high()
                            IN2.low()
                            IN3.high()
                            IN4.low()
                            
                            
                                
                                
                    except TypeError:
                        print(f'something went wrong; remote disconnected?')
                        connected = False
                        alive = False
                        return
                    except asyncio.TimeoutError:
                        print(f'something went wrong; timeout error?')
                        connected = False
                        alive = False
                        return
                    except asyncio.GattError:
                        print(f'something went wrong; Gatt error - did the remote die?')
                        connected = False
                        alive = False
                        return
                
                await asyncio.sleep_ms(10)

async def main():
    tasks = []
    tasks = [
        
        asyncio.create_task(peripheral_task()),
    ]
    await asyncio.gather(*tasks)
    
while True:
    asyncio.run(main())




1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 15374

Trending Articles