Issue with Basic Arduino Multi-tasking (Need answers ASAP PLEASE)

Thread Starter

John A Bonilla

Joined Mar 11, 2017
92
Hello Everyone,

Here I am again asking for help with this code, however I have narrowed down the issue to basically the Arduino Mega not being able to multi task. The basic thing that I'm asking in the code is for the arduino to just read the mouse data, not to use it or display it to me in any way. While spinning the motors continuously. The issue is that, when doing this I end up with only one motor receiving power to spin while the other doesn't. Maybe I have done something wrong, maybe theirs and issue with the arduino (I tested this on two other ones, with the same problem though) or maybe theirs an issue with the motor controller shield (however this seems to spin the motors fine when its told to just spin them by itself) or maybe its something else. If anyone has any suggestions on how to go about this, I would be very thankful!

My code:

C:
#include "DualVNH5019MotorShield.h"
#include <ps2.h>

PS2 mouse(6, 5);
DualVNH5019MotorShield md;

char mstat;
char mx;
char my;

void setup() {
  // put your setup code here, to run once:

Serial.begin(9600);md.init();

}

void loop() {
  // put your main code here, to run repeatedly:


  mouse.write(0xeb);  // give me data!
  mouse.read();      // ignore ack
  mstat = mouse.read();
  mx = mouse.read();
  my = mouse.read();

  md.setSpeeds(400, 400);
}
Mod edit: code tags
 
Last edited by a moderator:

philba

Joined Aug 17, 2017
959
OK, to start with, have you tried this without the mouse code? (I think you said so, just checking to make sure)

Have you googled to see if people have issues with either driver/library?

Have you looked through the code for both libraries?
 

philba

Joined Aug 17, 2017
959
I can state categorically that there is no inherent reason why the processor cannot implement multi-tasking.
That is quite true though Arduino software is pretty much single threaded. A driver that is poorly written can cause all sorts of problems. My money is on the PS2 mouse driver code. Though I couldn't see any examples of dual motor code on the pololu site.
 

Thread Starter

John A Bonilla

Joined Mar 11, 2017
92
OK, to start with, have you tried this without the mouse code? (I think you said so, just checking to make sure)

Have you googled to see if people have issues with either driver/library?

Have you looked through the code for both libraries?
Also, have you made sure that the default pin assignments between the libraries are ok.
That is quite true though Arduino software is pretty much single threaded. A driver that is poorly written can cause all sorts of problems. My money is on the PS2 mouse driver code. Though I couldn't see any examples of dual motor code on the pololu site.
Thank you for your suggestions! I have tested the code with just the motors spinning and it works fine. I do find your theory of the pins being overlapped interesting, and I will be taking a look at it. However within the library file, the code reports that the pwm pins 9 and 10 are being used. The motor shield I am using is:

https://www.pololu.com/product/2507/resources

and the library can be found here:

https://github.com/pololu/dual-vnh5019-motor-shield

If you go into the .h file, that is where I found the pin outs, however I may be wrong as it says in one of the comments that its a user defined pin selection. The code that I am using with the mouse is plugged into pwm pins 5 and 6 which shouldn't result in any conflicts but I will be sure to test it.
 

AlbertHall

Joined Jun 4, 2014
12,328
I'm not sure I understand what you mean by using the same timings.
The Arduino Mega has 6 timers. Timer 0 is set up to maintain the millis() function. Other things may use the other timers. Both the motor control and the mouse libraries may well use a timer. There will be problems if they are both trying to use the same timer.

You might try swapping lines 4 and 5 and see what happens.

http://www.robotshop.com/letsmakerobots/arduino-101-timers-and-interrupts
 

Thread Starter

John A Bonilla

Joined Mar 11, 2017
92
The Arduino Mega has 6 timers. Timer 0 is set up to maintain the millis() function. Other things may use the other timers. Both the motor control and the mouse libraries may well use a timer. There will be problems if they are both trying to use the same timer.

You might try swapping lines 4 and 5 and see what happens.

http://www.robotshop.com/letsmakerobots/arduino-101-timers-and-interrupts
Thank you, I'll look further into that as well however, i'm not using any timing commands so I'm not sure if that would change anything, never the less I will do my research.
 
