扭矩模式车辆控制报错:wb_motor_set_force()传入无效NaN参数
wb_motor_set_force() called with an invalid 'force' argument (NaN) Error Hey Nelson, let's break down what's causing this error and get your vehicle control code working right. That NaN error means somewhere, you're passing a "not-a-number" value to the motor force function—so let's hunt down where that's coming from in your code.
1. Your Control Logic Has Contradictory Checks
Look at this section of your code:
if(type_ == BREAK){ if(!type_){ // Wait, you just checked type_ == BREAK—!type_ will NEVER be true here type_ = true; driver_->setThrottle(0); } driver_->setBrakeIntensity(breaking_); } else if(type_ == ACCELERATE){ if(type_){ // This checks if type_ is truthy, but you already know it's ACCELERATE type_ = false; driver_->setBrakeIntensity(0); } driver_->setThrottle(throttle_); }
In the BREAK branch, that inner !type_ check is totally useless—since you just confirmed type_ == BREAK, negating it will always be false. That means when you switch to braking mode, you never reset the throttle to 0. Over time, this could lead to weird value calculations that spit out NaNs. Also, you're toggling type_ between enum values (BREAK/ACCELERATE) and boolean true/false, which is messy and can cause unexpected behavior.
2. Variable Initialization & Spelling Issues
- You initialized
breaking_asfalsethen immediately set it to0—mixing booleans and numeric values like this can lead to odd type conversion bugs. Plus, "breaking" is a spelling mistake (it should be "brake")—small typos can cause big headaches if you accidentally use the wrong variable later. - Double-check where
throttle_andbreaking_get their values elsewhere in your code. If either variable is being calculated with a division by zero, or using an uninitialized value, that's a surefire way to get a NaN.
3. Step-by-Step Fixes
Fix the Control Logic
First, clean up your mode handling—use an enum to make things clear, and ensure switching modes resets the other control to 0:
// Define your modes clearly with an enum enum ControlMode { BREAK, ACCELERATE, IDLE }; ControlMode type_ = IDLE; // Updated control logic if(type_ == BREAK){ // When braking, make sure throttle is off driver_->setThrottle(0); // Guard against invalid brake values (NaN or out of 0-1 range) if(std::isnan(breaking_) || breaking_ < 0 || breaking_ > 1){ breaking_ = 0; // Fallback to a safe value } driver_->setBrakeIntensity(breaking_); } else if(type_ == ACCELERATE){ // When accelerating, make sure brakes are off driver_->setBrakeIntensity(0); // Guard against invalid throttle values if(std::isnan(throttle_) || throttle_ < 0 || throttle_ > 1){ throttle_ = 0; } driver_->setThrottle(throttle_); }
Validate Your Variables
- Go through every place you assign values to
throttle_andbreaking_—make sure you're never dividing by zero, using uninitialized variables, or pulling values from a sensor that might return NaN. - Rename
breaking_tobrakeIntensity_to fix the spelling and make the variable's purpose clearer.
Add Debug Prints
To confirm where the NaN is coming from, add quick print statements before you set throttle/brake:
std::cout << "Current throttle value: " << throttle_ << std::endl; std::cout << "Current brake intensity: " << breaking_ << std::endl;
This will let you see exactly when a NaN pops up, so you can trace it back to the source.
Wrap-Up
Most likely, that broken conditional in your control logic is preventing proper reset of throttle/brake values, leading to a NaN being passed to the motor function. Fixing the logic, validating your variables, and cleaning up typos should get rid of that error.
内容的提问来源于stack exchange,提问作者Nelson

