Пример #1
0
static uint8_t Tune(const CLS1_StdIOType *io, uint8_t channel, MOT_MotorDevice *motorHandle) {
	#define TUNE_MOTOR_PERCENT 20
  uint16_t dac;
  int i;
  QuadTime_t timing;
  uint8_t buf[48];
  uint8_t res;
 
//#if PL_HAS_DRIVE
//  DRV_SetMode(DRV_MODE_NONE); /* turn off drive mode */
//#endif

MOT_SetSpeedPercent(motorHandle, TUNE_MOTOR_PERCENT);
  CLS1_SendStr((uint8_t*)"Tuning channel...\r\n", io->stdOut);
  res = ERR_FAILED;
  for(i=0,dac=0;dac<=MCP4728_MAX_DAC_VAL;i++) {
    UTIL1_strcpy(buf, sizeof(buf), (uint8_t*)"Channel: ");
    UTIL1_chcat(buf, sizeof(buf), (uint8_t)('A'+channel)); /* 0:A, 1:B, 2:C, 3:D */
    UTIL1_strcat(buf, sizeof(buf), (uint8_t*)" DAC: 0x");
    UTIL1_strcatNum16Hex(buf, sizeof(buf), dac);
    UTIL1_chcat(buf, sizeof(buf), ' ');
    CLS1_SendStr(buf, io->stdOut);
    if (MCP4728_FastWriteDAC(channel, dac)!=ERR_OK) { /* writes single channel DAC value, not updating EEPROM */
      CLS1_SendStr((uint8_t*)"ERROR writing DAC channel!\r\n", io->stdErr);
      res = ERR_FAILED;
      break;
    }
    WAIT1_WaitOSms(100); /* wait some time to allow DAC and OP-Amp change */
    if (Measure(channel, &timing)==ERR_OK) {
      buf[0] = '\0';
      UTIL1_strcatNum8u(buf, sizeof(buf), timing.highPercent);
      UTIL1_strcat(buf, sizeof(buf), (uint8_t*)"% high, low ");
      UTIL1_strcatNum8u(buf, sizeof(buf), timing.lowPercent);
      UTIL1_strcat(buf, sizeof(buf), (uint8_t*)"%\r\n");
      CLS1_SendStr(buf, io->stdOut);
      if (timing.highPercent==50 || timing.lowPercent==50) {
        CLS1_SendStr((uint8_t*)"Set!\r\n", io->stdErr);
        CLS1_SendStr((uint8_t*)"Writing to EEPROM...\r\n", io->stdOut);
        if (MCP4728_WriteDACandEE(channel, dac)!=ERR_OK) {
          CLS1_SendStr((uint8_t*)"ERROR writing DAC/EEPROM\r\n", io->stdErr);
          res = ERR_FAILED;
          break;
        }
        CLS1_SendStr((uint8_t*)"...done!\r\n", io->stdOut);
        res = ERR_OK;
        break; /* go to next channel */
      }
      dac += 0x1; /* smaller increase */
    } else {
      CLS1_SendStr((uint8_t*)"No signal\r\n", io->stdErr);
      dac += 0x10; /* larger increase */
    }
  } /* for finding DAC value */
  MOT_SetSpeedPercent(motorHandle, 0); /* turn off again */
  if (res!=ERR_OK) {
    CLS1_SendStr((uint8_t*)"ERROR!\r\n", io->stdErr);
  }
  CLS1_SendStr((uint8_t*)"Tuning finished!\r\n", io->stdOut);
  return res;
}
Пример #2
0
byte MCP4728_ParseCommand(const unsigned char *cmd, bool *handled, const CLS1_StdIOType *io) {
  const unsigned char *p;

  if (UTIL1_strcmp((char*)cmd, CLS1_CMD_HELP)==0 || UTIL1_strcmp((char*)cmd, "MCP4728 help")==0) {
    *handled = TRUE;
    return PrintHelp(io);
  } else if ((UTIL1_strcmp((char*)cmd, CLS1_CMD_STATUS)==0) || (UTIL1_strcmp((char*)cmd, "MCP4728 status")==0)) {
    *handled = TRUE;
    return PrintStatus(io);
  } else if (UTIL1_strcmp((char*)cmd, "MCP4728 reset")==0) {
    *handled = TRUE;
    if (MCP4728_Reset()!=ERR_OK) {
      return ERR_FAILED;
    } else {
      return ERR_OK;
    }
  } else if (UTIL1_strcmp((char*)cmd, "MCP4728 wakeup")==0) {
    *handled = TRUE;
    if (MCP4728_Wakeup()!=ERR_OK) {
      return ERR_FAILED;
    } else {
      return ERR_OK;
    }
  } else if (UTIL1_strcmp((char*)cmd, "MCP4728 update")==0) {
    *handled = TRUE;
    if (MCP4728_Update()!=ERR_OK) {
      return ERR_FAILED;
    } else {
      return ERR_OK;
    }
  } else if (UTIL1_strncmp((char*)cmd, "MCP4728 fastwrite all ", sizeof("MCP4728 fastwrite all ")-1)==0) {
    uint16_t dac[4];
    uint8_t pd[4];
    int i;

    *handled = TRUE;
    p = cmd+sizeof("MCP4728 fastwrite all ")-1;
    for(i=0;i<4;i++) { /* init */
      dac[i] = 0;
      pd[i] = 0;
    }
    for(i=0;i<4;i++) {
      if (UTIL1_ScanHex16uNumber(&p, &dac[i])!=ERR_OK) {
        break;
      }
      dac[i] &= MCP4728_MAX_DAC_VAL; /* ensure it is 12 bits */
    }
    if (i!=4) {
      CLS1_SendStr((unsigned char*)"**** Not enough values, 4 expected!\r\n", io->stdErr);
      return ERR_FAILED;
    }
    if (MCP4728_FastWriteAllDAC(dac, sizeof(dac), pd, sizeof(pd))!=ERR_OK) {
      CLS1_SendStr((unsigned char*)"**** FastWrite failed.\r\n", io->stdErr);
      return ERR_FAILED;
    } else {
      return ERR_OK;
    }
  } else if (UTIL1_strncmp((char*)cmd, "MCP4728 fastwrite ", sizeof("MCP4728 fastwrite ")-1)==0) {
    /* e.g. MCP4728 fastwrite 2 0x512 */
    uint16_t dac=0;
    uint8_t channel=0;

    *handled = TRUE;
    p = cmd+sizeof("MCP4728 fastwrite ")-1;
    if (UTIL1_ScanDecimal8uNumber(&p, &channel)!=ERR_OK || channel>3) {
      CLS1_SendStr((unsigned char*)"**** error parsing channel, must be 0, 1, 2 or 3!\r\n", io->stdErr);
      return ERR_FAILED;
    }
    if (UTIL1_ScanHex16uNumber(&p, &dac)!=ERR_OK || dac>MCP4728_MAX_DAC_VAL) {
      CLS1_SendStr((unsigned char*)"**** error parsing 12bit hex value, must start with 0x!\r\n", io->stdErr);
      return ERR_FAILED;
    }
    dac &= MCP4728_MAX_DAC_VAL; /* ensure it is 12 bits */
    if (MCP4728_FastWriteDAC(channel, dac)!=ERR_OK) {
      CLS1_SendStr((unsigned char*)"**** Write channel DAC failed!\r\n", io->stdErr);
      return ERR_FAILED;
    } else {
      return ERR_OK;
    }
  } else if (UTIL1_strncmp((char*)cmd, "MCP4728 write ", sizeof("MCP4728 write ")-1)==0) {
    uint16_t dac;
    uint8_t ch, res;

    *handled = TRUE;
    res = ERR_OK;
    p = cmd+sizeof("MCP4728 write ")-1;
    if (UTIL1_ScanDecimal8uNumber(&p, &ch)!=ERR_OK || ch>3) {
      CLS1_SendStr((unsigned char*)"**** Failed reading channel. Must be 0, 1, 2 or 3\r\n", io->stdErr);
      res = ERR_FAILED;
    } else {
      if (UTIL1_ScanHex16uNumber(&p, &dac)!=ERR_OK) {
        CLS1_SendStr((unsigned char*)"**** Failed DAC value, must start with 0x\r\n", io->stdErr);
        res = ERR_FAILED;
      } else {
        if (MCP4728_WriteDACandEE(ch, dac)!=ERR_OK) {
          CLS1_SendStr((unsigned char*)"**** Write failed.\r\n", io->stdErr);
          res = ERR_FAILED;
        }
      }
    }
    return res;
  }
  return ERR_OK;
}