Modified VNH5019 shield library for 20kHz PWM with Mega

Support for Pololu motor controllers/drivers and our motors and gearmotors.

Modified VNH5019 shield library for 20kHz PWM with Mega

Postby Claire » Fri Nov 16, 2012 6:08 pm

A number of people have been asking for a version of the DualVNH5019MotorShield library that can do 20kHz PWM frequencies with the Arduino Mega. To address this, we have made a modified version of our DualVNH5019MotorShield library that lets you use the Dual VNH5019 Motor Driver Shield with an Arduino Mega to drive motors with a 20kHz PWM. Using the 20kHz PWM should eliminate the whining sound heard when PWMing at lower frequencies, like the Arduino's standard PWM frequency of 500Hz. This library uses pins 11 and 12 of the Arduino, which are connected to one of the Mega's 16bit timers, timer 1.

To use this library you will need to either make some hardware modifications, or use the driver without plugging it in as a shield. As mentioned above, M1PWM and M2PWM were moved to pins 11 and 12 respectively. In addition, M2EN/DIAG was moved from pin 12 on the Arduino to pin 5. All other connections between the board and shield are the same.

Hardware modifications can be made by cutting traces on the shield and running a wire from a different Arduino pin to the appropriate trace, as explained in the user's guide.

The new version of the library should be compatible with code written to use the old version, but the include statement will need to be changed.

DualVNH5019MotorShieldMega.h
Code: Select all Expand


DualVNH5019MotorShieldMega.cpp
Code: Select all Expand
Attachments
DualVNH5019MotorShieldMega.zip
(3.06 KiB) Downloaded 184 times
Last edited by Claire on Mon Mar 25, 2013 6:05 pm, edited 1 time in total.
Reason: Made corrections specified below to the library.
User avatar
Claire
Pololu Employee
 
Posts: 191
Joined: Mon Jun 04, 2012 9:01 am
Location: Las Vegas, NV

Re: Modified VNH5019 shield library for 20kHz PWM with Mega

Postby effgee » Sat Nov 17, 2012 2:42 am

Thanks Claire.

We especially appreciate you staying back late on a Friday to sort this out.

I was just having a look myself, and it appears my previous attempt to get it working on Timer 3 (pins 3 & 5, chosen to minimise the number of track cuts and jumpers required) was foiled by some Arduino bug; I loaded this simple test code
Code: Select all Expand
and put my scope on pins 2, 3 & 5.
Pin5 shows 12.5% duty cycle, 20kHz pwm, Pin2 shows 25%, but pin 3 (timer3C) just sits on zero volts. I suspect its a shortcoming of the Arduino IDE.
effgee
 
Posts: 38
Joined: Mon Aug 20, 2012 12:30 am

Re: Modified VNH5019 shield library for 20kHz PWM with Mega

Postby effgee » Sat Nov 17, 2012 3:33 am

OK, now I get it.

On reading up the ATmega docs at http://www.atmel.com/devices/atmega2560 ... =documents, I worked out that channel C simply wan't being enabled for pwm.
To correct this, we need to write a different magic number to timer control register A:
Code: Select all Expand

This gem should allow successful running ultrasonic pwm on pins 3 & 5, which aren't already used, so only 2 jumper wires and 2 track cuts are needed.
effgee
 
Posts: 38
Joined: Mon Aug 20, 2012 12:30 am

Re: Modified VNH5019 shield library for 20kHz PWM with Mega

Postby Launchpad » Tue Dec 18, 2012 8:51 pm

Thank you for the work on this. I was unable to make this work. I'm trying to use this shield and an XBee shield. I have remapped the RX/TX lines of the XBee (2,3) to (10,13) which works fine with softwareSerial. I bent the stackable header leads of 2,3,5 on the XBee out of the way. I remapped pins 11,12 to 2,5 on the motorDriver board, and bent pins 2,5 out of the way on the motorDriver board. I changed the include to be for the Mega, and placed the libraries in the Arduino library folder.

From here, I thought I'd be able to use the same commands as before (dm.setM1Speed(0)) but the motor just spins as if set to full speed. Did I miss something above?
Launchpad
 
Posts: 3
Joined: Tue Dec 18, 2012 8:37 pm

Re: Modified VNH5019 shield library for 20kHz PWM with Mega

