10 years, 8 months ago.

Using an RtosTimer inside of an RtosThread

Hi,

I am writing some software to control a quadcopter and have got completely stuck with RtosTimers. I am getting the error "Error: No instance of constructor "rtos::RtosTimer::RtosTimer" matches the argument list in "flightController.h", Line: 13, Col: 29"

I have looked at the example code in the handbook and my code seems to match. I have also googled but I couldn't find anything on using RtosTimers inside of RtosThreads.

Maybe I am going about this the wrong way so if anyone has any suggestions it would be much appreciated.

Here is the code that is causing me problems

//Rtos Timers
RtosTimer UpdateFlightTimer(Task500Hz, osTimerPeriodic, (void *)0);
RtosTimer UpdateCommandTimer(Task50Hz, osTimerPeriodic, (void *)0);

// A thread to monitor the serial ports
void FlightControllerThread(void const *args) 
{  
    UpdateFlightTimer.start(2);
    UpdateCommandTimer.start(20);
    
    // Wait here forever
    Thread::wait(osWaitForever);
}

void Task500Hz(void const *n)
{
    //Get IMU data and convert to yaw, pitch, roll
    _freeIMU.getQ(_rawQuaternion);
    _freeIMU.getRate(_gyroRate);
    GetAttitude();
    
    //Rate mode
    if(_rate == true && _stab == false)
    {
        //Update rate PID process value with gyro rate
        _yawRatePIDController->setProcessValue(_gyroRate[0]);
        _pitchRatePIDController->setProcessValue(_gyroRate[2]);
        _rollRatePIDController->setProcessValue(_gyroRate[1]);
        
        //Update rate PID set point with desired rate from RC
        _yawRatePIDController->setSetPoint(_rcConstrainedCommands[0]);
        _pitchRatePIDController->setSetPoint(_rcConstrainedCommands[1]);
        _rollRatePIDController->setSetPoint(_rcConstrainedCommands[2]);
        
        //Compute rate PID outputs
        _ratePIDControllerOutputs[0] = _yawRatePIDController->compute();
        _ratePIDControllerOutputs[1] = _pitchRatePIDController->compute();
        _ratePIDControllerOutputs[2] = _rollRatePIDController->compute();
    }
    //Stability mode
    else
    {
        //Update stab PID process value with ypr
        _yawStabPIDController->setProcessValue(_yrp[0]);
        _pitchStabPIDController->setProcessValue(_yrp[2]);
        _rollStabPIDController->setProcessValue(_yrp[1]);
        
        //Update stab PID set point with desired angle from RC
        _yawStabPIDController->setSetPoint(_yawTarget);
        _pitchStabPIDController->setSetPoint(_rcConstrainedCommands[1]);
        _rollStabPIDController->setSetPoint(_rcConstrainedCommands[2]);
        
        //Compute stab PID outputs
        _stabPIDControllerOutputs[0] = _yawStabPIDController->compute();
        _stabPIDControllerOutputs[1] = _pitchStabPIDController->compute();
        _stabPIDControllerOutputs[2] = _rollStabPIDController->compute();
        
        //if pilot commanding yaw
        if(abs(_rcConstrainedCommands[0]) > 5)
        {  
            _stabPIDControllerOutputs[0] = _rcConstrainedCommands[0];  //Feed to rate PID (overwriting stab PID output)
            _yawTarget = _yrp[0];
        }
        
        //Update rate PID process value with gyro rate
        _yawRatePIDController->setProcessValue(_gyroRate[0]);
        _pitchRatePIDController->setProcessValue(_gyroRate[2]);
        _rollRatePIDController->setProcessValue(_gyroRate[1]);
        
        //Update rate PID set point with desired rate from stab PID
        _yawRatePIDController->setSetPoint(_stabPIDControllerOutputs[0]);
        _pitchRatePIDController->setSetPoint(_stabPIDControllerOutputs[1]);
        _rollRatePIDController->setSetPoint(_stabPIDControllerOutputs[2]);
        
        //Compute rate PID outputs
        _ratePIDControllerOutputs[0] = _yawRatePIDController->compute();
        _ratePIDControllerOutputs[1] = _pitchRatePIDController->compute();
        _ratePIDControllerOutputs[2] = _rollRatePIDController->compute();
    }


    //Calculate motor power if flying
    if(_rcCommands[3] > 0 && _armed == true)
    {
        _motorPower[0] = Constrain(_rcConstrainedCommands[3] + _ratePIDControllerOutputs[1] + _ratePIDControllerOutputs[2] + _ratePIDControllerOutputs[0], MOTORS_MIN, MOTORS_MAX);
        _motorPower[1] = Constrain(_rcConstrainedCommands[3] + _ratePIDControllerOutputs[1] - _ratePIDControllerOutputs[2] - _ratePIDControllerOutputs[0], MOTORS_MIN, MOTORS_MAX);
        _motorPower[2] = Constrain(_rcConstrainedCommands[3] - _ratePIDControllerOutputs[1] - _ratePIDControllerOutputs[2] + _ratePIDControllerOutputs[0], MOTORS_MIN, MOTORS_MAX);
        _motorPower[3] = Constrain(_rcConstrainedCommands[3] - _ratePIDControllerOutputs[1] + _ratePIDControllerOutputs[2] - _ratePIDControllerOutputs[0], MOTORS_MIN, MOTORS_MAX);
    }
    //Not flying
    else
    {
        //Disable motors
        _motorPower[0] = MOTORS_OFF;
        _motorPower[1] = MOTORS_OFF;
        _motorPower[2] = MOTORS_OFF;
        _motorPower[3] = MOTORS_OFF;
        
        _notFlying ++;
        if(_notFlying > 200) //Not flying for 1 second
        {
            //Reset iteratior
            _notFlying = 0;
            
            //Zero gyro
            _freeIMU.zeroGyro();
            
            //Reset I
            _yawRatePIDController->reset();
            _pitchRatePIDController->reset();
            _rollRatePIDController->reset();
            _yawStabPIDController->reset();
            _pitchStabPIDController->reset();
            _rollStabPIDController->reset();
        }
    } 
    
    //Set motor power
    _motor1.write(_motorPower[0]);
    _motor2.write(_motorPower[1]);
    _motor3.write(_motorPower[2]);
    _motor4.write(_motorPower[3]);
}

