Using Enumerators to Control Motor Speed in Microcontrollers Project

  • Thread starter Thread starter Lancelot59
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the use of enumerators (enums) to control motor speed in a microcontroller project for a pathfinding robot. Participants explore the implementation of enums in the context of programming logic for motor control, addressing issues related to existing code and seeking clarification on how to effectively use enums with arrays or vectors.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant expresses confusion about how to integrate enumerated values with an array or vector for motor speed settings.
  • Another participant explains that enums are simply named constants and suggests that the participant should use these constants directly without additional enumeration.
  • A later reply acknowledges the confusion with the provided code and indicates that the participant has managed to understand it with assistance.
  • Another participant emphasizes that using enums improves code readability compared to using raw constants.

Areas of Agreement / Disagreement

Participants generally agree on the utility of enums for clarity in code, but there is no consensus on the best approach to integrate them with arrays or vectors, as the original poster remains uncertain about the implementation.

Contextual Notes

The discussion highlights limitations in the provided code and the original poster's understanding, as well as the dependency on the clarity of the existing implementation.

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 4 ·
Replies
4
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K