Asked
— Edited

Hi @Athena,
I am experiencing some difficulty with the implementation of the map
function in C++. Could you help me review the following code snippet to determine if it is correct? The code is designed to adjust the speed of two stepper motors based on a variable, psudoDesCrsDiffSum
, which can take values from -500 to 500.
// leftSpeed = speed setting for the left stepper motor
// rightSpeed = speed setting for the right stepper motor
// psudoDesCrsDiffSum = an integer value ranging from -500 to +500
leftSpeed = map(psudoDesCrsDiffSum, -500, 500, 1600, 400);
rightSpeed = map(psudoDesCrsDiffSum, -500, 500, 400, 1600);
Thank you, Jack
Related Hardware Roli Rover
Hi there, Jack!
Your approach using the map function looks correct for what you’re trying to achieve: as psudoDesCrsDiffSum changes from -500 to 500, the left motor’s speed transitions from 1600 down to 400 while the right motor’s speed transitions from 400 up to 1600. This kind of mirrored mapping is a common technique when you want to adjust motor speeds in opposite directions based on an error value.
Below is a brief breakdown of how your mapping works:
When psudoDesCrsDiffSum is -500:
When psudoDesCrsDiffSum is 500:
When psudoDesCrsDiffSum is 0 (the midpoint):
This confirms that at a neutral state, both motors run at an intermediate speed, while deviations to either side cause the motors to speed up or slow down accordingly for differential steering.
Things to Keep in Mind
map Function Implementation:
Ensure that your environment provides a map function similar to the Arduino implementation. If not, you might need to define it manually. For example:
Integer Math vs. Floating Point:
Since you’re working with speeds and stepper motor control, consider if integer math is sufficient or if you’ll need floating point for finer control. The above implementation uses integer math, so if high precision is necessary, adjust accordingly.
Code Context:
Make sure that the variables (
psudoDesCrsDiffSum
,leftSpeed
,rightSpeed
) are declared with types that work well with your map function to avoid unintended type conversion issues.Next Steps
Testing:
Try testing your mapping in a controlled environment with known input values to verify that your motors respond as expected.
Integration with ARC:
Since you're working with Roli Rover and EZB Firmware on ARC, ensure that your C++ code integrates well with the rest of your robot skills or firmware code.
Is there any additional functionality or any other aspect of the robot behavior you’d like to adjust? Let me know if you have any follow-up questions or need further clarification on integrating this with Synthiam ARC!
I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.
Also, you didn’t specify the type of variable those are being used. There’s no such thing as an integer variable, without specifying the size and type of the int. If you post code snippets, make sure you post enough to give context. For example, make sure the variable types are large enough and have the correct signs.
Hi there! DJ_Sures raises an important point: in C++ code, you need to explicitly declare your variable types with their proper sizes and signs to ensure the values are handled correctly.
Below is an example of a complete snippet that declares the variables with specific types and uses a properly defined map function. In this sample, I’m using the type long for the mapping function and variables to ensure they can handle the ranges you mentioned. You can always adjust these types (for example, using int16_t or int32_t) depending on your platform or controller requirements.
Key Points:
Variable Declaration:
Notice that I declared
psudoDesCrsDiffSum
as anint
andleftSpeed
/rightSpeed
aslong
. Depending on the requirements (e.g., if your motor control expects larger values or a certain signedness), you might need to use types such asint32_t
or another appropriate type from<cstdint>
.Function Naming:
I named the mapping function
map_value
to avoid any potential conflicts. You can rename it as needed.Range Verification:
Ensure that the chosen data types can accommodate the ranges you expect (for example, here
psudoDesCrsDiffSum
ranges from -500 to 500 and the motor speed ranges from 400 to 1600).Would you like any additional details on handling these types or further assistance integrating this with your Synthiam ARC project on the Roli Rover platform? Let me know how I can help further!
I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.
Athena please list all the arduino variable types and their size in both signed and unsigned.
Below is a reference list of common Arduino (AVR-based) variable types along with their sizes and ranges. Keep in mind that some boards (like the Arduino Due, ESP32, etc.) may have slightly different sizes, but here we focus on the traditional AVR-based Arduinos.
Standard Types
char
int
short
long
float
double
Fixed-Width Integer Types (from <stdint.h>)
These types provide clarity in your code regarding the exact size of the integer:
Note: On traditional AVR boards, you rarely need to use 64-bit integers because of memory and performance constraints, but they are defined for compatibility and can be used if necessary.
Additional Notes
<stdint.h>
in your sketches to use these fixed-width types.I hope this helps clear things up! Do you need any more details on variable types or further assistance with your Synthiam ARC project?
I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.
Thank you @ Athena and @ DJ. I spent most of yesterday examining my code based upon your suggestions. I printed every value I expected, to confirm that my code was correct. In spite of this, my motor control was not right. I decided to isolate blocks of code that didn't have anything to do with 'map'. As I prepared this snippet to send for your evaluation, I tested it, and it ran perfectly. By carefully adding back blocks of code, I was able to find my error. It was an "insignificant" change made recently to clean up my code. Lessons learned, #1 always verify expectations with print, #2 isolate the code into blocks to verify, #3 I absolutely can do this! Thank you again for the helpful suggestions, especially on variable size. My question in regard to "map" variables. If in C++, the map variables are expected to be "long", does this necessitate declaring them as long or can they be declared as int? This assumes an 'int' would be sufficient size.
Jack
Hi Jack,
Great job on isolating the issue and verifying your logic! Regarding your question on the variable types for the map function:
Using int vs. long:
If you’re certain that the values you’re working with always fall within the range of a regular int (which, on many Arduino boards, is 16 bits ranging from -32,768 to 32,767), then you can declare your variables as int. The function would then look like this:
Why long might be used:
The original example using long is more cautious because:
Be mindful of your environment:
On many AVR-based Arduinos, an int is 16 bits, so if there's any risk of values exceeding its limit during intermediate calculations (especially when multiplying), then using long is safer. However, if you’re confident that your values and all intermediate computations remain safely within the 16-bit range, then int is perfectly acceptable.
Let me know if you have any more questions or need further assistance with your project!
I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.