To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.roboticsOpen lugnet.robotics in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Robotics / 3844
3843  |  3845
Subject: 
Re: Motor power? (LONG! with code example)
Newsgroups: 
lugnet.robotics
Date: 
Thu, 18 Feb 1999 04:55:04 GMT
Viewed: 
1091 times
  
(My apologies if this is a duplicate. I tried to send it to the lugnet news
server, but that derver doesn't seem to like HTML in news message)

Y'all,

Yesterday, I asked how to use motor power to control the motor's speed. The
answer that I got was that the RCX firmware uses an internal PWM (pulse
width modulation) technique to control power and possibly speed. I observed
that I didn't see much change in motor speed even under load when I played
with motor power.

So I created a little program with NQC (thanks Dave Baum!)  that illustrates
my concept. However, to do it effectively, I need to pulse between motor ON
and motor OFF. If I cycle betweem motor ON and motor FLOAT, I see basically
what I get with RCX.

With a higher speed execution environment such as legOS, the pulsing that
you see with RCX shouldn't even be noticeable. legOS provides a 1 ms
interrupt rate. Assuming that the motor runs at 6000 RPM, that is 100
rotations per second, or 10 interrupts (to change motor power/speed) every
rotation.

Enjoy.

Cheers,
danm
---------- Cut here ---------------------------------------
// if MY_PWM is defined, my own PWM motor control is used
// otherwise, use standard RCX motor power control
#define MY_PWM

int     powA;           // power setting for motor A
int     powC;           // power setting for motor C


// move robot in an ess shaped curve by varying the power to motors
// motors are controller with PWM (pulse width modulation)
//
//                 -
//                / \
//               |   |
//              /     \
//     -       -       -
//      \     /
//       |   |
//        \ /
//         -
//     |---|---|---|---|
//       |   |   |   |
//       |   |   |   |
//       |   |   |   -> Ramp A down and up
//       |   |   -----> Ramp C down and up
//       |   ---------> Ramp C down and up
//       -------------> Ramp A down and up
//
task main
{
        Fwd(OUT_A + OUT_C, OUT_FULL);

        // start with some power settings
        powA = OUT_FULL;
        powC = OUT_FULL;

#ifdef  MY_PWM
        // start motor PWM task
        start Motor;
#endif

        // for always, ...
        while (true) {
                // ramp motor A down to 0
                while (powA != 0) {
#ifndef MY_PWM
                        Fwd(OUT_A, powA);
#endif
                        Sleep(100);
                        powA -= 1;
                }

                // ramp motor A up to OUT_FULL
                while (powA != OUT_FULL) {
#ifndef MY_PWM
                        Fwd(OUT_A, powA);
#endif
                        Sleep(100);
                        powA += 1;
                }

                // ramp motor C down to 0
                while (powC != 0) {
#ifndef  MY_PWM
                        Fwd(OUT_C, powC);
#endif
                        Sleep(100);
                        powC -= 1;
                }

                // ramp motor C up to OUT_FULL
                while (powC != OUT_FULL) {
#ifndef MY_PWM
                        Fwd(OUT_C, powC);
#endif
                        Sleep(100);
                        powC += 1;
                }

                // ramp motor C down to 0
                while (powC != 0) {
#ifndef MY_PWM
                        Fwd(OUT_C, powC);
#endif
                        Sleep(100);
                        powC -= 1;
                }

                // ramp motor C up to OUT_FULL
                while (powC != OUT_FULL) {
#ifndef MY_PWM
                        Fwd(OUT_C, powC);
#endif
                        Sleep(100);
                        powC += 1;
                }

                // ramp motor A down to 0
                while (powA != 0) {
#ifndef MY_PWM
                        Fwd(OUT_A, powA);
#endif
                        Sleep(100);
                        powA -= 1;
                }

                // ramp motor A up to OUT_FULL
                while (powA != OUT_FULL) {
#ifndef MY_PWM
                        Fwd(OUT_A, powA);
#endif
                        Sleep(100);
                        powA += 1;
                }
        }
}

#ifdef  MY_PWM
// variables for motor PWM task
int     powCur;         // current power setting for motor task
int     accumA;         // power accumulator for motor A in motor task
int     accumC;         // power accumulator for motor C in motor task

// Motor task
//      This task works by modulating the motor power by the ratio
//              power / powerMax
//      This ratio is computed by successively subtracting
//      the numerator (power) from an accumulator. When the accumulator is
//      positive, the motor is turned off. When the accumulator underflows
//      (becomes negative), the denominator (powerMax) is added to the
//      accumulator and the motor is turned on. When this happens very
//      rapidly, the motor appears to be moving at the appropriate fraction
//      of full speed.
//
//      Due to the limited speed of RCX code, you can actually see the motor

//      speed pulse or throb. Running on a faster system (such as legOS),
//      no pulsing should be perceivable.
task Motor
{
        // initialize task variables
        accumA = 0;
        accumC = 0;

        while (true) {
                // motor A is current motor
                powCur = powA;


                // if motor A is supposed to go forwards, ...
                if (powCur > 0) {
                        accumA -= powCur;
                        OutputDir(OUT_A, OUT_FWD);
                }
                else {  // motor A goes backwards
                        accumA += powCur;
                        OutputDir(OUT_A, OUT_REV);
                }

                // if accumulator A underflows, ...
                if (accumA < 0) {
                        accumA += OUT_FULL;
                        OutputMode(OUT_A, OUT_ON);
                }
                else {
                        OutputMode(OUT_A, OUT_OFF);
                }

                // motor C is current motor
                powCur = powC;

                // if motor C is supposed to go forwards, ...
                if (powCur > 0) {
                        accumC -= powCur;
                        OutputDir(OUT_C, OUT_FWD);
                }
                else {  // make motor C go backwards
                        accumC += powCur;
                        OutputDir(OUT_C, OUT_REV);
                }

                // if accumulator C undeflows, ...
                if (accumC < 0) {
                        accumC += OUT_FULL;
                        OutputMode(OUT_C, OUT_ON);
                }
                else {
                        OutputMode(OUT_C, OUT_OFF);
                }
        }
}
#endif



Message has 2 Replies:
  Re: Motor power? (LONG! with code example)
 
In article <36CB9D28.446A4B5B@seanet.com>, Dan McCabe <danm@seanet.com> writes (...) And quite right too. (26 years ago, 18-Feb-99, to lugnet.robotics)
  Storage for RCX - and other Technic
 
I have just updated my website and added a new page outlining PLANO Tackle boxes and how to modify them for that few Technic parts that just won't fit. Have a look at <www.bmts.com/~rhempel> and follow the clearly marked path on the left to the (...) (26 years ago, 18-Feb-99, to lugnet.robotics, lugnet.storage)

Message is in Reply To:
  Motor power?
 
When I set the motor power (both in RIS and nqc), I expected the speed of the motor to be proportional to the power setting (i.e., higher power setting makes motor turn faster and vice versa). However, what I see is that the power setting appears to (...) (26 years ago, 17-Feb-99, to lugnet.robotics)

13 Messages in This Thread:






Entire Thread on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact

This Message and its Replies on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    
Active threads in Robotics

 
Verified and Trusted Team of Hackers
5 hours ago
Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR