PWM Frequency - Dual VNH5019 Shield & Arduino Library

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

PWM Frequency - Dual VNH5019 Shield & Arduino Library

Postby Bob101 » Sun Aug 19, 2012 10:04 am

I recently purchased the Dual VNH5019 Motor Driver Shield for Arduino.

It works perfectly with my Arduino Uno using the pololu Arduino library for the board. My motors operate very quietly, there is no PWM type buzzing, I assume this is because of the 20kHz "ultrasonic" operation.

However, using the Arduino Mega2560 R3 everything functions as with the Arduino Uno except that the motors make a distinct buzzing sound well within audible range. This buzzing sounds very much like a low kHz PWM type buzzing. I have the shield attached as with my Arduino Uno so M1PWM & M2PWM are attached to pins 9 & 10 on the Arduino.

Am I correct in thinking that this buzzing is due to timers on the Mega2560 R3 differing from those on the UNO? For example, I think that pins 9 & 10 are now on timer2 rather than timer1 as with the Uno (according to: http://arduino.cc/forum/index.php?topic=72092.0).

If this is correct would it be possible for the library to be updated to detect the ATmega2560 and alter timer2 in place of timer1?

Alternatively, since your library alters timer1 I could connect M1PWM & M2PWM to pins 11 & 12 which I believe are now controlled by timer1.
Bob101
 
Posts: 7
Joined: Sat Nov 05, 2011 5:08 pm

Re: PWM Frequency - Dual VNH5019 Shield & Arduino Library

Postby ryantm » Mon Aug 20, 2012 10:51 am

Hi, Bob.

Yes, that buzzing sound is most likely the PWM whine. Unfortunately, timer 2 is an 8-bit timer and there is no good configuration that gives you a frequency close to an ultrasonic PWM while being below the driver's 20kHz maximum. If you want an ultrasonic PWM, you will need to remap the PWM pins to one of the 16-bit timers on the Mega (like timer 1 as you suggest), and modify the library accordingly. Please note that the nSF pin is currently mapped to pin 12.

Please let me know if you need assistance with the remapping or code modifications, because I would be glad to help.

- Ryan
User avatar
ryantm
Pololu Employee
 
Posts: 1268
Joined: Mon May 19, 2008 12:00 pm
Location: Las Vegas, NV

Re: PWM Frequency - Dual VNH5019 Shield & Arduino Library

Postby effgee » Mon Aug 20, 2012 5:18 pm

Hi Ryan

I'm about to do the same thing, VNH5109 board on a Mega, glad I read this post.

I'm certainly no guru in C/C++, so I'd greatly appreciate if you could update your library to recognise and handle the Mega.

Will I also need to make/change jumpers on the driver board ?
effgee
 
Posts: 38
Joined: Mon Aug 20, 2012 12:30 am

Re: PWM Frequency - Dual VNH5019 Shield & Arduino Library

Postby effgee » Tue Aug 21, 2012 6:18 pm

(bump)

Hi Ryan

I don't know about Bob, but I definitely need assistance with the remapping / code modifications to suit the Mega.

I found this http://arduino.cc/forum/index.php?topic=77168.0 but I'm unclear on a couple of things:
1/ how do I "reassign MIPWM to Arduino digital pin 11 and M2PWM to Arduino digital pin 12"? - is that in software or hardware ?
2/ I'd really prefer if the 'official' library was updated on github https://github.com/pololu/Dual-VNH5019-Motor-Shield rather than me trying to track my own personal versions, since I'm likely to use both types of boards. Seems there are several people suffering from the same thing, thanks to Arduino being inconsistent with their pin assignments.

thanks in advance,
Michael
effgee
 
Posts: 38
Joined: Mon Aug 20, 2012 12:30 am

Re: PWM Frequency - Dual VNH5019 Shield & Arduino Library

Postby ryantm » Wed Aug 22, 2012 8:48 am

Hi, Michael.

Just to be clear, the motor shield works with the Arduino Mega 2560 R3 by default without any extra configuration or modifications. The issue in this thread is that some people prefer to get an ultrasonic PWM frequency. Have you already determined that the low-frequency PWM is unacceptable to you?

- Ryan
User avatar
ryantm
Pololu Employee
 
Posts: 1268
Joined: Mon May 19, 2008 12:00 pm
Location: Las Vegas, NV

Re: PWM Frequency - Dual VNH5019 Shield & Arduino Library

Postby effgee » Wed Aug 22, 2012 4:07 pm

Hi Ryan

I tried it yesterday, and its borderline at best, but only because I've got such a noisy motor. The pwm squeal is almost as loud as the motor now, and the person I'm building this for wants it to be quiet, so I know once they source a quieter motor they will be complaining about the noise.

I guess my other option is to mount the board separately (not as a shield) and connect via jumpers to the row of General-Purpose Motor Driver connections, but I'd really prefer to use it as a shield plugged into my mega. I'm just unclear how to do that, I gather I need to:
1. cut some traces and use jumpers on the pin re-assignment block so pwm's come from pins connected to a 16 bit timer (since default 9 & 10 are connected to an 8 bit timer on the Mega), and
2. change the library to reflect pin and timer re-assignment.

I'm drifting a long way from the "buy a shield, plug and play" dream. Reading http://www.mythic-beasts.com/~markt/ATmega-timers.html suggests maybe timer3 on pins 3 & 5 (otherwise free with the VNH5019 shield) could be a good choice for minimum change. Will this work ?

thanks
effgee
 
Posts: 38
Joined: Mon Aug 20, 2012 12:30 am

Re: PWM Frequency - Dual VNH5019 Shield & Arduino Library

Postby ryantm » Fri Aug 24, 2012 9:22 am

Sorry for the delay in replying; I know it can be frustrating when you expect things to be plug and play and they do not perform up to your expectations. We had to make some trade off with the pin assignment because Arduino unfortunately did not connect the same kind of timer to the pins of the Mega.

I think that using Timer 3 should work, but I have not had a chance to try it myself yet.

Yes, your understand of reassignment is correct. There is a section in the user's guide called "Remapping the Arduino Connections", which has more information.

Here is a general description of the changes you'd have to make to the library code:
  • In DualVNH5019MotorShield.h: _PWM1 and _PWM2 would need to changed to the newly assigned pins
  • In DualVNH5019MotorShield.cpp: All the code inside the "#if defined(__AVR_ATmega168__)|| defined(__AVR_ATmega328P__)" blocks would have to be used instead of analogWrite, and if you used timer 3 instead of Timer 1, you'd have to change registers like TCCR1A to TCCR3A.
I would like to make a better way to configure the library, but I have not been able to do that yet.

- Ryan
User avatar
ryantm
Pololu Employee
 
Posts: 1268
Joined: Mon May 19, 2008 12:00 pm
Location: Las Vegas, NV

Re: PWM Frequency - Dual VNH5019 Shield & Arduino Library

Postby effgee » Wed Aug 29, 2012 5:05 am

Hi Ryan

thanks for the reply, no drama with the delay since I've been dragged off onto other jobs for the last week, so haven't had a chance to even read your reply until now.

Guess I'll have a shot at modding the software myself, and will post the result (hopefully success!)
effgee
 
Posts: 38
Joined: Mon Aug 20, 2012 12:30 am

Re: PWM Frequency - Dual VNH5019 Shield & Arduino Library

Postby effgee » Thu Sep 06, 2012 3:44 pm

OK, I think I've got it sorted for the Mega on pins 3 & 5, but this same library wont work with both Mega and other boards. Someone that knows more about compiler directives may be able to sort it.

Here is the code that works with the Mega:

DualVNH5019MotorShield.h
Code: Select all Expand


DualVNH5019MotorShield.cpp
Code: Select all Expand


Here is my attempt to have a single library that works with Uno/Duemilenova and Mega:

The problem is that only one channel lights up, pin 5 gets pwm, but pin 3 doesn't. I've run out of time to get this working 'universally', I just have to run with 2 versions of the library (the original and the one above).

Would love to see someone solve this.

DualVNH5019MotorShield.cpp
Code: Select all Expand
effgee
 
Posts: 38
Joined: Mon Aug 20, 2012 12:30 am

Re: PWM Frequency - Dual VNH5019 Shield & Arduino Library

Postby siattasm » Mon Nov 12, 2012 6:35 am

Hi effgee,

Would this mean that instead of connecting the motor driver to pins 9 & 10 (for PWM) one would connect them to pins 3 & 5 instead leaving all other pin connections as they are? Sorry if the question is extremely obvious but I am a beginner with regards to this subject.

Many thanks in advance,
siattasm
siattasm
 
Posts: 2
Joined: Mon Nov 12, 2012 6:31 am

Re: PWM Frequency - Dual VNH5019 Shield & Arduino Library

Postby effgee » Thu Nov 15, 2012 4:28 pm

siattasm wrote:instead of connecting the motor driver to pins 9 & 10 (for PWM) one would connect them to pins 3 & 5 instead leaving all other pin connections as they are?


That's what I did, i.e. cut the tracks and solder in jumper wires. However, I never did get it working properly, ended up damaging the drive card somehow (it now flags a motor fault if I exceed about 1500mA on channel 2). For a while I was using a stepper motor instead of the brushed motor on channel 2, hence how I thought I had it working OK (since I wasn't using the problematic 2nd channel).

