如何将Micropython上的Motor导入DFRobot Quad Motor Shield

时间:2019-03-24 02:18:36

标签: python robotics micropython

我正在尝试将两台电机连接到我的DFRobot DC Quad电机护罩的Micropython pyboard中。他们的示例代码是用Arduino写的,我无法翻译。附上我以前的代码无效,并且电动机罩的速度和连接在其上方。

感谢您的帮助!

Code Previously Written

Board Overview

这就是我所拥有的

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)

这是arduino代码

/*!
* @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
}

1 个答案:

答案 0 :(得分:0)

这是尝试之一。考虑使用保险丝,直到我们确定这是将DFRobot Motor HAT连接到pyboard的方式。 电动机帽上的连接点是绿色的数字总线。

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)

我猜想FREQ为50Hz,对于许多伺服器来说,我猜arduino使用它作为PWM(脉冲宽度调制)的默认值。

将电动机连接到连接器M1,将电池连接到电动机帽的电源插座。不要连接到pyboard上的v +。用自己的电源为pyboard供电。您的运动帽可能会产生很多电子噪音。

电动机应向后加速,然后突然停止,然后再向前加速,然后突然停止。

祝你好运