Using Enumerators to Control Motor Speed in Microcontrollers Project

  • Thread starter Thread starter Lancelot59
  • Start date Start date
AI Thread Summary
The discussion focuses on using enumerators to control motor speed in a microcontroller project for a pathfinding robot. The original code is described as poorly structured, prompting a complete rewrite of the control system. The user seeks clarification on how to effectively use enums to manage motor speeds and integrate them with an array or vector. It is explained that enums are simply named constants that can be used directly in function calls, enhancing code readability. The conversation emphasizes the importance of using these named constants for clarity over using raw numerical values.
Lancelot59
Messages
640
Reaction score
1
I'm working on a project for my microcontrollers course. It involves programming a pathfinding robot that uses 5 reflected light sensors to follow a track. Since the code that we were provided that was written I don't know how long ago fails as hard as it does I decided to re-write the whole control system.

Only thing is to simplify things when programming the logic into this thing I want to use enums to control the speed of each motor.

I have NO clue how this guy put the code together, but it's just a big partially commented mess.

Here is the applicable code from the header file
Code:
enum motor_speed_setting { rev_fast, rev_medium, rev_slow, stop, slow, medium, fast };



enum motor_selection { left, right };



void set_motor_speed(enum motor_selection the_motor, enum motor_speed_setting motor_speed, int speed_modifier);

and here is the code from the source file

Code:
void set_motor_speed(enum motor_selection the_motor, enum motor_speed_setting motor_speed, int speed_modifier)

{

    const static int motor_speeds[] = { -800, -600, -400, 0, 400, 600, 800};

    int duty_cycle;

    enum e_direction {reverse,forward} dir_modifier= forward;

Now what I want to do is have my array (or vector, I like them better) with similar values, with enumerated values for each setting. I just don't get how this stuff goes together.

How do I go about enumerating values to the values in the array/vector? I know how to assign them when creating the array by making the values equal something, but this fellow seemed to create the values, then enumerate them.
 
Last edited:
Physics news on Phys.org
Lancelot59 said:
I'm working on a project for my microcontrollers course. It involves programming a pathfinding robot that uses 5 reflected light sensors to follow a track. Since the code that we were provided that was written I don't know how long ago fails as hard as it does I decided to re-write the whole control system.

Only thing is to simplify things when programming the logic into this thing I want to use enums to control the speed of each motor.

I have NO clue how this guy put the code together, but it's just a big partially commented mess.

Here is the applicable code from the header file
Code:
enum motor_speed_setting { rev_fast, rev_medium, rev_slow, stop, slow, medium, fast };



enum motor_selection { left, right };



void set_motor_speed(enum motor_selection the_motor, enum motor_speed_setting motor_speed, int speed_modifier);

and here is the code from the source file

Code:
void set_motor_speed(enum motor_selection the_motor, enum motor_speed_setting motor_speed, int speed_modifier)

{

    const static int motor_speeds[] = { -800, -600, -400, 0, 400, 600, 800};

    int duty_cycle;

    enum e_direction {reverse,forward} dir_modifier= forward;

Now what I want to do is have my array (or vector, I like them better) with similar values, with enumerated values for each setting. I just don't get how this stuff goes together.

How do I go about enumerating values to the values in the array/vector? I know how to assign them when creating the array by making the values equal something, but this fellow seemed to create the values, then enumerate them.

You're making this much more complicated than it really is. An enum (short for enumeration or enumerated type) is nothing more than a type that lists a bunch of named constants. A variable of this type can be one of the values in the enumeration. That's all.

You don't enumerate anything; you just use those named constants. Do a search for C++ enum and you'll find lots of pages that describe enumerated types. Here's one that I found: http://www.cppreference.com/wiki/keywords/enum.

Your set_motor function takes three arguments: the first is the_motor, the second is a motor speed, and the third is a speed modifier. A call to this function would look like this:

set_motor( left, rev_medium, 2);

This call would apparently set the motor speed to medium for the left motor (the motor in the left wheel?). I don't know what the 2 would do, since you don't show enough of the code for me to be able to tell.
 
I didn't code that, it was provided. The massive confusion I had when I tried to figure out what was going on is the reason I'm re-writing everything. I did manage to figure it out with that though. Thanks for the help.
 
Right, I knew that you didn't code it. The thing about using enums is that the named constants give you a better idea of what's going on than just constants would.

For example, a call to set_motor( 1, 3, 2); would be much harder to figure out.
 
That makes sense.
 

Similar threads

Replies
3
Views
2K
Back
Top