Exception System.Exception - CLR_E_DIVIDE_BY_ZERO (1)

So I have my HydraBot built and the range sensor working the code I have does what I want it to but is throwing this error

#### Exception System.Exception - CLR_E_DIVIDE_BY_ZERO (1) ####
#### Message: 
#### Gadgeteer.Modules.GHIElectronics.MotorControllerL298::MoveMotorRamp [IP: 0026] ####
#### HydraBotWithRangeF.Program+<>c__DisplayClass1::<ProgramStarted>b__0 [IP: 007d] ####
#### Gadgeteer.Timer::dt_Tick [IP: 0018] ####
#### Microsoft.SPOT.DispatcherTimer::FireTick [IP: 0010] ####
#### Microsoft.SPOT.Dispatcher::PushFrameImpl [IP: 004a] ####
#### Microsoft.SPOT.Dispatcher::PushFrame [IP: 001d] ####
#### Microsoft.SPOT.Dispatcher::Run [IP: 0006] ####
#### Gadgeteer.Program::Run [IP: 001c] ####
#### HydraBotWithRangeF.Program::Main [IP: 001a] ####

A first chance exception of type ‘System.Exception’ occurred in GTM.GHIElectronics.MotorControllerL298.dll
Exception performing Timer operation

The motors do stop and start when there is something in front of the sensor at the right distance and the motors start back up when there is nothing in the way.


namespace HydraBotWithRangeF
{
    public partial class Program
    {
        GT.Socket.SocketInterfaces.AnalogInput ain5 = GT.Socket.GetSocket(14, true, null, null).AnalogInput5;
        GT.Timer RangFinderTimer = new GT.Timer(100);

        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            motorControllerL298.MoveMotor(MotorControllerL298.Motor.Motor1, 0);
            motorControllerL298.MoveMotor(MotorControllerL298.Motor.Motor2, 0);
            double v;
            double r;
            int d = 11;
            var timer = new GT.Timer(100);
            timer.Tick += timer1 =>
            {
                v = ain5.ReadVoltage();
                r = (v / 0.009766);
           // Debug.Print((r.ToString()));
                if (d < r)
                {
                    Debug.Print("greater than 11");
                    motorControllerL298.MoveMotorRamp(MotorControllerL298.Motor.Motor1, 100, 5000);
                    motorControllerL298.MoveMotorRamp(MotorControllerL298.Motor.Motor2, 100, 5000);
                }
                else
                {
                    Debug.Print("less than 11");
                    motorControllerL298.MoveMotorRamp(MotorControllerL298.Motor.Motor1, 0, 1000);
                    motorControllerL298.MoveMotorRamp(MotorControllerL298.Motor.Motor2, 0, 1000);
                }
            };

            timer.Start();
           

            {

            }
        }
    }
}


do I have the

"if "

in the wrong place

I think you’ve discovered a bug in the MotorControllerL298 driver. Check the Gadgeteer Codeplex site and see if you can download the source and use it to debug with.

I suspect the problem is in either the MoveMotor() or MoveMotorRamp() calls where you have some zero parameters. I’ve never used that module. Are you sure that zero is a valid parameter value? Maybe try a 1 and see if it goes away.

I took a quick look at the MotorController code and I suspect that the issue is that you are calling MoveMotorRamp with a target speed of ‘0’ on subsequent timer ticks.

For example, your timer tick fires and r <= 11, so you call the code that sets that ramps the motor speed down to 0, then 100ms later your timer fires again and r <= 11 so you again call the function to ramp the motor speed down to 0. It is this second call that I think is causing your problem. I would suggest that you rather set a target speed variable and then only if that differs from the original value do you call the MoveMotorRamp function.

As @ Ianlee74 has said, this looks like an issue with the driver. The MoveMotorRamp function has at least one place that is subtable to ‘division by zero’ errors if the current speed is the same as the new speed.

From MotorControllerL298_42.cs, line 290


// Determine how long we need to wait between move calls.
timeStep = _rampingDelayMilli / (_newSpeed - lastSpeed); 

If _newSpeed = lastSpeed you will have a divide by zero.

The way I understand the Ramp to work is set the speed and then how long in milliseconds to reach that speed after I went to bed I had an idea that my timer is on 100 but the Ramp to 0 is 1000 and that the timer tick may fire before the Ramp has had time to finish. I am going to reset some numbers and see what happens.