Thank you for your suggestions! I have tested the code with just the motors spinning and it works fine. I do find your theory of the pins being overlapped interesting, and I will be taking a look at it. However within the library file, the code reports that the pwm pins 9 and 10 are being used. The motor shield I am using is:

https://www.pololu.com/product/2507/resources

and the library can be found here:

https://github.com/pololu/dual-vnh5019-motor-shield

If you go into the .h file, that is where I found the pin outs, however I may be wrong as it says in one of the comments that its a user defined pin selection. The code that I am using with the mouse is plugged into pwm pins 5 and 6 which shouldn't result in any conflicts but I will be sure to test it.
Have you investigate the alternative drivers specifically for the Mega? According to this thread , the normal driver should work with the Mega, but I can see that they remapped pins in the "better" driver below (and modified the board).

https://forum.pololu.com/t/modified-vnh5019-shield-library-for-20khz-pwm-with-mega/5178


Hope it helps.
 

AlbertHall

Joined Jun 4, 2014
12,328
Your program may not directly use any timers but the PWM almost certainly does. It needs to set an output high then low for specific periods of time. Therefore the library will use a timer to set these time periods. The mouse library may need to measure the width of pulses or the time between pulses and in that case it also may use a timer. This is fine as long as the two libraries use different timers but they may be trying to use the same timer. You may be able to find some documentation that will tell you what resources these libraries use but it may be that the only to find out is to look into the actual library code to see just what it does.
 

Papabravo

Joined Feb 24, 2006
21,020
That is quite true though Arduino software is pretty much single threaded. A driver that is poorly written can cause all sorts of problems. My money is on the PS2 mouse driver code. Though I couldn't see any examples of dual motor code on the pololu site.
Multi-tasking and multi-threading are different things. As has been pointed out multiple tasks can be part of a single thread as long as there are no resource conflicts. You pretty much have to enforce the rules yourself since there is no OS to protect you.
 
Last edited:
Hmmm,
in your code you have:
PS2 mouse(6, 5);
That means the driver is using Pins 5 & 6.

The default values for the motors are:
Code:
:
DualVNH5019MotorShield:DualVNH5019MotorShield()
{
//Pin map
_INA1 = 2;
_INB1 = 4;
_EN1DIAG1 = 6;
_CS1 = A0;
_INA2 = 7;
_INB2 = 8;
_EN2DIAG2 = 12;
_CS2 = A1;
}
That means that both drivers are using pin 6 - Aha! Bad drivers, no donut for you.

Change the PS2 instance...
Code:
PS2Mouse:pS2Mouse(int clock_pin, int data_pin, int mode)
so that it does not use 6 or any pins that you see above.

That should do it, I would think (hope).
 
Last edited:

Thread Starter

John A Bonilla

Joined Mar 11, 2017
92
Hmmm,
in your code you have:
PS2 mouse(6, 5);
That means the driver is using Pins 5 & 6.

The default values for the motors are:
Code:
:
DualVNH5019MotorShield:DualVNH5019MotorShield()
{
//Pin map
_INA1 = 2;
_INB1 = 4;
_EN1DIAG1 = 6;
_CS1 = A0;
_INA2 = 7;
_INB2 = 8;
_EN2DIAG2 = 12;
_CS2 = A1;
}
That means that both drivers are using pin 6 - Aha! Bad drivers, no donut for you.

Change the PS2 instance...
Code:
PS2Mouse:pS2Mouse(int clock_pin, int data_pin, int mode)
so that it does not use 6 or any pins that you see above.

That should do it, I would think (hope).
I truly appreciate it, I don't have the robot with me now, but I will test it tmm. I really hope this works which it should! I will reply again to confirm it, or to see if something else went wrong
 

philba

Joined Aug 17, 2017
959
I'm not sure I understand what you mean by using the same timings.
That's a timer meaning the two drivers might be stepping on each other over that resource. With out reading the libraries, it's hard to say. A well written arduino library shouldn't touch shared hardware.
Multi-tasking and multi-threading are different things. As has been pointed out multiple tasks can be part of a single thread as long as there are no resource conflicts. You pretty much have to enforce the rules yourself since there in no OS to protect you.
Dude, I know the difference - 35 years writing operating systems. But there really are no rules in the Arduino world. Heck, there aren't even Ms to RTFM.
 
Top