Asked — Edited
Resolved Resolved by Athena AI!

How To Implement A Ramp Function To A Motor Value Derived From A Map Function.

Hello @Athena,

I am experiencing difficulties with implementing a ramp function for motor values that are determined using a map function in my C++ code. Could you please review the attached code and provide guidance on why it won't compile? The variables leftSpeed and rightSpeed are the values produced by the map function.

Thank you,
Jack

void mapMotors() { 
  if (abs(diffCourse) <= 5) {  // Based on the value of diffCourse, choose a MAP profile
    Serial.print("diffCourse:   ");
    Serial.println(abs(diffCourse));  // Get positive value for diffCourse

    // Calculate leftSpeed and rightSpeed values
    leftSpeed = map(diffCourse, -4, 4, 160, 60);
    rightSpeed = map(diffCourse, -4, 4, 40, 120);
    Serial.print("< than map   ");
    leftSpeed = constrain(leftSpeed, 60, 160);   // Constrain the value of leftSpeed
    rightSpeed = constrain(rightSpeed, 40, 120);  // Constrain the value of rightSpeed
  } else {     
    leftSpeed = map(diffCourse, -15, 15, 160, 60);
    rightSpeed = map(diffCourse, -15, 15, 40, 120);
    Serial.print("> than map   ");
    leftSpeed = constrain(leftSpeed, 60, 160);   // Constrain the value of leftSpeed
    rightSpeed = constrain(rightSpeed, 40, 120);  // Constrain the value of rightSpeed
  }
}

void rampUpMotors(int currentLeftSpeed, int currentRightSpeed) {
  int targetLeftSpeed = leftSpeed;
  int targetRightSpeed = rightSpeed;
  int step = 4; // Adjust step size for smoother or faster ramp-up

  while (currentLeftSpeed < targetLeftSpeed || currentRightSpeed < targetRightSpeed) {
    if (currentLeftSpeed < targetLeftSpeed) {
      currentLeftSpeed += step;
      if (currentLeftSpeed > targetLeftSpeed) currentLeftSpeed = targetLeftSpeed;
    }
    if (currentRightSpeed < targetRightSpeed) {
      currentRightSpeed += step;
      if (currentRightSpeed > targetRightSpeed) currentRightSpeed = targetRightSpeed;
    }
    analogWrite(11, currentLeftSpeed);
    analogWrite(10, currentRightSpeed);
    delay(50); // Adjust delay for smoother or faster ramp-up
  }
}

Please let me know if you need any additional information.


Related Hardware Arduino Genuino Uno

ARC Pro

Upgrade to ARC Pro

Stay on the cutting edge of robotics with ARC Pro, guaranteeing that your robot is always ahead of the game.

#1  

*Note: we modified your post and selected Arduino Uno instead of Roli Rover. The Roli Rover uses an EZB v4 from EZ-Robot and it does not run c++ code. The only product ARC supports that runs C++ would be in the Arduino or ESP32 line. So we selected what we believe would be most appropriate. You can edit your post if that is not correct.

PRO
Canada
#2  

Hello Jack,

It looks like you're trying to implement a ramp function for motor values in your C++ code for an Arduino Genuino Uno. Let's go through your code and identify potential issues and improvements.

Issues and Improvements

  1. Syntax Errors:

    • The if condition in mapMotors() seems incomplete. Ensure that the condition is correctly written.
    • The abs(diffCourse)than map line seems to be a syntax error. Ensure that the condition is complete and logical.
  2. Variable Initialization:

    • Ensure that leftSpeed, rightSpeed, and diffCourse are properly initialized before use.
  3. Ramp Function Logic:

    • The rampUpMotors() function should increment or decrement the current speed towards the target speed. Ensure that the logic correctly updates currentLeftSpeed and currentRightSpeed.
  4. Loop Logic:

    • The while loop in rampUpMotors() should have conditions that allow it to exit once the target speeds are reached. Ensure that the loop conditions are correct.
  5. Analog Write:

    • Ensure that the analogWrite() function is used correctly with the appropriate pins and values.

Revised Code

Here's a revised version of your code with some corrections and improvements:

void mapMotors() {
    // Ensure diffCourse is defined and initialized
    if (abs(diffCourse) > someThreshold) { // Replace 'someThreshold' with your condition
        leftSpeed = map(diffCourse, -100, 100, 60, 160); // Example mapping
        rightSpeed = map(diffCourse, -100, 100, 40, 120); // Example mapping
        leftSpeed = constrain(leftSpeed, 60, 160);
        rightSpeed = constrain(rightSpeed, 40, 120);
    }
}

void rampUpMotors(int currentLeftSpeed, int currentRightSpeed) {
    int targetLeftSpeed = leftSpeed;
    int targetRightSpeed = rightSpeed;
    int step = 4; // Adjust step size for smoother or faster ramp-up

    while (currentLeftSpeed != targetLeftSpeed || currentRightSpeed != targetRightSpeed) {
        if (currentLeftSpeed < targetLeftSpeed) {
            currentLeftSpeed += step;
            if (currentLeftSpeed > targetLeftSpeed) currentLeftSpeed = targetLeftSpeed;
        } else if (currentLeftSpeed > targetLeftSpeed) {
            currentLeftSpeed -= step;
            if (currentLeftSpeed < targetLeftSpeed) currentLeftSpeed = targetLeftSpeed;
        }

        if (currentRightSpeed < targetRightSpeed) {
            currentRightSpeed += step;
            if (currentRightSpeed > targetRightSpeed) currentRightSpeed = targetRightSpeed;
        } else if (currentRightSpeed > targetRightSpeed) {
            currentRightSpeed -= step;
            if (currentRightSpeed < targetRightSpeed) currentRightSpeed = targetRightSpeed;
        }

        analogWrite(11, currentLeftSpeed);
        analogWrite(10, currentRightSpeed);
        delay(50); // Adjust delay for smoother or faster ramp-up
    }
}

Additional Tips

  • Debugging: Use Serial.print() statements to debug and check the values of variables at different stages.
  • Testing: Test the ramp function with different step sizes and delays to achieve the desired smoothness.

If you have any more questions or need further assistance, feel free to ask!


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.
#3  

Thank you @Synthiam Support and @Athena.  My trouble lay in the inconsistent use of my variables.  I'm still far from finished with this project but well on my way thanks to you! Very pleased with my Pro Subscription. Jack

PRO
Canada
#4  

You're very welcome, Jack! I'm glad to hear that the guidance helped you resolve the issue with your variables. It's great to know you're making progress on your project. If you have any more questions or need further assistance as you continue, feel free to reach out. Enjoy exploring the full potential of your Pro Subscription, and happy building with 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.