示例#1
0
 void M8_PID::setupPID( byte sensor, float pGain, float iGain, float dGain, float iTermMin, float iTermMax )
 {
   setPGain( sensor, pGain );
   setIGain( sensor, iGain );
   setDGain( sensor, dGain );
   
   _info[ sensor ]._iTermMin = iTermMin;
   _info[ sensor ]._iTermMax = iTermMax;
         
   _info[ sensor ]._error = 0;
   _info[ sensor ]._iState = 0;
   _info[ sensor ]._dState = 0;
 }
示例#2
0
void CommanderCallBack(super_modified_servo::commanderConfig &config, uint32_t level)
{
    if (config.command == "search")
    {
        for (motor_id = 4; motor_id < MaxNumSMS; motor_id++)
        {
            start(fd, motor_id);
            sleep(1);
            printf("Start Node ID %d\n", motor_id);
            if (getCommunicationSuccess())
                printf("ID is %d\n", motor_id);
            resetErrors(fd, motor_id);
            sleep(1);
            stop(fd, motor_id);
            printf("Stop Node ID %d\n", motor_id);
        }
    }
    if (config.command == "start")
    {
        printf("[INFO] Start\n");
        start(fd, motor_id);
        start_flag = true;
    }
    if (config.command == "stop")
    {
        printf("[INFO] Stop\n");
        stop(fd, motor_id);
        start_flag = false;
    }
    if (config.command == "reset")
    {
        printf("[INFO] Reset\n");
        stop(fd, motor_id);
        sleep(1);
        start(fd, motor_id);
    }
    if (config.command == "setID")
    {
        printf("[INFO] Set ID: %d\n", new_motor_id);
        setNodeId(fd, motor_id, new_motor_id);
        printf("[INFO] Disconnect the servo\n");
    }
    if (config.command == "setBaud")
    {
        printf("[INFO] Set baud rate: %d\n", baudRate);
        setBaudRate(fd, motor_id, baudRate);
        printf("[INFO] Disconnect the servo\n");
        printf("[INFO] Change the bad rate in serialPortOpen function");
    }
    if (config.command == "error_reaction")
    {
        printf("[INFO] Error reaction\n");
        uint8_t resp[20];
        uint8_t errorReaction[20] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x02,
             0x02, 0x01};
        setErrorReaction(fd, motor_id, errorReaction);
        sleep(3);
        if (getCommunicationSuccess() == false)
        {
            resetErrors(fd, motor_id);;
            printf("[ERROR]: %d \n", getWarning());
        }
        getErrorReaction(fd, motor_id, resp);
        for (int i=0; i<20; i++)
            printf("D[%d]: %d\n", i, resp[i]);
    }
    if (config.command == "setGain")
    {
        printf("[INFO] Set Gain: \n");
        setPGain(fd, motor_id, kp);
        setIGain(fd, motor_id, ki);
        setDGain(fd, motor_id, kd);
        printf("[INFO] kp %d \n", getPGain(fd, motor_id));
        printf("[INFO] ki %d \n", getIGain(fd, motor_id));
        printf("[INFO] kd %d \n", getDGain(fd, motor_id));
    }
    if (config.command == "getGain")
    {
        printf("[INFO] kp %d \n", getPGain(fd, motor_id));
        printf("[INFO] ki %d \n", getIGain(fd, motor_id));
        printf("[INFO] kd %d \n", getDGain(fd, motor_id));
    }

    motor_id = config.motor_id;
    new_motor_id = config.new_motor_id;
    baudRate = config.baudRate;
    kp = config.p_gain;
    ki = config.i_gain;
    kd = config.d_gain;
    set_point = config.set_point;
}
示例#3
0
short processRadioMessages(char *buf, short lenth) {
  short cmdIndex = -1;
  char cmd[lenth];
  char token2[lenth];
  short i = 0;
  short changeLeve = 0;
  short parameter = 0;
  float parameterF = 0.0;
  float rollSpShift = 0;
  float pitchSpShift = 0;
  float yawShiftValue = 0;
  float throttlePercentage = 0;
  static unsigned long  radio_last_tv3 = 0;

#if 0
   unsigned long tv=millis();
  Serial.println(tv-radio_last_tv3);
   radio_last_tv3=tv;
  // Serial.println(buf);
#endif

  resetPacketAccCounter();
  

  memset(cmd, '\0', sizeof(cmd));

  for (i = 1; buf[i] != '#'; i++) {
    cmd[i - 1] = buf[i];
  }
 
  qToken(cmd, token2,lenth, 0);
  cmdIndex = atoi(token2);


  switch (cmdIndex) {
    case ON_OFF_FLY_SYSTEM_MESSAGE:

	  qToken(cmd, token2,lenth,ON_OFF_FLY);
      parameter = atoi(token2);

      if (1 == parameter) {
          enableFlySystem();
      } else {
        disenableFlySystem();
      }

      break;
    case PILOT_CONTROL_MESSAGE:
     // printf("%s\n",buf);

	  qToken(cmd, token2,lenth, THROTTLE_LEVEL);
      parameter = atoi(token2);
	  
      qToken(cmd, token2,lenth, ROLL_SP_SHIFT);
      rollSpShift = atof(token2);

      qToken(cmd, token2,lenth, PITCH_SP_SHIFT);
      pitchSpShift = atof(token2);

       qToken(cmd, token2,lenth, YAW_SP_SHIFT);
      yawShiftValue = atof(token2);
      //printf("factor=%d\n",(int)(((float)(parameter-100)/(float)100)*(float)(MAX_POWER_LEVEL-MIN_POWER_LEVEL)));
      //printf("parameter=%d\n",parameter);
      throttlePercentage = (float)parameter / 100.f;
      parameter = getMinPowerLevel()
                  + (int) (throttlePercentage
                           * (float) (getMaxPowerLeve()
                                      - getMinPowerLevel())); /*(100~200)*10=1000~2000 us*/
      //printf("getMaxPowerLeveRange()- getMinPowerLeveRange()=%f\n",getMaxPowerLeve()- getMinPowerLevel());
      //printf("parameter=%d\n",parameter);

      if (parameter > getMaxPowerLeve()
          || parameter < getMinPowerLevel()) {
         printf( "break\n");
        break;
      }
      if (true == flySystemIsEnable()) {

        setThrottlePowerLevel(parameter);

        if (getMinPowerLevel() == parameter) {
          //printf("STOP\n");
          resetPidRecord(&rollAttitudePidSettings);
          resetPidRecord(&pitchAttitudePidSettings);
          resetPidRecord(&yawAttitudePidSettings);
          resetPidRecord(&rollRatePidSettings);
          resetPidRecord(&pitchRatePidSettings);
          resetPidRecord(&yawRatePidSettings);
          setYawCenterPoint(0);
          setPidSp(&yawAttitudePidSettings, 321.0);

        } else {
          if (getPidSp(&yawAttitudePidSettings) == 321.0) {
             printf( "START Flying\n");
            setYawCenterPoint(getYaw());
            setPidSp(&yawAttitudePidSettings, 0);
          }
          setPidSp(&rollAttitudePidSettings,
                   LIMIT_MIN_MAX_VALUE(rollSpShift, -getAngularLimit(),
                                       getAngularLimit()));
          setPidSp(&pitchAttitudePidSettings,
                   LIMIT_MIN_MAX_VALUE(pitchSpShift, -getAngularLimit(),
                                       getAngularLimit()));
          setYawCenterPoint(getYawCenterPoint() + (yawShiftValue * 1.0));

        }

      }

      //printf("throttle=%d roll=%f pitch=%f\n",parameter,rollSpShift,pitchSpShift);

      break;



    case SYSTEM_SETTING_MESSAGE:
       printf( "%s " , buf);

	  qToken(cmd, token2,lenth, ANAGULAR_VELOVITY_LIMIT);
      parameterF = atof(token2);
      if (parameterF == 0) {
        parameterF = 1;
      }
      setGyroLimit(parameterF);
	  printf( "Angular Velocity Limit: %4.3f\n",getGyroLimit());
      /***/

	  qToken(cmd, token2,lenth, ROLL_CALIBRATION);
      parameterF = atof(token2);
      setPidSpShift(&rollAttitudePidSettings, parameterF);
	  printf( "Roll Angular Calibration: %4.3f\n",getPidSpShift(&rollAttitudePidSettings));

      /***/

	  qToken(cmd, token2,lenth, PITCH_CALIBRATION);
      parameterF = atof(token2);
      setPidSpShift(&pitchAttitudePidSettings, parameterF);
	  printf( "Pitch Angular Calibration: %4.3f\n",getPidSpShift(&pitchAttitudePidSettings));


      break;

    case PID_SETTING_MESSAGE:
	
       printf("%s\n", buf);

    
	  qToken(cmd, token2,lenth, ATTITUDE_ROLL_P);
      parameterF = atof(token2);
      setPGain(&rollAttitudePidSettings, parameterF);
       printf( "Attitude Roll P Gain=%f\n", getPGain(&rollAttitudePidSettings));

      
      qToken(cmd, token2,lenth, ATTITUDE_ROLL_I);
      parameterF = atof(token2);
      setIGain(&rollAttitudePidSettings, parameterF);
      printf("Attitude Roll I Gain=%f\n", getIGain(&rollAttitudePidSettings));

    
       qToken(cmd, token2,lenth,ATTITUDE_ROLL_I_LIMIT );
      parameterF = atof(token2);
      setILimit(&rollAttitudePidSettings, parameterF);
       printf( "Attitude Roll I Output Limit=%f\n",
                getILimit(&rollAttitudePidSettings));

       qToken(cmd, token2,lenth,ATTITUDE_ROLL_D );
      parameterF = atof(token2);
      setDGain(&rollAttitudePidSettings, parameterF);
       printf("Attitude Roll D Gain=%f\n", getDGain(&rollAttitudePidSettings));

       qToken(cmd, token2,lenth,ATTITUDE_PITCH_P );
      parameterF = atof(token2);
      setPGain(&pitchAttitudePidSettings, parameterF);
       printf( "Attitude Pitch P Gain=%f\n", getPGain(&pitchAttitudePidSettings));

       qToken(cmd, token2,lenth, ATTITUDE_PITCH_I);
      parameterF = atof(token2);
      setIGain(&pitchAttitudePidSettings, parameterF);
      printf( "Attitude Pitch I Gain=%f\n", getIGain(&pitchAttitudePidSettings));

       qToken(cmd, token2,lenth, ATTITUDE_PITCH_I_LIMIT);
      parameterF = atof(token2);
      setILimit(&pitchAttitudePidSettings, parameterF);
      printf( "Attitude Pitch I Output Limit=%f\n",
                getILimit(&pitchAttitudePidSettings));

       qToken(cmd, token2,lenth, ATTITUDE_PITCH_D);
      parameterF = atof(token2);
      setDGain(&pitchAttitudePidSettings, parameterF);
       printf( "Attitude Pitch D Gain=%f\n", getDGain(&pitchAttitudePidSettings));


       qToken(cmd, token2,lenth,ATTITUDE_YAW_P );
      parameterF = atof(token2);
      setPGain(&yawAttitudePidSettings, parameterF);
       printf( "Attitude Yaw P Gain=%f\n", getPGain(&yawAttitudePidSettings));

       qToken(cmd, token2,lenth, ATTITUDE_YAW_I);
      parameterF = atof(token2);
      setIGain(&yawAttitudePidSettings, parameterF);
       printf( "Attitude Yaw I Gain=%f\n", getIGain(&yawAttitudePidSettings));

       qToken(cmd, token2,lenth, ATTITUDE_YAW_I_LIMIT);
      parameterF = atof(token2);
      setILimit(&yawAttitudePidSettings, parameterF);
       printf( "Attitude Yaw I Output Limit=%f\n", getILimit(&yawAttitudePidSettings));
	   
       qToken(cmd, token2,lenth, ATTITUDE_YAW_D);
      parameterF = atof(token2);
      setDGain(&yawAttitudePidSettings, parameterF);
       printf( "Attitude Yaw D Gain=%f\n", getDGain(&yawAttitudePidSettings));

       qToken(cmd, token2,lenth, RATE_ROLL_P);
      parameterF = atof(token2);
      setPGain(&rollRatePidSettings, parameterF);
       printf( "Rate Roll P Gain=%f\n", getPGain(&rollRatePidSettings));
    
       qToken(cmd, token2,lenth, RATE_ROLL_I);
      parameterF = atof(token2);
      setIGain(&rollRatePidSettings, parameterF);
       printf( "Rate Roll I Gain=%f\n", getIGain(&rollRatePidSettings));
     
       qToken(cmd, token2,lenth, RATE_ROLL_I_LIMIT);
      parameterF = atof(token2);
      setILimit(&rollRatePidSettings, parameterF);
       printf( "Rate Roll I Output Limit=%f\n",
                getILimit(&rollRatePidSettings));
      
       qToken(cmd, token2,lenth,RATE_ROLL_D);
      parameterF = atof(token2);
      setDGain(&rollRatePidSettings, parameterF);
      printf( "Rate Roll D Gain=%f\n", getDGain(&rollRatePidSettings));                                                                                                                                                                                                                                                     

    
       qToken(cmd, token2,lenth,RATE_PITCH_P );
      parameterF = atof(token2);
      setPGain(&pitchRatePidSettings, parameterF);
      printf( "Rate Pitch P Gain=%f\n", getPGain(&pitchRatePidSettings));
   
       qToken(cmd, token2,lenth,RATE_PITCH_I );
      parameterF = atof(token2);
      setIGain(&pitchRatePidSettings, parameterF);
       printf( "Rate Pitch I Gain=%f\n", getIGain(&pitchRatePidSettings));
     
       qToken(cmd, token2,lenth,RATE_PITCH_I_LIMIT );
      parameterF = atof(token2);
      setILimit(&pitchRatePidSettings, parameterF);
       printf( "Rate Pitch I Output Limit=%f\n",
                getILimit(&pitchRatePidSettings));
  
       qToken(cmd, token2,lenth, RATE_PITCH_D);
      parameterF = atof(token2);
      setDGain(&pitchRatePidSettings, parameterF);
       printf( "Rate Pitch D Gain=%f\n", getDGain(&pitchRatePidSettings));

    
     qToken(cmd, token2,lenth,RATE_YAW_P );
      parameterF = atof(token2);
      setPGain(&yawRatePidSettings, parameterF);
       printf( "Rate Yaw P Gain=%f\n", getPGain(&yawRatePidSettings));
     
       qToken(cmd, token2,lenth, RATE_YAW_I);
      parameterF = atof(token2);
      setIGain(&yawRatePidSettings, parameterF);
      printf( "Rate Yaw I Gain=%f\n", getIGain(&yawRatePidSettings));
    
       qToken(cmd, token2,lenth,RATE_YAW_I_LIMIT );
      parameterF = atof(token2);
      setILimit(&yawRatePidSettings, parameterF);
       printf("Rate Yaw I Output Limit=%f\n",
                getILimit(&yawRatePidSettings));

       qToken(cmd, token2,lenth, RATE_YAW_D);
      parameterF = atof(token2);
      setDGain(&yawRatePidSettings, parameterF);
       printf( "Rate Yaw D Gain=%f\n", getDGain(&yawRatePidSettings));
      break;
	  default:
	  	printf("unknow message\n");
  }

}