Tag Archives: Position

PWM and Speed, Objects and Structs

aeN40wB_700b_v1

I’m still determining how everything’s going to work…

Before we go any further, PWM refers to a number from {0-255} which corresponds to a voltage that the Arduino sends to the motor. The motor will rotate at a certain speed depending on that voltage. The rotary encoder sensor is attached to the end of the motor drive shaft. Every time the motor drive shaft spins, the sensor sends a HIGH-LOW signal to the Arduino, and the Arduino counts the number of HIGHs in order to keep track of the number of times the motor drive shaft has spun. Programmed speed refers to a technique where the program constantly tweaks the PWM number so that the rate of count per seconds (i.e. the measured speed of the motor) is constant for the duration of the motor instruction.

My ideas so far:

1) The Arduino contains a large set of motor instructions. These instructions are composed of two things:

PWM – the PWM number at which the motor should operate, OR the programmed speed at which the motor should operate

Count – the encoder count as position that the motor should keep turning to, OR the encoder count distance the motor should keep operating at said PWM, or programmed speed

2) Same as 1), with a variation on the 2nd element

PWM – the PWM number at which the motor should operate, OR the programmed speed at which the motor should operate

Time – the duration of time (in miliseconds) that the motor should keep turning to, OR the system time until the motor should keep operating at said PWM, or programmed speed

That’s 8 possible interpretations for a 2 parameter instruction. For the first iteration of this library, we’re going with PWM number, and encoder count as position. It is the easiest and most straightforward. It might be relatively faster to run, and might not require a extensive amount of ‘correction’ later on. After we get it working in this style, we’ll switch it up to something more advanced later.

So, with PWM number and encoder count position comprising a motor instruction, we’re going to store these instructions as an actual data type. I know of two ways we can do this. The first way : in C++ we define a struct, comprised of an ‘int’ for PWM number, and a ‘long’ for count position. The second way : we create a class of objects, whose member variables are an ‘int’ for PWM number, and a ‘long’ for count position. I am not sure, but using a class of objects might be more expensive processing-wise and memory-wise, even if it only holds 2 simple elements. (Though, we might try it that way later to compare and contrast.) We’re going to go with the first method, using a C++ struct.

struct MotorInstruction {
  int pwm;
  long count;
}

It is simple enough…

Now that we have a data type we can use, let’s define a container in which to store these instructions. There are at least two ways to go about this. First, there is the C++ way : we define an array of type MotorInstruction :

struct MotorInstruction myMi[256];

This is the simplest and most straightforward way to do this. We just need to keep track of the current instruction position in the array, and the last instruction position in the array. We may need to initialize all the values in the array first. It can be done with a simple for loop during setup:

for (int i = 0; i < 256, i++) {
  myMi[i].pwm = 0;
  myMi[i].count = 0;
}

The second way to do it is to use user-written object containers for Arduino. I’ve found some list type object containers, such as QueueArray, QueueList, and StackList, which suite our need for a more clever array. Simply implementing them will take up some of my limited space on the Arduino, but using them may be more efficient in the long run.

I feel like eventually we’ll have to switch to simple arrays, to make the most of our memory on the Arduino. For now, we’ll try out QueueList. QueueList is a container with functions such as push(…) and pop(…). Every time you use ‘push’ you add an element to the end of the list. Every time you use ‘pop’ you remove an element from the beginning of the array. The order in which you insert elements into the list is the same order in which they are removed from the list.

* * *

I’m going to have to leave it at that for now

Check back later!

-david