FEZ Panda 2 crash on motor run

Hi All,

I’m very new to all this but i’m stuck for days allready so am hoping for some help. I have the FEZ Panda 2 controller running on the latest firmware. On top of that I stocked the fez connect shield and on top of that is the DFIRobot DFRduino L298p shield.

M1 and M2 are both connected to a 6v, 180rpm DC Geared motor. A 6pack AA batteries power my FEZ Panda 2. I’ve got the motor shield on “power from internal device”. I know it’s powering the engine’s because as the microcontroller boots they run at full speed.

But when I start controlling the engines with the FEZ Panda 2 it seems to freeze all the time. I’m using the following code to initialize:

            static PWM _pwm1;
            static OutputPort _dir1;
            static PWM _pwm2; static OutputPort _dir2;
            static sbyte _last_speed1, _last_speed2;

            static public void Initialize()
            {
                _pwm1 = new PWM((PWM.Pin)FEZ_Pin.PWM.Di5);
                _dir1 = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di4, false);
                _pwm2 = new PWM((PWM.Pin)FEZ_Pin.PWM.Di6);
                _dir2 = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di7, false);
            }

And there are functions to set the speed and direction as well:


            static public void Move(sbyte speed1, sbyte speed2)
            {
                if (speed1 > 100 || speed1 < -100 || speed2 > 100 || speed2 < -100)
                    new ArgumentException();
                _last_speed1 = speed1;
                _last_speed2 = speed2;

                if (speed1 < 0)
                    _dir1.Write(true);
                else
                    _dir1.Write(false);

                _pwm1.Set(1000, (byte)(Math.Abs(speed1)));

                ////////////////////////////       
                if (speed2 < 0)
                    _dir2.Write(true);
                else

                    _dir2.Write(false);
                _pwm2.Set(1000, (byte)(Math.Abs(speed2)));

            }
            static public void MoveRamp(sbyte speed1, sbyte speed2, byte ramping_delay)
            {
                sbyte temp_speed1 = _last_speed1;
                sbyte temp_speed2 = _last_speed2;
                while ((speed1 != temp_speed1) || (speed2 != temp_speed2))
                {
                    if (temp_speed1 > speed1)
                        temp_speed1--;
                    if (temp_speed1 < speed1)
                        temp_speed1++;
                    if (temp_speed2 > speed2)
                        temp_speed2--;
                    if (temp_speed2 < speed2)
                        temp_speed2++;
                    Move(temp_speed1, temp_speed2);
                    Thread.Sleep(ramping_delay);
                }

            }

MoveRamp accepts 3 parameters, one for the first engine speed, the second engine speed and the delay (so it’s increasing speed with a certain delay).

If I set MoveRamp with 100,100, 20 i can debug the FEZ. It run’s the MoveRamp loop about 20 times and then stops debugging and completely freezes. If I stop the program in C# and re deploy it’s nagging about a hardware problem.

Removing the power and booting the FEZ again helps to be able to deploy and debug again.

“Move” has a similar problem. It runs the function but does not proceed to the next function afterwards.

For instance:

Move(50,50);
Sleep(5000);
Move(20,20);

This should start driving for 5 seconds and then drive slower. However, FEZ freezes and the engines just keep running at the 50,50 speed.

I tried hooking up another battery pack to the motor controller and set the jumpers to “external power” but this does not change anything. You can find the motor shield manual here Arduino_Motor_Shield__L298N___SKU_DRI0009_-DFRobot

Really hope someone can help me out here / help troubleshoot. I don’t have any more clues on how to fix this issue.

Best regards,

Arjan.

Motors are notorious for making noise on power circuits that upset microcontrollers.

The first step I would take is to plug in a USB cable and debug your app. Where is it “breaking”? What diagnostics can you get out of it when you step through your code?

Second step I would do is to make sure you run the motors of external power; you can’t draw that much current from a set of batteries like you’re doing when you have it set to run from the “internal” source, and you’re probably seeing the voltage sag sufficently to cause the processor to behave unexpectedly.

Third thing I would do is find a better power supply than AA batteries - they are so unreliable on heavy current consumers like motors.

Fourth thing I would do is disconnect the motors themselves and make sure you can reliably run your app, and if so then you have basically confirmed that there’s a power issue in your circuit that you need to isolate the power input for Fez from the motors (except GND, you want that tied together everywhere). You might then have to look at adding capacitors to filter glitches.

Hi Brett,

Thank you so much for your quick response and taking the time for me!

If I connect a USB the program freezes at about the 20th time it loops the power increase. No debugging possible anymore after that. Sometimes this happens upon the 21th or 22th loop as well.

I did try powering the motor shield with an external battery pack as well (5x AA batteries). The microcontroller powered only by USB. Unfortunatly with the same result.

If I power the motors directly with the battery pack they run very fast. Seems like the batteries have enough power to drive them. I need this to be battery powered so the entire project will not be hooked to a power cable.

If I disconnect the motor shield the application runs fine.

Now for a real noob question (I’m a software guy :-). What do people mean by “using the same gnd”? Say I connect both the microcontroller and the motor shield with a separate battery pack. Should I connect both “-” wires together? One of my battery packs is using 5 AA batteries, the other one 6. Would that be a problem?

Again, thanks a lot for your help!!

Best regards,

Arjan.

When you’re debugging, and stepping through the code, at what statement does it crash?

Make sure you have not surpressed GC messages and see if anything is reported back. Add your own debugging to check progress.

You need to ignore the batteries as a short term step to make sure you get decent power. The problem isn’t how much power they can supply but how stable that supply is. You want to prove what is having an effect here and then deal with it.

You must not try to power the motors by the internal power; you must power them externally. The shield is aimed at Arduino, which is a 5v supply, not a 3v3 supply like the Fez chip needs. Perhaps they’re powering it from the 5v pin, but perhaps not. Still, you’re not going to be able to control that so the simplest way to deal with that is to power it from external source.

Tie GND together simply means making sure that there’s a common ground which can be as simple as making sure you join the - wires together somewhere. In your case, that can be as simple as a wire running from the - on the motor input of the shield to one of the female GND sockets on the shield

In almost every case I can remember that this kind of issue has come up, it’s been power and in particular motors drawing too much power from a shared source causing instability on the uC.

the other think i think you should do is disconnect the motors (like i suggested in the first instance). This can help show that there’s no fault when you’re actually controlling the shield, but you’re not drawing the extra current because of the motors

Thanks again for taking so much time to help me out. Really apreciate it. I’ve connected the - wires from both battery packs together. Weird thing is that I can now order the main program to start the engines, stop, reverse, spin, etc. All without crashing. So this was a major improvement.

As soon as I connect an IR receiver, LED, speaker and motor encoders and make the robot drive on IR commands it fails at random (totally freezes). Also, IR only seems to receive commands if it is powered b the USB plug from my laptop. It’s not doing anything when running on the battery alone.

If I disconnect the motors everythin is fine. Amazing, because the motors run on their own power supply and should not infect the microprocessor to my noob’s opinion.

There must be something else wrong and I also believe it has something to do with the power supply… but can’t get my finger on it.

So i’m stil confused but not giving up yet :-). I’m dismantling the entire thing now and start building it up from scratch again to make sure that everything is connected correctly.

Thanks again for all your help!!

Best regards,

Arjan.

So if you disconnect the motors the entire app including the IR code works fine?

Definetely power related.

I don’t know enough to help you here though, but you’re likely to be seeing a problem with noise on the power circuits, potentially you will need capacitors to help filter that.