Javadoc link

A general DC continous Motor which is controlled by 2 inputs.
A motor controller (such as Arduino or AdafruitMotorShield) is needed for the Motor service to attach.
One of the most useful methods is the motor's move(powerLevel).  The powerLevel needs to be a float value between -1.0 and 1.0   
0.0 is stop.  1.0 is full power in a clockwise direction, and -1.0 is full power is a counter clockwise direction. And fractional value will resolve to a fractional amount of power which the motor controller can supply.  e.g. 0.5 would be clockwise at 1/2 power.

Interfacing a motor and motor controller in Python

Be aware that this service use AnalogWrite so it can not be used on all pins.

On the Arduino Uno pins 3,5,6,9,10 and 11 can be used.

On the Arduino Mega pins 2-13 and 44-46 can be used.

For other boards please check the reference below

References

https://www.arduino.cc/en/Reference/AnalogWrite

MOTOR TYPE - SIMPLE - 1 PWM & 1 DIR 


  • DFRobot v1.3 Dual 2A Hbridge 

    This is a 4.8-46V, 2A Dual Motor Controller which is the revised version of the DF-MDV1.0. Its performance has been improved greatly. It can bear larger current due to the increased haetsink dissipation. It is easy to control, using LGS's outstanding high-power motor driver chip, the L298N. This chip allows for direct drive of two bi-directional DC motors, and incorporates high-speed short diodes for protection. Drive current up to 2A per motor output. The driver uses a broad-brush design to reduce wire resistance.

References

[[service/Motor.py]]

[[service/MotorC16.py]]

 

Example code (from branch develop):
#file : Motor.py (github)
#########################################
# Motor.py
# categories: motor
# more info @: http://myrobotlab.org/service/Motor
#########################################
# uncomment for virtual hardware
virtual = True
 
# demonstrates the basic motor api
# an Arduino is used as a motor controller
# this dc motor has a simple h-bridge
# 1 pin controls power/speed with pulse width modulation
# the other controls direction
 
port = "COM99"
 
# start the services
arduino = runtime.start("arduino", "Arduino")
m1 = runtime.start("m1","Motor")
m1.setPwrPin(3)
m1.setDirPin(4)
 
# start optional virtual arduino service, used for test
if ('virtual' in globals() and virtual):
    virtualArduino = runtime.start("virtualArduino", "VirtualArduino")
    virtualArduino.connect(port)
 
# connect the Arduino - (our motor controller)
arduino.connect(port)
 
# connect motor m1 with pwm power pin 3, direction pin 4
arduino.attach(m1)
 
# move both motors forward
# at 50% power
# for 2 seconds
m1.move(0.5)
 
sleep(2)
 
# move both motors backward
# at 50% power
# for 2 seconds
m1.move(-0.5)
 
sleep(2)
 
# stop and lock m1
m1.stopAndLock()
 
# after locking
# m1 should not move
m1.move(0.5)
 
sleep(2)
 
# unlock m1 and move it
m1.unlock()
m1.move(0.5)
 
sleep(2)
 
m1.stop()

Darek

11 years 3 months ago

Above python code seems to be out of date and it is not working for me. So I done some  small  alteration and now is working.
Pls,  correct me if something is bad.

 

arduino = Runtime.createAndStart("arduino", "Arduino")
arduino.setSerialDevice("COM10")
m1 = Runtime.createAndStart("M1","Motor")
m2 = Runtime.createAndStart("M2","Motor")
 
arduino.motorAttach("M1", 3, 4) 
arduino.motorAttach("M2", 6, 7) 
 
M1.move(0.5)
M2.move(0.5)

Thank you Darek !

That is great.. the script was out of date.. I have updated your script slightly and moved it into the repo as the main example.  Could you test it to be sure it works.  

Thanks for the help !

my pleasure (;

Now script is working fine.

but I found  small bug related to motor speed ( + direction):

m1.move(0.1) give 90% power  and 1.0 -  stoped motor - so as I good understand is bad.
Second direction is ok
m1.move(-0.1) give 10% power so it is ok.

 

 

What H-Bridge are you using? 
1.0 should correspond to analogWrite 255 for a pwm pin - if that is not what your getting can you send me a no-worky (has to have DEBUG logging set or I won't get the values passed to the Arduino)?

I watched the values is a reduced script which only uses m1 .. and motor.move(1.0) gives me 255 on the pwm pin

for example :

m1.move(1.0)
m1.move(0.5)
m1.move(0.0)
m1.move(-0.5)
 
sends :
sendMsg -> MAGIC_NUMBER|SZ 3|FN 2|P0 3|P1 255
sendMsg -> MAGIC_NUMBER|SZ 3|FN 2|P0 3|P1 127
sendMsg -> MAGIC_NUMBER|SZ 3|FN 0|P0 4|P1 0
sendMsg -> MAGIC_NUMBER|SZ 3|FN 2|P0 3|P1 127 (with a different value on the direction pin)
 
is your H-bridge different in some way?

you 'are right. I Have the same for 1.0. 
 
- sendMsg -> MAGIC_NUMBER|SZ 3|FN 0|P0 4|P1 1
  - sendMsg -> MAGIC_NUMBER|SZ 3|FN 2|P0 3|P1 255
- sendMsg -> MAGIC_NUMBER|SZ 3|FN 0|P0 7|P1 1
  - sendMsg -> MAGIC_NUMBER|SZ 3|FN 2|P0 6|P1 255
  - sendMsg -> MAGIC_NUMBER|SZ 3|FN 0|P0 4|P1 0
  - sendMsg -> MAGIC_NUMBER|SZ 3|FN 2|P0 3|P1 255
 - sendMsg -> MAGIC_NUMBER|SZ 3|FN 0|P0 7|P1 0
- sendMsg -> MAGIC_NUMBER|SZ 3|FN 2|P0 6|P1 255
 
I'have this  H-bridge
 
 

As it turned out, this driver need 3 wires control: 

1.PWM for speed.  
2. Input 1 for direction
3. Input 2 for direction.
 
Input protocol for  motor control is: 
corotation: IN1 HIGH      IN2 LOW
reversal:    IN1 LOW      IN2 HIGH
Stop         IN1 LOW      IN2 LOW
 
Is there any way to run this driver in MRL ?

Ya 3 wire I will do.. but I was asking the question, what does you document for the H-bridge say will happen when both direction wires are high?  Nothing, or will it melt the H-Bridge ?

Sorry, I did not make myself clear.

ambroise

8 years 6 months ago

 

HELLO EVERYBODY

here the scrip python for people use this L298 like me for drive this motor dc

 

thank you tony

 

from time import sleep
 
arduino = Runtime.createAndStart("arduino", "Arduino")
arduino.serial.refresh()
sleep(1)
arduino.connect("COM10") 
sleep(1)
arduino.publishState()
arduino.digitalWrite(9,255)
 
moteur = Runtime.start("moteur","Motor")
moteur.setType2Pwm(7,8)
moteur.attach(arduino)
 
moteur.move(0.6)
sleep(2)
moteur.move(0.0)
sleep(2)
moteur.move(-0.6)
sleep(2)
moteur.move(0.0)
Example configuration (from branch develop):
#file : Motor.py (github)
!!org.myrobotlab.service.config.MotorConfig
axis: null
controller: null
dirPin: null
listeners: null
locked: false
mapper:
  clip: true
  inverted: false
  maxIn: 100.0
  maxOut: 100.0
  minIn: -100.0
  minOut: -100.0
peers: null
pwmFreq: null
pwrPin: null
type: Motor