Thursday, 27 April 2017

H bridges, minor failure and troubleshooting

When I left off last time, I was raring to go with some sample PID code that allowed me to test out a basic movement between two points. I realised I hadn't included any wiring, and that's important for this post. Suffice to say, the code didn't work quite as I had intended, but not for obvious reasons. By that, I mean I spent hours trying to debug what was going on without success. Instead, I had to break out the oscilloscope and measure to find out what was really happening.

If you remember from the last post, I'd utilised an H bridge to drive the motor, and that the H bridge had three inputs. Two inputs set the polarity of the two outputs, and the last input enabled or disabled the output transistors. I've thrown together a rough overview in the picture below. Things like power and encoder are deliberately omitted.

Rough hookup
The original code set up two digital outputs on the Seeduino Lite (Leonardo clone) to set the polarity of the motor. This way I could switch the logic state on the output and have the motor reverse direction. The PWM output was connected to the enable input on the H bridge. My plan had been to use the PWM to modulate the drive to the motor and thus control the speed.  I know that motors are inductive, but the H bridge module has some diodes to clamp the terminals at just about the supply and just below ground in order to protect the driver chip.

The first iteration of the code only used the two inputs on the H bridge, as I wanted a quick test to see I was in the right place. That seemed to go well. The second iteration used all three inputs, and the result was less than great. What I got was a noisy, jerky drive from the motor, with lots of vibration and hunting as the PID loop tried to keep the carriage in position.

Now, as I'd made some code changes, I immediately blamed my code changes, even though they shouldn't have caused an issue. I went back to a simpler version of the code, and that all worked well - leaving me scratching my head. Reflashing the updated code demonstrated the same noisy, jerky, vibrating drive that I'd seen.

Wondering what was going on, I used some LEDs to monitor the output from the Seeduino Lite, but they seemed to indicate that the outputs from the Seeduino were as I would have expected them to be. Hooking up an oscilloscope to one of the drive lines and the PWM line netted me this picture:

Input A and PWM lines
The green trace is the PWM line, and the cyan line is one of the two direction lines on the H bridge (they're blue where they overlap). This all looked like I'd expected! How was it possible that the motor was acting so badly, when the outputs from the Seeduino were as expected? Well, hooking up the oscilloscope across the motor terminals soon showed what was going on, and it wasn't at all what I'd thought might be happening.

Motor terminals

The green trace is one side of the motor referenced to ground, and the cyan trace is the other (apologies - colours are reverse from the previous image). I was expecting to see the green trace stay grounded while the cyan trace pulsed on and off at the same speed as the PWM signal. What I got instead was the two output channels switching polarity, driving the motor in the opposite direction! The H bridge was the source of the heavy vibration and jerkiness I was seeing. It's also odd to see that there are differences in the first and second set of pulses. So, using the enable input to PWM modulate the power to the motor was causing this strange response from the H bridge. Obviously this isn't how I'm going to get things happening.

Now, if I had read some of the links I'd posted in my previous post - especially those from Modular Circuits' H-Bridges series, I would have saved myself a lot of trouble. However, I finished writing the previous post after I discovered the issue and wanted to save you, dear reader, the trouble I encountered!

No comments:

Post a Comment