简体   繁体   中英

How to import Motor on Micropython into a DFRobot Quad Motor Shield

I am attempting to import two motors onto my pyboard in micropython that are connected to my DFRobot DC Quad Motor Shield. Example code from them is written in Arduino and I cannot translate. Attached is my previous code that did not work and the motor shield over view with its speeds and connections.

Any help is appreciated!

Code Previously Written

Board Overview

Here is what I have

i2c = machine.I2C(scl=machine.Pin('Y9'), sda=machine.Pin('Y10'))
motors = motor.DCMotors(i2c)
MOTOR1 = 3
MOTOR2 = 4
#Initiate Communication from Sonar sensor
sensor_front = adafruit_hcsr04.HCSR04(trigger_pin=board.Pin('X3'), echo_pin=board.Pin('X4'))
sensor_back = adafruit_hcsr04.HCSR04(trigger_pin=board.Pin('X6'), echo_pin=board.Pin('X7'))
#Create minimum distance For Ultrasonic sensor
min_distance = sensor.distance(70)
button = pyb.switch()
def autonomy():
    #no_problem = True
    while (True):
        if (button()):
            dist_front = sensor1.distance(15)
            dist_back = sensor2.distance(15)
            if dist_front > min_distance:
                print('Nothing can stop me!')
                motors.speed(MOTOR1, 1500)
                motors.speed(MOTOR2,-1500)

Here is the arduino code

/*!
* @file QuadMotorDriverShield.ino
* @brief QuadMotorDriverShield.ino  Motor control program
*
* Every 2 seconds to control motor positive inversion
* 
* @author linfeng(490289303@qq.com)
* @version  V1.0
* @date  2016-4-5
*/
const int E1 = 3; ///<Motor1 Speed
const int E2 = 11;///<Motor2 Speed
const int E3 = 5; ///<Motor3 Speed
const int E4 = 6; ///<Motor4 Speed

const int M1 = 4; ///<Motor1 Direction
const int M2 = 12;///<Motor2 Direction
const int M3 = 8; ///<Motor3 Direction
const int M4 = 7; ///<Motor4 Direction


void M1_advance(char Speed) ///<Motor1 Advance
{
 digitalWrite(M1,LOW);
 analogWrite(E1,Speed);
}
void M2_advance(char Speed) ///<Motor2 Advance
{
 digitalWrite(M2,HIGH);
 analogWrite(E2,Speed);
}
void M3_advance(char Speed) ///<Motor3 Advance
{
 digitalWrite(M3,LOW);
 analogWrite(E3,Speed);
}
void M4_advance(char Speed) ///<Motor4 Advance
{
 digitalWrite(M4,HIGH);
 analogWrite(E4,Speed);
}

void M1_back(char Speed) ///<Motor1 Back off
{
 digitalWrite(M1,HIGH);
 analogWrite(E1,Speed);
}
void M2_back(char Speed) ///<Motor2 Back off
{
 digitalWrite(M2,LOW);
 analogWrite(E2,Speed);
}
void M3_back(char Speed) ///<Motor3 Back off
{
 digitalWrite(M3,HIGH);
 analogWrite(E3,Speed);
}
void M4_back(char Speed) ///<Motor4 Back off
{
 digitalWrite(M4,LOW);
 analogWrite(E4,Speed);
}



void setup() {
  for(int i=3;i<9;i++)
    pinMode(i,OUTPUT);
  for(int i=11;i<13;i++)
    pinMode(i,OUTPUT);
}

void loop() {
M1_advance(100);
M2_advance(100);
M3_advance(100);
M4_advance(100);
delay(2000); ///<Delay 2S
M1_back(100);
M2_back(100);
M3_back(100);
M4_back(100);
delay(2000); ///<Delay 2S
}

Here's attempt one. Consider using fuses until we're certain this is how to connect a DFRobot Motor HAT to a pyboard. The connection point on the motor hat is the green digital bus.

import pyb                                                                  

# don't use timers 2,3,5,6                                                  

'''                                                                         
connect:                                                                    
pyboard   Motor Hat                                                         
  X1         4                                                              
  X3         3          
 GND        GND                                            
'''                                                                         

FREQ = 50                                                                   

backward = pyb.Pin('X1' ,pyb.Pin.OUT_PP)                                    
speed = pyb.Timer(9, freq=FREQ).channel(1, pyb.Timer.PWM,\
                  pin = pyb.Pin('X3'))                                                                             

while True:                                                                 
    backward.high()                                                         
    for percent in range(100):                                                 
        speed.pulse_width_percent(percent + 1)
        pyb.delay(10)
    speed.pulse_width_percent(0)

    backward.low()
    for percent in range(100):
        speed.pulse_width_percent(percent + 1)
        pyb.delay(10)
    speed.pulse_width_percent(0)

I've guessed FREQ to be 50Hz it's that for many servos and i'm guessing arduino uses this as a default value for PWM (pulse width modulation).

Connect a motor to connector M1 and a battery to the motor hat's power socket. Don't connect to v+ on the pyboard. Power your pyboard with it's own power source. You motor hat probably creates a lot of electrical noise.

The motor should go backwards speeding up then suddenly stopping before going forwards getting faster then suddenly stop.

good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM