Go to Post The competition is over, I have no other FIRST-related activities to pursue–WHAT ELSE AM I GOING TO DO WITH MY LIFE BUT BROWSE CHIEF DELPHI UNTIL JANUARY???? - David8696 [more]
Home
Go Back   Chief Delphi > Technical > Electrical
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
 
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 06-06-2012, 00:09
Greg McCoy's Avatar
Greg McCoy Greg McCoy is offline
boiler up!
FRC #3940 (CyberTooth)
Team Role: Engineer
 
Join Date: Feb 2002
Rookie Year: 2002
Location: Kokomo, IN
Posts: 484
Greg McCoy has a reputation beyond reputeGreg McCoy has a reputation beyond reputeGreg McCoy has a reputation beyond reputeGreg McCoy has a reputation beyond reputeGreg McCoy has a reputation beyond reputeGreg McCoy has a reputation beyond reputeGreg McCoy has a reputation beyond reputeGreg McCoy has a reputation beyond reputeGreg McCoy has a reputation beyond reputeGreg McCoy has a reputation beyond reputeGreg McCoy has a reputation beyond repute
Send a message via AIM to Greg McCoy
Re: Arduino PWM output

Quote:
Originally Posted by sand500 View Post
I dont know why this is happening but the duty cycle frequency was wrong, then why does it stop completely at 255?
The motor controller is behaving erratically because the PWM frequency and the duty cycle are wrong. The analogWrite() function is generating this PWM signal:


(from Arduino.cc)

...but your motor controller is expecting this:




The frequency of the signal in Hz is the number of pulses per second. The reciprocal of the frequency is the period, which is the time between the rising edges of the PWM signal. For your motor controller, this should be 50 Hz, or a 20 ms (millisecond) period. According to the Arduino documentation, the analogWrite() function produces a 490 Hz PWM signal, or a 2 ms period.


The duty cycle of the signal is the ratio between the pulse width and the period. For your motor controller, the pulse width is "mapped" as follows:

Full Forward: 2 ms pulse / 20 ms period = 10% duty cycle.
Neutral: 1.5 ms pulse / 20 ms period = 7.5% duty cycle.
Full Reverse: 1 ms pulse width / 20 ms period = 5% duty cycle.


As others have suggested, the Arduino servo library should be able to produce the correct signal. 180 degrees on a servo is the equivalent of "full forward" on a motor attached to the motor controller (0 degrees is full reverse, 90 is neutral, etc.)

Quote:
Originally Posted by sand500 View Post
When I hook up the pwm and ground on the arduino to a regular dc motor that you would find in an electronics kit, the motor works just like how pwm should.
The motor controller does just this - it generates a 0-100% duty cycle PWM signal to the motor, using a circuit called an "H-bridge" to reverse the voltage when changing directions.

Hope this helps...
  #2   Spotlight this post!  
Unread 06-06-2012, 00:15
slijin's Avatar
slijin slijin is offline
Pockets
AKA: Samuel Lijin
FRC #0694 (StuyPulse)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2010
Location: New York City
Posts: 537
slijin is a splendid one to beholdslijin is a splendid one to beholdslijin is a splendid one to beholdslijin is a splendid one to beholdslijin is a splendid one to beholdslijin is a splendid one to beholdslijin is a splendid one to behold
Re: Arduino PWM output

Quote:
Originally Posted by Greg McCoy View Post
The motor controller is behaving erratically because the PWM frequency and the duty cycle are wrong
Can't believe I overlooked that. Should've been the first thing I realized.
__________________

2010-12 CT Chairman's
2011 Galileo 5th seed
2010 NY Regional Winners
  #3   Spotlight this post!  
Unread 06-06-2012, 04:41
Greg Needel's Avatar Unsung FIRST Hero
Greg Needel Greg Needel is offline
REVving up for a new season
FRC #2848 (All-sparks)
Team Role: Engineer
 
Join Date: Jan 2002
Rookie Year: 2002
Location: Dallas, TX
Posts: 3,102
Greg Needel has a reputation beyond reputeGreg Needel has a reputation beyond reputeGreg Needel has a reputation beyond reputeGreg Needel has a reputation beyond reputeGreg Needel has a reputation beyond reputeGreg Needel has a reputation beyond reputeGreg Needel has a reputation beyond reputeGreg Needel has a reputation beyond reputeGreg Needel has a reputation beyond reputeGreg Needel has a reputation beyond reputeGreg Needel has a reputation beyond repute
Re: Arduino PWM output

I have a custom victor library for the arduino that was written for one of my projects. This takes care of the timing issues, and duty cycle issues. While the the stock servo library will work, there are things that will cause hiccups.

Edit, I have attached the files.


revictorlibsforarduino.zip

Greg
__________________
Greg Needel│www.robogreg.com
Co-founder REV Robotics LLC www.REVrobotics.com
2014 FRC World Champions with 254, 469, & 74

Last edited by Greg Needel : 06-06-2012 at 10:54.
  #4   Spotlight this post!  
Unread 08-06-2012, 16:59
sand500 sand500 is offline
Registered User
FRC #3540 (Wildcat Robotics)
 
