Using Enumerators to Control Motor Speed in Microcontrollers Project

  • Thread starter Lancelot59
  • Start date
In summary: If you don't enumerate anything, the compiler just uses the default values for the named constants. Using enums lets you explicitly specify which value should be used.
  • #1
Lancelot59
646
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
  • #2
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.
 
  • #3
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.
 
  • #4
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.
 
  • #5
That makes sense.
 

1. What is an enumerator in microcontrollers?

An enumerator is a special data type in microcontrollers that allows for the creation of a list of named constants. These constants can then be used to represent different states or values within the program, making it easier to read and write code.

2. How can enumerators be used to control motor speed in microcontrollers?

By assigning specific values to the enumerator constants, they can be used to set the speed of a motor. For example, a constant called "SLOW" can be assigned a value of 50, representing a slow speed setting, while a constant called "FAST" can be assigned a value of 100, representing a fast speed setting. These values can then be used in the code to control the motor speed.

3. What are the benefits of using enumerators in motor speed control?

Using enumerators can make the code more readable and easier to maintain. It also allows for more flexibility in adjusting the motor speed, as the values can be easily changed without having to modify the entire code. Additionally, using enumerators can help prevent errors in programming, as the constants provide a more intuitive and descriptive way of representing different motor speed settings.

4. Are there any limitations to using enumerators for motor speed control?

One limitation is that the speed settings must be predefined and cannot be changed dynamically during the program's execution. Also, the number of speed settings may be limited by the microcontroller's memory and processing capabilities. It is important to consider these limitations when using enumerators for motor speed control.

5. What other applications can enumerators have in microcontroller projects?

Enumerators can be used for a variety of purposes in microcontroller projects, such as controlling the state of different components, setting different modes of operation, or defining different input/output configurations. They can also be used to create menus or options for user input in a project.

Similar threads

  • Programming and Computer Science
Replies
4
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Electrical Engineering
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
Back
Top