-1

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
}
  • Can you post the arduino code? If you get closed. Repost on https://forum.micropython.org/index.php – phil Mar 24 '19 at 04:00
  • Can you repost your code. It has to all be indented an extra 4 spaces for the stackoverflow to make it into a code block – phil Mar 24 '19 at 07:36
  • @rhubarbdog There, sorry for such a late response. – Kyle Everett Shapen Mar 24 '19 at 23:02
  • What is your question? – nekomatic Mar 25 '19 at 10:53
  • @Kyle Everett Shapen i looked at the arduino code. The device is not i2c it works by `.write_digital()` to a pin to engage that motor followed by `.write_analog()` to another pin to control the speed. Do you have a pdf for this device? Post a link to it. I think i know how to wire it up and get mooving. – phil Mar 26 '19 at 09:25
  • This is link to the product page and that’s all the documentation I have on it. https://www.dfrobot.com/wiki/index.php/Quad_Motor_Driver_Shield_for_Arduino_SKU:DRI0039 – Kyle Everett Shapen Mar 26 '19 at 16:27
  • Also, thank you for the help. That makes a little more sense. Was only taught to use an i2c so it’s confusing for me. – Kyle Everett Shapen Mar 26 '19 at 16:27
  • How does it connect to an arduino uno? Do you just use jumper cables between the arduino and motor hat's digital (green) bus or does it push straight on like a raspberry pi HAT – phil Mar 26 '19 at 16:51
  • Does it have pins on the bottom to plug onto an arduino uno – phil Mar 26 '19 at 17:41
  • Which pyboard are you using. It effects the program I'm going to supply – phil Mar 26 '19 at 17:47
  • The MicroPython pyboard v1.1. Linked here ---> https://www.sparkfun.com/products/14413 – Kyle Everett Shapen Mar 26 '19 at 18:46
  • I am not connecting to arduino Uno I'm going straight from my micropython Pyboard to the motor shield – Kyle Everett Shapen Mar 26 '19 at 18:48
  • Wow thank you for the detail. I think this will finally get me moving thank you for all the help. – Kyle Everett Shapen Mar 26 '19 at 21:37

1 Answers1

0

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

phil
  • 561
  • 3
  • 10
  • @Kyle Everett Shapen i noticed you were also using an HCSR04 ultrasonic distance measurement. Here's a link to some pyboard code for this https://github.com/rhubarbdog/pyboard-hcsr04. – phil Mar 29 '19 at 00:47