Motor

javadoc

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


#file : service/Motor.py edit raw
# 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

arduino = Runtime.start("arduino", "Arduino")
arduino.connect("COM15")

m1 = Runtime.start("m1","Motor")

# connect motor m1 with pwm power pin 3, direction pin 4
arduino.motorAttach("m1", 3, 4) 

# 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()


#file : service/MotorC16.py edit raw
# The motor service can also be connected to an Adafruit 16-channel servo driver
# The limitation is that you have to choose between using it to drive servos or motors, not both at the same time.
# The reason is that only one pwm frequency can be set for all pins
# This is how you use the motor service when it's connected to an Adafruit 16-channel servo driver thru the Arduino
arduino = Runtime.createAndStart("Arduino", "Arduino");
arduino.connect("COM3")
ada = Runtime.createAndStart("Ada","Adafruit16CServoDriver")
ada.setController(arduino,"1","0x40")
motor01 = Runtime.createAndStart("motor01", "Motor");
motor01.setPwmPins(0,1);
motor01.attach(ada);
motor01.move(0.3);

# This is how you use the motor service when it's connected to an Adafruit 16-channel servo driver connected to the i2c GPIO pins on the Raspberry PI
raspi = Runtime.createAndStart("RasPi", "RasPi");
ada = Runtime.createAndStart("Ada","Adafruit16CServoDriver")
ada.setController(raspi,"1","0x40")
motor01 = Runtime.createAndStart("motor01", "Motor");
motor01.setPwmPins(0,1);
motor01.attach(ada);
motor01.move(0.3);

 

Examples:


#file : service/Motor.py edit raw
# 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

arduino = Runtime.start("arduino", "Arduino")
arduino.connect("COM15")

m1 = Runtime.start("m1","Motor")

# connect motor m1 with pwm power pin 3, direction pin 4
arduino.motorAttach("m1", 3, 4) 

# 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's picture

Above python code seems to be

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)
GroG's picture

Thank you Darek ! That is

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 !

Darek's picture

my pleasure (; Now script is

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.

 

 

GroG's picture

What H-Bridge are you

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?
Darek's picture

you 'are right. I Have the

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
 
 
Darek's picture

As it turned out, this driver

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 ?
GroG's picture

what is IN1 HIGH IN2 HIGH ?

what is IN1 HIGH IN2 HIGH ?  

EXPLODE ?!?! :D

I can probably do this, I will see...

Darek's picture

your proposal is interesting

your proposal is interesting and will hand over to producer (;

GroG's picture

Ya 3 wire I will do.. but I

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.

Darek's picture

for motor DC driver high high

for motor DC driver high high state is not descreibed

for driving stepper motor - yes

 

datasheet:

http://www.icstation.com/ebay/IC/All%20data%20modules/1381.zip

ambroise's picture

  HELLO EVERYBODY here the

 

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)