Dual VNH5019 motor driver troubles reading library interpretation

Hi to all

I have this powerful motor driver, which documentation is found here

The thing is, I’ve been modifying a code that was made for adafruit motor driver V2, in order to use it on this shield.

another adafruit library reference AF_DCMotor Class | AFMotor Library Reference | Adafruit Learning System
I’ve accomplished make it working (drive two motors) but not in the way I want. The following lines are the ones I changed to do exactly the same.
The order is, original adafruit reading and a comment text with the current vnh5019 reading I put.


int direction1 = FORWARD;                         // int direction1 = 400
int direction2 = FORWARD;                         // int direction2 = 400
int prev_direction1 = RELEASE;                  // int prev_direction1 = 0
int prev_direction2 = RELEASE;                  // int prev_direction2 = 0
motor1->setSpeed(0);                                  // md.setM1Speed (0)          this line is fine for me
motor2->setSpeed(0);                                  // md.setM2Speed (0)          this line too
motor1->run(FORWARD);                            // md.setM1Speed (400)
motor1->run(RELEASE);                              // md.setM1Speed (0)
motor2->run(FORWARD);                            // md.setM2Speed (400)
motor2->run(RELEASE);                              // md.setM2Speed (0)
if(PWM_val1 > 0) direction1 = FORWARD;                // if(PWM_val1 > 0) direction1 = 400
else if(PWM_val1 < 0) direction1 = BACKWARD;      // else if(PWM_val1 < 0) direction1 = -400
if (rpm_req1 == 0) direction1 = RELEASE;                 // if (rpm_req1 == 0) direction1 = 0
if(PWM_val2 > 0) direction2 = FORWARD;                // if(PWM_val2 > 0) direction2 = 400
else if(PWM_val2 < 0) direction2 = BACKWARD;      // else if(PWM_val2 < 0) direction2 = -400
if (rpm_req2 == 0) direction2 = RELEASE;                 // if (rpm_req2 == 0) direction2 = 0
motor1->run(direction1);                                             // md.setM1Speed(direction1)
motor2->run(direction2);                                             // md.setM2Speed(direction2)
motor1->setSpeed(abs(PWM_val1));                         // md.setM1Speed(abs(PWM_val1))           this line is fine for me
motor2->setSpeed(abs(PWM_val2));                         // md.setM2Speed(abs(PWM_val2))           this line too

I think for RELEASE param I should put in there md.setSpeeds(0, 0) because in the second adafruit reference it says "is equivalent to setSpeed (0), but another problem appears: I can’t put a library inside a value, for instance “int prev_direction1 = md.setM1Speed(0)”. What should I put in that case? and what about the rest?
FORWARD, BACKWARD and motor1/2->run doesn’t work that way, so I’m lost in that part. Please I need a proper change and I’m quite new in arduino coding.
By the way, here is the original source code my_personal_robotic_companion/motor_controller.ino at master · sungjik/my_personal_robotic_companion · GitHub
And in attachments the one I changed.
Thanks!
motor_controller3.ino (11.2 KB)

Hello.

Is there a particular reason why you are not using our library for the our Dual VNH5019 Motor Driver Shield (which you linked to in your post) and trying to use the Adafruit library written specifically for their motor shield instead?

- Amanda

No, sorry. I meant to say I want those lines but for Dual VNH5019 Motor Driver, for pololu shield. Unfortunately are written for Adafruit Motor Driver V2 and I need to interpret it for VNH5019 and doing exactly the same. Amanda, could you help me translating that for this shield? I’m still trying to change that without succcess so far. Sorry for my english, I speak native spanish.

I read your first post again, and I think I understand what it is you are trying to do. Can you tell me what your modified code is actually doing and what you expect it to do?

I looked at the code and comments in your first post and noticed that you are replacing void run(uint8_t) from the Adafruit_MotorShield library with void setM1Speed(int speed) from the DualVNH5019MotorShield library. According to the library reference section for the Adafruit Motor Shield V2, run just controls the motors’ direction and state. However, setM1Speed, setM2Speed, and setSpeeds sets both the direction and speed of a motor channel in the DualVNH5019MotorShield library. The motor direction is determined by the sign of the speed number, which is what the conditional IF/ELSE IF statements are doing in your code.

You can condense this part of your code:

if(PWM_val1 > 0) direction1 = FORWARD;                // if(PWM_val1 > 0) direction1 = 400
else if(PWM_val1 < 0) direction1 = BACKWARD;      // else if(PWM_val1 < 0) direction1 = -400
if (rpm_req1 == 0) direction1 = RELEASE;                 // if (rpm_req1 == 0) direction1 = 0
if(PWM_val2 > 0) direction2 = FORWARD;                // if(PWM_val2 > 0) direction2 = 400
else if(PWM_val2 < 0) direction2 = BACKWARD;      // else if(PWM_val2 < 0) direction2 = -400
if (rpm_req2 == 0) direction2 = RELEASE;                 // if (rpm_req2 == 0) direction2 = 0
motor1->run(direction1);                                             // md.setM1Speed(direction1)
motor2->run(direction2);                                             // md.setM2Speed(direction2)
motor1->setSpeed(abs(PWM_val1));                         // md.setM1Speed(abs(PWM_val1))           this line is fine for me
motor2->setSpeed(abs(PWM_val2));                         // md.setM2Speed(abs(PWM_val2))           this line too

to:

if (rpm_req1 == 0)
   md.setM1Speed(0);
else
   md.setM1Speed(PWM_val1);
if (rpm_req2 == 0)
   md.setM2Speed(0);
else
   md.setM2Speed(PWM_val2);

I did not look at the rest of your Arduino sketch, since it is quite long. If you are still having trouble converting your code, please simplify your code as much as possible to where it should work, but does not, then post it here.

- Amanda

Thank you so much Amanda! that solved my problem. Now it behaves as it should. But only one motor is moving as before, precisely the M2. Everything is well connected, but M1 seems doesn’t recibe the commands. Anyways, I’ll keep searching where the problem is in the motor_controller3.ino file for driving the two motors, and thanks again :smiley:

As far as I read, interruptions 20 and 21 are the I2C pins and there are more people having problems with those interrupts on the arduino mega 2560 board. And those interrupts I’m using just for motor 1 (M1) which is the one that doesn’t move. Probably if I use pins 2 and 3 (interrupts 0 and 1) instead 20 and 21, work my problem out. But I’m not sure if I can, because M1INA (Motor 1 direction input A) uses the digital pin 2 on the dual VNH5019 board, and I need that pin and pin 3 in order to use interrupts 0 and 1. Is it safe trying to do that?
Thanks

I realize M1A and M1B on the VNH5019 board was solder in a wrong way, by testing with a simple demo example given by it library. Now this is perfectly working, so… problem solved!

From your description, it is not exactly clear why using the I2C pins on your Arduino Mega would cause motor 1 to not move, however, it sounds like you figured out the issue and fixed it; thanks for letting us know.

- Amanda