Postby Claire » Thu Dec 20, 2012 3:11 pm

It sounds like you did not remap the right pins for using the library given above. Could you list what each of the pins on your Arduino is connected to? Also, could you post a picture of the remapping?

-Claire
User avatar
Claire
Pololu Employee
 
Posts: 191
Joined: Mon Jun 04, 2012 9:01 am
Location: Las Vegas, NV

Re: Modified VNH5019 shield library for 20kHz PWM with Mega

Postby Launchpad » Thu Dec 20, 2012 10:20 pm

Thank you for the very fast reply. Here's the exact remapping that I did:

The motor driver shield sits on the Mega. The Xbee shield sits on the motor driver shield.

XBee shield: Bent the through stackable header pins 2,3,5 out of the way. With solid core wire, via the stackable headers, mapped pins 10,13 to pins 2,3 on the XBee shield for RX/TX using SoftwareSerial.

MotorDriver shield: Bent through stackable header pins 2,5 out of way. With solid core wire, mapped pins 11,12 to 2,5 on the motor driver shield.

So... now...
pins 2 and 5 from the MEGA do not continue up the boards at all.
pins 2,3, and 5 do not continue up from motor driver shield to xbee shield.

I thought that this was right, as based on the libraries, the motor driver shield needs pins 2,3,5 for PWM and pins 11,12 now remap for pins 2,5.

Normally, the Xbee needs pins 2,3 for RX/TX, but those are now remapped to pins 10,13.

I'll post a photo as soon as my phone(camera) comes back online.
Launchpad
 
Posts: 3
Joined: Tue Dec 18, 2012 8:37 pm

Re: Modified VNH5019 shield library for 20kHz PWM with Mega

Postby Claire » Fri Dec 21, 2012 6:02 pm

To use the DualVNH5019MotorShieldMega library you should connect the M1PWM and M2PWM lines from the shield to pins 11 and 12 on the Arduino and the M2EN/DIAG line to pin 5 on the Arduino. This can be done by cutting the traces going to pins 9, 10, and 12 on the bottom of the shield and soldering a wire to connect the PWM pins to pins 11 and 12 and the M2EN/DIAG pin to pin 5 on the outside edge of the shield as described in the "Remapping the Arduino Connections" section of the user's guide.

If you are able to post a picture of your setup, I might be able to help you further.

However, please not that this modification is only for using the modified version of our VNH5019 library that is given at the top of this thread, and you do not need to use this library to use the VNH5019 shield with an Arduino Mega. This library should only be used if you want to run your motors with a 20kHz PWM to eliminate the motor noise.

-Claire
User avatar
Claire
Pololu Employee
 
Posts: 191
Joined: Mon Jun 04, 2012 9:01 am
Location: Las Vegas, NV

Re: Modified VNH5019 shield library for 20kHz PWM with Mega

Postby Launchpad » Fri Dec 21, 2012 8:57 pm

Thank you for the explicit clarifications. I got a little confused by the thread but the original and these instructions make more sense and work just fine for me now. I wanted this ultrasonic PWM library so that I could remove the loudly audible PWM whine coming from my motors. I've just used simple H-bridge circuits in the past and there are nicely built functions to set the PWM frequency on Arduino PWM pins, but this motor driver shield is very handy and quite powerful. Thanks for the speedy and clear help. Cheers.
Launchpad
 
Posts: 3
Joined: Tue Dec 18, 2012 8:37 pm

Re: Modified VNH5019 shield library for 20kHz PWM with Mega

Postby Connor » Fri Mar 22, 2013 6:19 pm

Hello,

I'm working with the VNH5019 on a Mega 1280. I've found that when I set the speed to 399, I only get around 62 to 64% Duty Cycle. When I set it to 400, I get 100% Duty Cycle. What am I missing? Were did my other 40% go?
Connor
 
Posts: 5
Joined: Fri Mar 22, 2013 6:17 pm

Re: Modified VNH5019 shield library for 20kHz PWM with Mega

Postby Connor » Fri Mar 22, 2013 9:10 pm