Join Date: Jan 2011
Rookie Year: 2011
Location: United States
Posts: 81
sand500 is an unknown quantity at this point
Re: Arduino PWM output

Quote:
Originally Posted by Greg McCoy View Post
The motor controller is behaving erratically because the PWM frequency and the duty cycle are wrong. The analogWrite() function is generating this PWM signal:


(from Arduino.cc)

...but your motor controller is expecting this:




The frequency of the signal in Hz is the number of pulses per second. The reciprocal of the frequency is the period, which is the time between the rising edges of the PWM signal. For your motor controller, this should be 50 Hz, or a 20 ms (millisecond) period. According to the Arduino documentation, the analogWrite() function produces a 490 Hz PWM signal, or a 2 ms period.


The duty cycle of the signal is the ratio between the pulse width and the period. For your motor controller, the pulse width is "mapped" as follows:

Full Forward: 2 ms pulse / 20 ms period = 10% duty cycle.
Neutral: 1.5 ms pulse / 20 ms period = 7.5% duty cycle.
Full Reverse: 1 ms pulse width / 20 ms period = 5% duty cycle.


As others have suggested, the Arduino servo library should be able to produce the correct signal. 180 degrees on a servo is the equivalent of "full forward" on a motor attached to the motor controller (0 degrees is full reverse, 90 is neutral, etc.)



The motor controller does just this - it generates a 0-100% duty cycle PWM signal to the motor, using a circuit called an "H-bridge" to reverse the voltage when changing directions.

Hope this helps...
Thanks a lot, its a lot clearer to me now. I'll try Greg Needel's library and the servo library.
  #5   Spotlight this post!  
Unread 26-07-2012, 13:48
markgryzwa markgryzwa is offline
Registered User
FRC #3206 (Royal-T-Wrecks)
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2010
Location: MN
Posts: 30
markgryzwa is on a distinguished road
Re: Arduino PWM output

Here is some servo code that I put together to try to run some basic robot functions. I've not tested in out for polarity yet but the ideas are correct.

#include <Servo.h>

// global variables

// ------------- Servo setup ------------------------------
Servo myservo_left;
Servo myservo_right;

int left_pwm = 10; // set up the servo connections for the Jaguar controllers
int right_pwm = 9;
int rate = 0;


// ------- Spike relay control signals -------------------
int FWD = 1;
int REV = 2;
int OFF = 0;
int spike_white = 0; // pin 0 for spike signal wire
int spike_red = 1; // pin 1 for spike red wire


void setup() {
// put your setup code here, to run once:

myservo_left.attach(left_pwm);
myservo_right.attach(right_pwm);

pinMode(spike_white, OUTPUT);
pinMode(spike_red, OUTPUT);

}

void loop() {

// Set up the default
Spike(OFF);


// Make stuff happen
Forward(100, 1000); // move forward as a % of full speed for ms Foward(%*full speed,ms)
delay(1000);

Spike(FWD);
Reverse(100, 1000);
delay(1000);

Spike(REV);
Right(50, 100);
delay(1000);

Left(50,200);
delay(1000);

Spike(OFF);
Right(50, 100);
delay(6000);


}



//------------------ Subroutines ---------------------------

//-----------------------------------------------------------
void Forward(int rate, int duration) {

int spd = rate * 500; // set speed as a % of rate

myservo_left.writeMicroseconds(1500+spd);
myservo_right.writeMicroseconds(1500-spd);
delay(duration);
myservo_left.writeMicroseconds(1500);
myservo_right.writeMicroseconds(1500);
}

//-----------------------------------------------------------
void Reverse(int rate, int duration) {

int spd = rate * 500; // set speed as a % of rate

myservo_left.writeMicroseconds(1500-spd);
myservo_right.writeMicroseconds(1500+spd);
delay(duration);
myservo_left.writeMicroseconds(1500);
myservo_right.writeMicroseconds(1500);

}

//------------------------------------------------------------
void Left(int rate, int duration) {

int spd = rate * 500; // set speed as a % of rate

myservo_left.writeMicroseconds(1500+spd);
myservo_right.writeMicroseconds(1500+spd);
delay(duration);
myservo_left.writeMicroseconds(1500);
myservo_right.writeMicroseconds(1500);

}


//------------------------------------------------------------
void Right(int rate, int duration) {

int spd = rate * 500; // set speed as a % of rate

myservo_left.writeMicroseconds(1500-spd);
myservo_right.writeMicroseconds(1500-spd);
delay(duration);
myservo_left.writeMicroseconds(1500);
myservo_right.writeMicroseconds(1500);

}



//---------------------------------------------------------------
void Spike(int state) { //states can be 0=Off, 1 = FWD, 2=REV

switch (state) {
case 0:
digitalWrite(spike_red, LOW);
digitalWrite(spike_white, LOW);
break;

case 1:
digitalWrite(spike_red, HIGH);
digitalWrite(spike_white, LOW);
break;

case 2:
digitalWrite(spike_red, LOW);
digitalWrite(spike_white, HIGH);
break;

default:
digitalWrite(spike_red, LOW);
digitalWrite(spike_white, LOW);
break;
}

}
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 06:28.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi