Asked — Edited
Resolved Resolved by Athena AI!

Assistance With C++ Map Function

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

ARC Pro

Upgrade to ARC Pro

Get access to the latest features and updates before they're released. You'll have everything that's needed to unleash your robot's potential!

PRO
Canada
#9  

Hi DJ_Sures,

You're absolutely right-unit testing is a very effective approach to isolate and verify your code logic, and your two outlined strategies are spot on. Let’s break them down:


1. Using Compiler Directives

By leveraging compiler directives (e.g., using #ifdef, #ifndef, etc.), you can compile only specific sections of your code for testing. For example:

#ifdef UNIT_TESTING
  // Test code executed only during unit testing
  int testValue = someFunction(knownInput);
  if (testValue != expectedOutput) {
    Serial.println("Test failed!");
  } else {
    Serial.println("Test passed!");
  }
#else
  // Normal operational code
#endif

This way, you can selectively isolate code blocks and run tests with predetermined constants and conditions without impacting the production code. It works particularly well when working in environments like Arduino, where code size and efficiency are important.


2. Splitting into Granular Functions

The second approach is to break down your program into smaller, independent functions. Once modularized, these functions can be directly invoked from a separate test harness. For example:

// main code file (MyLibrary.cpp)
int mapValue(int x, int in_min, int in_max, int out_min, int out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

You can then create a separate test file or section:

// test file (test_MyLibrary.cpp)
#include <cassert>
#include "MyLibrary.h"

int main() {
    // Test with a known input
    int result = mapValue(0, -500, 500, 1600, 400);
    // Expected result is in between 1600 and 400 (i.e. 1000)
    assert(result == 1000);
    
    // More test cases...
    result = mapValue(-500, -500, 500, 1600, 400);
    assert(result == 1600);
    
    result = mapValue(500, -500, 500, 1600, 400);
    assert(result == 400);
    
    // Indicate testing success
    return 0;
}

This method makes it easier to pinpoint issues since you isolate and test smaller logical units rather than debugging the entire program at once.


Additional Thoughts

  • Tool Assistance: There are also unit testing frameworks (such as Unity for embedded systems or even GoogleTest for larger projects) which can further streamline the process.
  • Frequency: While it might seem cumbersome at times, integrating unit testing can save a lot of headache down the road by catching bugs early.
  • Mindset: Often, integrating unit tests shifts the mindset from "my code works; there’s no problem" to "if my code is wrong, I’ll know immediately." This proactive approach is what separates robust systems from those that might have hidden flaws.

I absolutely agree-taking a little extra time to test small portions not only improves code reliability but also builds confidence in your development process. If you need further examples or help in setting up unit tests within your project, or how to best integrate this with your Synthiam ARC setup, feel free to ask!

What’s your experience been like with unit testing in your projects so far?


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.