void Task50Hz(void const *n)
{
    //Get RC control values
    
    //Constrain
    //Rate mode
    if(_rate == true && _stab == false)
    {
        _rcConstrainedCommands[0] = Constrain(_rcCommands[0], RC_YAW_RATE_MIN, RC_YAW_RATE_MAX);
        _rcConstrainedCommands[1] = Constrain(_rcCommands[1], RC_PITCH_RATE_MIN, RC_PITCH_RATE_MAX);
        _rcConstrainedCommands[2] = Constrain(_rcCommands[2], RC_ROLL_RATE_MIN, RC_ROLL_RATE_MAX);
        _rcConstrainedCommands[3] = Constrain(_rcCommands[3], RC_THRUST_MIN, RC_THRUST_MAX);
    }
    else
    {
        _rcConstrainedCommands[0] = Constrain(_rcCommands[0], RC_YAW_RATE_MIN, RC_YAW_RATE_MAX);
        _rcConstrainedCommands[1] = Constrain(_rcCommands[1], RC_PITCH_ANGLE_MIN, RC_PITCH_ANGLE_MAX);
        _rcConstrainedCommands[2] = Constrain(_rcCommands[2], RC_ROLL_ANGLE_MIN, RC_ROLL_ANGLE_MAX);
        _rcConstrainedCommands[3] = Constrain(_rcCommands[3], RC_THRUST_MIN, RC_THRUST_MAX);
    }
}

My program can be found at http://mbed.org/users/joe4465/code/QuadMK5/

And the problem is in flightController.h I think it should be clear what I am trying to do but if anyone isn't sure let me know.

I also have another totally unrelated problem. I can set my PID variables over serial and then save them to a config file but 1 in 3 times if will hang just after it has saved the data to the file and I'm not sure why. Does anyone have any idea what could cause this?

Thanks Joe

2 Answers

10 years, 8 months ago.

Where you define your functions on top, they lack the (void const *n) part. So it sees them as the incorrect type of function.

Accepted Answer
10 years, 8 months ago.

Of course, thanks