At my (low) level of expertise, I simply ran out of time to chase it down, so I ended up living with audible pwm noise. This was offset by the fact I ended up running at 100% duty cycle except during ramp up/ramp down. If you're a beginner, ask yourself do you really need ultrasonic (i.e. noiseless) pwm. If not, the shield should work off the bat, but just hum when driving.

If I get some time I'll have another look at it, but that is at least a week away.

Maybe if enough people ask for this a Pololu guru will step in and either solve it or say it can't be done. Its not Pololu's fault it doesn't work, they've at least provided jumers to facilitate pin changes. It's really a result of careless(?) pin re-assignment by the Arduino designers when laying out the Mega. Maybe it was unavoidable due to track routing restrictions/lack of layers on the pcb, but it sucks that sketches using timers aren't portable between boards, seems to go against the idea of the simplified IDE.
effgee
 
Posts: 38
Joined: Mon Aug 20, 2012 12:30 am

Re: PWM Frequency - Dual VNH5019 Shield & Arduino Library

Postby Claire » Mon Nov 19, 2012 10:23 am

A modified version of the VNH5019 shield library that allows for a 20kHz PWM frequency with the Arduino Mega is available in this thread.

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


Return to Pololu Motor Controllers/Drivers and Motors

Who is online

Users browsing this forum: No registered users and 4 guests