Okay. Update.. I did some massive testing tonight with the Library.. I changed the max PWM value to 625. I ran a looping test that count from 0 to 625. The duty cycle on my oscilloscope spike at a 100% when the counter hit 400 and 401... then resumed normal progression at 402 up through 625. I'm not sure what's magical about the 400 and 401 values for the PWM.. but, what ever the reason.. those 2 numbers cause the issue.. I wrote some extra code to make it skip those two numbers and my program works fine now. Anyone have ANY insight into why this may be? Where did that initial 400 value come from? I find it strange that that's the default top PWM value and that's the value that causes the duty cycle to spike.. Could they're be something strange with the timer values and some sort of strange relationship with 20Khz ??

Thanks, Connor
Connor
 
Posts: 5
Joined: Fri Mar 22, 2013 6:17 pm

Re: Modified VNH5019 shield library for 20kHz PWM with Mega

Postby Ben » Fri Mar 22, 2013 11:16 pm

Hello, Connor.

Are you getting the same behavior with both channels?

- Ben
User avatar
Ben
Pololu Employee
 
Posts: 3454
Joined: Mon Aug 28, 2006 12:05 pm
Location: Las Vegas, NV

Re: Modified VNH5019 shield library for 20kHz PWM with Mega

Postby Connor » Sat Mar 23, 2013 11:45 am

Yes, I am. I was trying to figure out where the "400" came from to begin with.. I couldn't figure that out.. But, like I said, at 399 it's round 62-64% duty, 400 and 401 it spikes to 100%, 402 it picks back up at the 64-65% mark and is good all the way up till it hits 625 which is 100% duty again.. What's stranger is.. I would think it would be 512 or 1024 for the top value for the PWM depending on if it was a 9 bit or 10 bit counter for the clock....
Connor
 
Posts: 5
Joined: Fri Mar 22, 2013 6:17 pm

Re: Modified VNH5019 shield library for 20kHz PWM with Mega

Postby Ben » Sat Mar 23, 2013 7:51 pm

A top value of 400 produces a 20 kHz PWM. What frequency do you see when you look at the PWM on your scope?

- Ben
User avatar
Ben
Pololu Employee
 
Posts: 3454
Joined: Mon Aug 28, 2006 12:05 pm
Location: Las Vegas, NV

Re: Modified VNH5019 shield library for 20kHz PWM with Mega

Postby Connor » Sun Mar 24, 2013 5:23 pm

Okay, Found the problem... issues is in the following areas..

#if defined(__AVR_ATmega168__)|| defined(__AVR_ATmega328P__)
OCR1B = speed;
#elseif defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
// Mega board specific stuff here - assumes assigning timer3, using pins 3 &5
OCR3B = speed;
#else
analogWrite(_PWM2,speed * 51 / 80); // default to using analogWrite, mapping 400 to 255
#endif

#elseif isn't valid, it should be #elif

also, OCR3B should be OCR1B.. The elseif problem is throughout the file, but the OCR3B is only on line 116.

I think it was falling through to the analogWrite part of the code.. which would work correctly up to the 400 or 401 which = 255 when rounded up. analogWrite then must put the pin in a high state regardless of the PWM value.. which is why it was acting strangely.

Continuing on with the math.. 625 = 398, 626 & 627 = 399 and 628 & 629 = 400.. Which was the cut off value I found..

So, that explains the strange behavior. Please update your libraries to reflect the bug fixes. :)

One think that would be nice would be a easy way to switch the motor orders... In my setup. Do to wire lengths.. I had to go through and swap all the pins.. and the OCR1A and OCR1B.. and of course, the _PWM1 and _PWM2 have no effect with this library, as we're not using the analogWrite function.. (or shouldn't be)

Thanks, Billy
Connor
 
Posts: 5
Joined: Fri Mar 22, 2013 6:17 pm

Re: Modified VNH5019 shield library for 20kHz PWM with Mega

Postby Claire » Mon Mar 25, 2013 6:07 pm

Hi, Billy.

Thank you for pointing those mistakes out; I really appreciate it. You are right about it not recognizing the elseif statement and falling through to the else statement. I have tested the library with both of the changes you indicated and updated it in my original post. I also fixed the second code block in that post, which used to have the header file instead of the C++ file in it.

-Claire
User avatar
Claire
Pololu Employee
 
Posts: 191
Joined: Mon Jun 04, 2012 9:01 am
Location: Las Vegas, NV

Next

Return to Pololu Motor Controllers/Drivers and Motors

Who is online

Users browsing this forum: No registered users and 7 guests