MotorDriverL298 issue with HubAP5

Whenever I connect the L298 motor modules to the Hub, I keep getting

An unhandled exception of type ‘System.ArgumentOutOfRangeException’ occurred in GTM.GHIElectronics.HubAP5.dll
Additional information: The frequency is too low.

Does anyone know how to fix this? I’ve looked through the Forums and it says to set the frequency to 5000 but I’m unable to do that since the error occurs when the motor object is instantiated. Until it’s a valid object I can’t set the frequency on the motor driver. I’ve tried different Hub sockets which caused the same problem. There’s only 1 socket on the Raptor which wont work in my scenario either since I need to control 2 modules.

Anyone have any ideas?

Did you try what I noted in the other thread you started on this? The instantiation happens behind the scenes (in the Gadgeteer generated code). The L298 uses four pins, two PWM and two digital IOs to control direction, and a P socket has 3 PWM pins, so you could do a bodge job with the Raptor and breakout boards to get at least 3 PWMs on the one P socket, which will give you 3 motors; is that sufficient for your needs?

That won’t work for me. I need to control 4 motors in 4 model trains.

I downloaded the driver source for the Hub and the frequency is initially set to 1 and it raises an error if it’s <= than 1.45. There is a public method named Set() that lets you set the frequency and duty cycle but that is in a private class named PwmOutputImplementation() inside the module code. So I can’t figure out how you would set the Frequency on the Hub. I set it to a static value of 5000 and it starts up and the motors turn on. All I’m using the hub for is the 2 motor controllers so I probably don’t/won’t have any issues with changing the driver for the Hub.

All I’m using right now is 2 tiny DC motors to test the concept out. The speed control isn’t the greatest with these motors, I have speed changes at 0, .25, .5, .75 & 1. It makes the speed changes at all but .25. I can feel it trying but the motor isn’t moving. I also will need to deal with inertia and momentum with the trains when I start and stop them.

Hmmm.

I had no trouble with two motor driver modules on a hubap5 on my raptor, once I set the frequency to 5K, and set the max speed to no more than .85 as taylorZa suggested…

but I dont have access to that source at the moment

1 Like

according to the spec sheet there are 4 PWMs on a Raptor, so you should be able to use the L298’s directly if you needed to. The 4th PWM channel is on pin 9 of the GY socket, socket 16, which means you may be stymied if you have a RGB display :frowning: as that pin looks like it drives the backlight :frowning: :frowning: . Might have to go back to the AP5 for looking at how to deal with this. (The AP5 isn’t an often-discussed item from what I recall)

Some of the things you’re seeing (with the motor stalling at less than .25) is probably a function of the motors, so you will need to tune your own parameters to match what they will need to do in real world. Obviously once you get further…

@ mtylerjr well, like I said, I couldn’t find a way to set the Hub frequency. I thought about making the private class public but since I use the Hub for just the motors modifying the Hub driver code is fine for now. I may make the private class public down the road and see what happens. The thing was, I got the error when it tried to instantiate the motor object and died every time and setting the value above 1.0 was impossible, even if I could get the object created.

@ Brett Yeah, I am using an RGB Display for this project. Right now I’m using a T43 screen with built in touch, I’m waiting on a Newhaven screen and board from GHI to come so I have a bit more real estate to work with for now. Eventually I want to use a 17" touch monitor and have the Video Out module and a USB module for that day.

I haven’t heard from GHI on this either. The module driver may be incorrect as far as I know.

I figured the motors were the issue. I’m using some cheap 6VDC motors I got at Radio Shack and a battery holder and the batteries may be dying. So until I get the model train locomotives out of the closet and hook them up I can’t do a whole lot of testing with them.

Question to GHI:
Can someone test a pair of L298’s attached to a hubAP5?

@ DanCio - Sorry for the delay. If you can, add the Gadgeteer module driver source to your project manually from https://bitbucket.org/ghi_elect/gadgeteer/src/88bd3ab6f5ed02c3e1c524780182ea4f6901d091/Modules/GHIElectronics/HubAP5/HubAP5_43/?at=master instead of the assembly in the SDK.

https://www.ghielectronics.com/docs/122/gadgeteer-driver-modification can help.

After that, find line 413 “if (frequency <= 1.45) {”. Change that line so it looks like the code below:


if (frequency < 0.0) {
    throw new ArgumentOutOfRangeException("frequency", "The frequency cannot be negative.");
}
else if (frequency == 0.0) {
    period = 0.0;
    clockSource = IO60P16.CLOCK_SOURCE_367HZ;
}
else if (frequency <= 1.45) {
    throw new ArgumentOutOfRangeException("frequency", "The frequency is too low.");
}
else if (frequency <= 125.5) {
    period = 367.6 / frequency;
    clockSource = IO60P16.CLOCK_SOURCE_367HZ;
}
...

1 Like

Is this intention John that the last else if will never be reached because the previous one will always cause that to be valid?

@ Dave, no the last shown code there is just saying if it’s greater than 1.45 but less than or equal to 125.5 then do something… there’s a series of other Else If statements after this that just range checks the frequency and sets the appropriate clock source and period.

Are you sure that the logic would work like that. I don’t see it.

If for instance the value was 1.10 then the one that checks for < 1.45 would handle this and the < 1.25 would never be reached and skipped as it is after the < 1.45 check. They are ELSE IF statements not a separate series of IF statements.

@ Dave McLaughlin - I don’t see a check against 1.25 anywhere, only 125.5.

agreed - the second check is against 125.5, which is greater than the 1.45 of the earlier statement. A value of 1.6 will pass thru the first check as it’s over 1.45, and will trigger the less-than 125.5 check.

WHAT THE ****!! I need new glasses. I could have sworn it was 1.25 when I read it the first time.

Now that is making me paranoid. :-[

Sorry guys. I should pay more attention next time.

2 Likes

You are an old fart - you’re excused :slight_smile:

1 Like