CAL_STATE pidHysterisis(int group) { if (RunEvery(&getPidGroupDataTable(group)->timer) > 0) { Print_Level l = getPrintLevel(); //setPrintLevelInfoPrint(); float boundVal = 150.0; float extr = GetPIDPosition(group); if (bound(0, extr, boundVal, boundVal)) {// check to see if the encoder has moved //we have not moved // println_I("NOT moved ");p_fl_I(extr); if (getPidGroupDataTable(group)->calibration.state == forward) { incrementHistoresis(group); } else if (getPidGroupDataTable(group)->calibration.state == backward) { decrementHistoresis(group); } int historesisBound = 25; if (getPidGroupDataTable(group)->config.lowerHistoresis < (-historesisBound) && getPidGroupDataTable(group)->calibration.state == backward) { println_E("Backward Motor seems damaged, more then counts of historesis #"); p_int_I(group); getPidGroupDataTable(group)->calibration.state = forward; } if (getPidGroupDataTable(group)->config.upperHistoresis > (historesisBound) && getPidGroupDataTable(group)->calibration.state == forward) { println_E("Forward Motor seems damaged, more then counts of historesis #"); p_int_I(group); getPidGroupDataTable(group)->calibration.state = done; } } else { pidReset(group, 0); setOutput(group, 0); println_E("Moved "); p_fl_E(extr); if (getPidGroupDataTable(group)->calibration.state == forward) { println_I("Backward Calibrated for link# "); p_int_I(group); getPidGroupDataTable(group)->calibration.state = backward; } else { println_I("Calibration done for link# "); p_int_I(group); getPidGroupDataTable(group)->calibration.state = done; float offset = .9; getPidGroupDataTable(group)->config.lowerHistoresis *= offset; getPidGroupDataTable(group)->config.upperHistoresis *= offset; calcCenter(group); } } if (getPidGroupDataTable(group)->calibration.state == forward) { setOutput(group, 1.0f); } else if (getPidGroupDataTable(group)->calibration.state == backward) { setOutput(group, -1.0f); } setPrintLevel(l); } if (getPidGroupDataTable(group)->calibration.state == done) SetPIDCalibrateionState(group, CALIBRARTION_DONE); return getPidGroupDataTable(group)->calibration.state; }
void UserInit(void){ StartCritical(); //println_W(startmessage);// All printfDEBUG functions do not need to be removed from code if debug is disabled //#if defined(DEBUG) // ConfigureUART(115200); // if(GetChannelMode(16)!=IS_UART_TX) // setMode(16,IS_UART_TX); //#endif setPrintLevelInfoPrint(); println_I(/*PSTR*/("\e[1;1H\e[2J ***Starting User initialization***")); InitFlagPins(); InitBankLEDs(); SetPowerState0(0,0); SetPowerState1(0,0); //_delay_ms(1); #if defined(WPIRBE) SPISlaveInit(); #endif //_delay_ms(100); println_I(/*PSTR*/("Starting Pin Functions")); InitPinFunction(); println_I(/*PSTR*/("Starting Pin Modes")); InitPinModes(); int i=0; //println_I(/*PSTR*/("Starting hardware modes")); for(i=0;i<GetNumberOfIOChannels();i++){ initPinState(i); configAdvancedAsyncNotEqual(i,10); setAsyncLocal(i,true) ; } //println_I(/*PSTR*/("DONE pin initialization")); //println_I(/*PSTR*/("Adding IO Initialization")); addNamespaceToList((NAMESPACE_LIST *)get_bcsIoNamespace()); //println_I(/*PSTR*/("Adding IO.SETMODE Initialization")); addNamespaceToList((NAMESPACE_LIST *)get_bcsIoSetmodeNamespace()); //println_I(/*PSTR*/("Adding Internal Initialization")); addNamespaceToList((NAMESPACE_LIST *)get_internalNamespace()); setNoAsyncMode(true); setIgnoreAddressing(true); //SetPinTris(0,OUTPUT); //SetDIO(0,OFF); #if defined(USE_AS_LIBRARY) InitializeUserCode(); #endif EndCritical(); println_I(/*PSTR*/("Starting Core")); // setMode(22,IS_DO); // setMode(23,IS_DI); // println_I(/*PSTR*/("Pin done")); }
void printValues(){ int i; println_I("Values"); for(i=0;i<GetNumberOfIOChannels();i++){ println_I("\t# ");p_int_I(i); print_I("\tCurrent ");p_int_I(getBcsIoDataTable()[i].PIN.currentValue); //print_I("\tPrevious ");p_int_I(getBcsIoDataTable()[i].PIN.previousValue); } }
void printModes(){ int i; println_I("Modes"); for(i=0;i<GetNumberOfIOChannels();i++){ println_I("\t# ");p_int_I(i); print_I("\tCurrent ");printMode(getBcsIoDataTable()[i].PIN.currentChannelMode,INFO_PRINT); //print_I("\tPrevious ");printMode(getBcsIoDataTable()[i].PIN.previousChannelMode,INFO_PRINT); } }
void printConfigurations(){ int i; println_I("Configurations"); for(i=0;i<GetNumberOfIOChannels();i++){ println_I("\t# ");p_int_I(i); //print_I("\tCurrent ");p_int_I(getBcsIoDataTable()[i].PIN.currentConfiguration); //print_I("\tPrevious ");p_int_I(getBcsIoDataTable()[i].PIN.previousConfiguration); } }
void LoadEEstore(void){ if(loadEEDone) return; println_I("Loading eeprom data"); loadEEDone=TRUE; int i; for (i=0;i<NUM_PID_GROUPS;i++){ GetEEPRomData((pidValSize*i),(pidValSize*i)+pidValSize,pidEEPRomVal[i].stream); } println_I("Done loading eeprom data"); }
void printAsync(){ int i; println_I("Async Data ");p_fl_I(getMs()); for(i=0;i<GetNumberOfIOChannels();i++){ println_I("\t# ");p_int_I(i); print_I("\tCurrent ");p_int_I(getBcsIoDataTable()[i].PIN.asyncDataCurrentVal); print_I("\tPrevious ");p_int_I(getBcsIoDataTable()[i].PIN.asyncDataPreviousVal); print_I("\tMode ");printAsyncType(getBcsIoDataTable()[i].PIN.asyncDataType); print_I("\tIteration ");p_fl_I(getBcsIoDataTable()[i].PIN.asyncDataTime.setPoint); print_I("\tLast ");p_fl_I(getBcsIoDataTable()[i].PIN.asyncDataTime.MsTime); } }
void InitSPI(void){ println_I("Initializing the SPI perpheral"); mPORTGOpenDrainOpen(BIT_6);// Clock is output mPORTGOpenDrainOpen(BIT_8);// Data Out is an output SPI_SCK_IO=1; SPI_SDO_IO=1; OpenSPI2(SPI_MODE8_ON|ENABLE_SDO_PIN|SLAVE_ENABLE_OFF|SPI_CKE_ON|MASTER_ENABLE_ON|SEC_PRESCAL_8_1|PRI_PRESCAL_64_1, SPI_ENABLE); println_I("Setting up SPI perpheral"); SetCoProcMode(0,IS_SPI_SCK); SetCoProcMode(1,IS_SPI_MISO); SetCoProcMode(2,IS_SPI_MOSI); }
float runPdVelocityFromPointer(PD_VEL* vel, float currentState,float KP, float KD){ float currentTime = getMs(); float timeMsDiff = (currentTime -vel->lastTime); float timeDiff = timeMsDiff/1000; float posDiff=currentState -vel->lastPosition; float currentVelocity = posDiff/timeDiff; //float velocityDiff = currentVelocity-vel->lastVelocity; float velocityDiff=0; float proportional = currentVelocity-vel->unitsPerSeCond; float set = (proportional*KP)+(velocityDiff*KD)*timeMsDiff; vel->currentOutputVel-=(set); if (vel->currentOutputVel>200){ vel->currentOutputVel=200; }else if(vel->currentOutputVel<-200){ vel->currentOutputVel=-200; } println_I("\t Velocity: set= ");p_fl_I(vel->unitsPerSeCond );print_I(" ticks/seCond" ); println_I("\t current state= ");p_fl_I(currentState );print_I(" ticks" ); println_I("\t last state= ");p_fl_I(vel->lastPosition );print_I(" ticks" ); println_I("\t position diff= ");p_fl_I(posDiff );print_I(" ticks" ); println_I("\t MS diff= ");p_fl_I(timeMsDiff ); println_I("\t current= ");p_fl_I(currentVelocity );print_I(" ticks/seCond" ); println_I("\t Velocity offset= ");p_fl_I(set ); println_I("\t Velocity set= ");p_fl_I(vel->currentOutputVel ); //cleanup vel->lastPosition=currentState; vel->lastVelocity=currentVelocity; vel->lastTime=currentTime; return vel->currentOutputVel; }
void initPIDChans(BYTE group){ if(dyPid[group].inputChannel==DYPID_NON_USED || dyPid[group].outputChannel==DYPID_NON_USED) return; switch(dyPid[group].inputMode){ case IS_COUNTER_INPUT_INT: case IS_COUNTER_INPUT_DIR: case IS_COUNTER_INPUT_HOME: dyPid[group].inputChannel = getCounterIntChannnel( channelToCounterGroup(dyPid[group].inputChannel)); StartCounterInput(dyPid[group].inputChannel); break; } println_I("Setting Modes for PID"); SetCoProcMode(dyPid[group].inputChannel,dyPid[group].inputMode); SetCoProcMode(dyPid[group].outputChannel,dyPid[group].outputMode); SyncModes(); if(dyPid[group].inputMode== IS_ANALOG_IN){ pidGroups[group].SetPoint=GetValFromAsync(dyPid[group].inputChannel); }else{ pidGroups[group].SetPoint=0; } }
void CheckRev(void) { LoadCorePacket(&downstreamPacketTemp); downstreamPacketTemp.use.head.Method = BOWLER_GET; downstreamPacketTemp.use.head.RPC = GetRPCValue("_rev"); downstreamPacketTemp.use.head.DataLegnth = 4; SendPacketToCoProc(&downstreamPacketTemp); if ((downstreamPacketTemp.use.data[0] == MAJOR_REV) && (downstreamPacketTemp.use.data[1] == MINOR_REV) && (downstreamPacketTemp.use.data[2] == FIRMWARE_VERSION)) { SetColor(0, 0, 1); } else { //SetColor(1, 0, 0); println_I("Rev. Check Failed! AVR:"); p_int_I(downstreamPacketTemp.use.data[0]); print_I("."); p_int_I(downstreamPacketTemp.use.data[1]); print_I("."); p_int_I(downstreamPacketTemp.use.data[2]); print_I(" PIC:"); p_int_I(MAJOR_REV); print_I("."); p_int_I(MINOR_REV); print_I("."); p_int_I(FIRMWARE_VERSION); } }
void SendPacketToSPI(BowlerPacket * Packet){ if(!isSPI(GetChannelMode(Packet->use.data[0]))){ println_I("channel is not SPI"); return; } SendPacketToSPIFromArray(Packet->use.head.DataLegnth-6,Packet->use.data+1); }
BOOL onCartesianPost(BowlerPacket *Packet){ Print_Level l = getPrintLevel(); switch(Packet->use.head.RPC){ case _SLI: if(FifoGetPacketSpaceAvailible(&packetFifo)>0){ if(Packet->use.data[0]==1){ processLinearInterpPacket(Packet); }else{ println_I("Cached linear Packet ");p_int_I(FifoGetPacketSpaceAvailible(&packetFifo)); FifoAddPacket(&packetFifo,Packet); } Packet->use.head.Method = BOWLER_STATUS; INT32_UNION tmp; tmp.Val=FifoGetPacketSpaceAvailible(&packetFifo); Packet->use.data[0]=tmp.byte.FB; Packet->use.data[1]=tmp.byte.TB; Packet->use.data[2]=tmp.byte.SB; Packet->use.data[3]=tmp.byte.LB; tmp.Val=SIZE_OF_PACKET_BUFFER; Packet->use.data[4]=tmp.byte.FB; Packet->use.data[5]=tmp.byte.TB; Packet->use.data[6]=tmp.byte.SB; Packet->use.data[7]=tmp.byte.LB; Packet->use.head.DataLegnth=4+4+4; if(tmp.Val == 0){ full=TRUE; } setPrintLevel(l); }else{ println_I("###ERROR BUFFER FULL!!");p_int_I(FifoGetPacketSpaceAvailible(&packetFifo)); setPrintLevel(l); ERR(Packet,33,33); } return TRUE; case PRCL: cancelPrint(); READY(Packet,35,35); return TRUE; } return FALSE; }
void PowerCycleAVR(){ println_I("Power cycling the AVR"); HoldAVRReset(); // getCommand(progmode); // writeLowFuse(); // writeHighFuse(); // writeExtendedFuse(); ReleaseAVRReset(); }
int resetPositionMine(int group, int current){ println_I("Resetting PID Local ");p_int_I(group);print_I(" to ");p_int_I(current);print_I(" from ");p_fl_I(getPositionMine(group)); if(group<numPidMotors){ setCurrentValue(group, current); }else{ resetHeater(group, current); } return getPositionMine(group); }
void InitPinStates(void){ SyncModes(); println_I("Modes synced, initializing channels"); initAdvancedAsync(); int i; for (i=0;i<GetNumberOfIOChannels();i++){ //DelayMs(10); SetChannelMode(i,GetChannelMode(i)); } }
BYTE setXYZ(float x, float y, float z,float ms){ updateCurrentPositions(); float t0=0,t1=0,t2=0; if(hwMap.iK_callback( x, y, z, &t0, &t1, &t2)==0){ println_I("New target angles t1=");p_fl_I(t0);print_I(" t2=");p_fl_I(t1);print_I(" t3=");p_fl_I(t2); setLinkAngle(0,t0,ms); setLinkAngle(1,t1,ms); setLinkAngle(2,t2,ms); }else{ println_E("Interpolate failed, can't reach: x=");p_fl_E(x);print_E(" y=");p_fl_E(y);print_E(" z=");p_fl_E(z); } }
void InitPID(void){ BYTE i; //WORD loop; for (i=0;i<NUM_PID_GROUPS;i++){ //DyPID values dyPid[i].inputChannel=DYPID_NON_USED; dyPid[i].outputChannel=DYPID_NON_USED; dyPid[i].inputMode=0; dyPid[i].outputMode=0; pidGroups[i].Enabled=FALSE; pidGroups[i].channel = i; vel[i].enabled=FALSE; vel[i].K.P=.1; limits[i].type=NO_LIMIT; LoadPIDvals(&pidGroups[i],&dyPid[i]); if( dyPid[i].inputChannel==DYPID_NON_USED || dyPid[i].outputChannel==DYPID_NON_USED || dyPid[i].outputChannel==dyPid[i].inputChannel) { dyPid[i].inputChannel=DYPID_NON_USED; dyPid[i].outputChannel=DYPID_NON_USED; WritePIDvalues(&pidGroups[i],&dyPid[i]); } force[i].MsTime=0; force[i].setPoint=200; } InitilizePidController( pidGroups, vel, NUM_PID_GROUPS, &getPositionMine, &setOutputMine, &resetPositionMine, //&asyncCallback, &onPidConfigureMine, &checkPIDLimitEventsMine); for (i=0;i<NUM_PID_GROUPS;i++){ printPIDvals(i); if(pidGroups[i].Enabled){ initPIDChans(i); println_I("Resetting PID channel from init"); int value = 0; if(dyPid[i].inputMode == IS_ANALOG_IN) value = 512; pidReset(i,value); } } }
void UserInit(void){ //setPrintStream(&USBPutArray); setPrintLevelInfoPrint(); println_I("\n\nStarting PIC initialization"); //DelayMs(1000); hardwareInit(); println_I("Hardware Init done"); ReleaseAVRReset(); CheckRev(); LoadEEstore(); LoadDefaultValues(); CartesianControllerInit(); InitPID(); UpdateAVRLED(); lockServos(); setPrintLevelInfoPrint(); BOOL brown = getEEBrownOutDetect(); setCoProcBrownOutMode(brown); setBrownOutDetect(brown); println_I("###Starting PIC In Debug Mode###\n");// All printfDEBUG functions do not need to be removed from code if debug is disabled DelayMs(1000); //setPrintLevelErrorPrint(); println_E("Error level printing"); println_W("Warning level printing"); println_I("Info level printing"); }
void cancelPrint(){ Print_Level l = getPrintLevel(); println_I("Cancel Print"); setPrintLevel(l); while(FifoGetPacketCount(&packetFifo)>0){ FifoGetPacket(&packetFifo,&linTmpPack); } setInterpolateXYZ(0, 0, getmaxZ(), 0); ZeroPID(hwMap.Extruder0.index); SetPIDTimed(hwMap.Heater0.index,0,0); }
void StopSPI(BYTE pin){ if (isSPI(GetChannelMode(pin))){ CloseSPI2(); _RG6=1; _RG8=1; SDI_TRIS=INPUT; SDO_TRIS=INPUT; SCK_TRIS=INPUT; println_I("Clearing up SPI perpheral"); SetCoProcMode(0,IS_DI); SetCoProcMode(1,IS_DI); SetCoProcMode(2,IS_DI); } }
uint8_t Bowler_Server_Local(BowlerPacket * Packet){ Print_Level l = getPrintLevel(); //setPrintLevelNoPrint(); if (GetBowlerPacket_arch(Packet)){ //setLed(1,1,1); if(Packet->use.head.RPC != _PNG){ println_I("Got:");printPacket(Packet,INFO_PRINT); } if ( (CheckAddress(MyMAC.v,Packet->use.head.MAC.v) == true) || ((CheckAddress((uint8_t *)Broadcast.v,(uint8_t *)Packet->use.head.MAC.v) == true) )) { float start=getMs(); Process_Self_Packet(Packet); if(getMs()-start>5){ println_E("Process too long: ");p_fl_E(getMs()-start); } for (i=0;i<6;i++){ Packet->use.head.MAC.v[i]=MyMAC.v[i]; } SetCRC(Packet); start=getMs(); PutBowlerPacket(Packet); if(getMs()-start>5){ println_E("Return too long: ");p_fl_E(getMs()-start); } if(Packet->use.head.RPC != _PNG){ println_I("Response:");printPacket(Packet,INFO_PRINT); } }else{ //println_I("Packet not addressed to me: ");printByteArray(Packet->use.head.MAC.v,6); print_I(" is not mine: ");printByteArray(MyMAC.v,6); } //setLed(0,0,1); setPrintLevel(l); return true; }//Have a packet setPrintLevel(l); return false; }
BOOL bcsIoAsyncEventCallback(BowlerPacket *Packet,BOOL (*pidAsyncCallbackPtr)(BowlerPacket *)){ int i; BOOL update=FALSE; println_W("Async ");print_W(ioNSName); for(i=0;i<GetNumberOfIOChannels();i++){ //println_W("Checking ");p_int_W(i); if(pushAsyncReady(i)){ update=TRUE; } } if(update){ println_I(__FILE__);println_I("Async: "); populateGACV(Packet); Packet->use.head.Method=BOWLER_ASYN; FixPacket(Packet); printBowlerPacketDEBUG(Packet,INFO_PRINT); if(pidAsyncCallbackPtr!=NULL){ pidAsyncCallbackPtr(Packet); } } println_W("Done ");print_W(ioNSName); return FALSE; }
void SendPacketToSPIFromArray(BYTE numBytes,BYTE * data){ BYTE ss = data[0]; if(ss<3){ println_I("invalid SS pin"); return; } if(!SetCoProcMode(ss,IS_DO)) SetChannelValueCoProc(ss,1); SetChannelValueCoProc(ss,0); BYTE i; for (i=0;i<numBytes;i++){ data[i+1]=GetByteSPI(data[i+1]); } SetChannelValueCoProc(ss,1); }
void PushAllDiVal(){ #if defined(WPIRBE) return; #endif println_I("Pushing digital"); //FlagBusy_IO=1; //_delay_us(800); sendHeader(4+24,"dasn"); int i=0; for(i=0;i<24;i++){ send(GetValFromAsync(i)); } //FlagBusy_IO=0; //_delay_us(800); }
void CheckSwitches(void){ Print_Level l = getPrintLevel(); setPrintLevelInfoPrint(); switched=0; volt = GetRawVoltage(); BOOL up = FALSE; BYTE reg = isRegulated_0(); if (bankState[0] != reg ){ bankState[0]=reg; up=TRUE; } reg = isRegulated_1(); if (bankState[1] != reg){ bankState[1] = reg; up=TRUE; } if ((lastVolt>RawVoltageMin) && (volt<RawVoltageMin)){ up=TRUE; lastVolt = volt; } if ((lastVolt<RawVoltageMin) && (volt>RawVoltageMin)){ up=TRUE; lastVolt = volt; } if(up){ println_I("\nVoltage on raw: \t"); p_fl_I(volt); println_I("Voltage on bank0: \t"); p_fl_I(GetRail0Voltage()); println_I("Voltage on bank1:\t"); p_fl_I(GetRail1Voltage()); println_I("Pushing upstream Power Packet bank 0: ");p_int_I(bankState[0]);print_I(" bank 1: ");p_int_I(bankState[1]); println_I("Power Code 0: ");p_int_I(GetRawVoltageCode(0)); println_I("Power Code 1 : ");p_int_I(GetRawVoltageCode(1)); println_I("Raw: ");p_fl_I(GetRawVoltage()); UpstreamPushPowerChange(); } setPrintLevel(l); }
BOOL onCartesianPacket(BowlerPacket *Packet){ Print_Level l = getPrintLevel(); println_I("Packet Checked by Cartesian Controller"); BOOL ret = FALSE; switch(Packet->use.head.Method){ case BOWLER_POST: ret = onCartesianPost(Packet); break; case BOWLER_GET: ret = onCartesianGet(Packet); break; case BOWLER_CRIT: ret = onCartesianCrit(Packet); break; } setPrintLevel(l); return ret; }
float setLinkAngle(int index, float value, float ms){ int localIndex=linkToHWIndex(index); float v = value/getLinkScale(index); if( index ==0|| index ==1|| index ==2 ){ // if(v>1650){ // println_E("Upper Capped link ");p_int_E(index);print_E(", attempted: ");p_fl_E(value); // v=1650; // } // if(v<-6500){ // v=-6500; // println_E("Lower Capped link ");p_int_E(index);print_E(", attempted: ");p_fl_E(value); // } } println_I("Setting position from cartesian controller ");p_int_I(index);print_I(" to ");p_fl_I(v); return SetPIDTimed(localIndex,v,ms); }
void setOutputMine(int group, float v){ if( dyPid[group].outputChannel==DYPID_NON_USED|| ((pidGroups[group].Enabled == FALSE) && (vel[group].enabled==FALSE))) return; Print_Level l = getPrintLevel(); setPrintLevelNoPrint(); int val = (int)(v); //BYTE center = getBcsIoDataTable()[dyPid[group].outputChannel].PIN.currentConfiguration; if(dyPid[group].outputMode == IS_SERVO){ val += 128; if (val>254) val=254; if(val<0) val=0; }else if(dyPid[group].outputMode == IS_DO){ if(val>0) val=1; else val=0; }else{ val += 128; if (val>255) val=255; if(val<0) val=0; } int set = (int)val; if (dyPid[group].outVal==set){ //if(!(RunEvery(&force[chan->channel])>0)) return; }else{ print_I(" Setting PID output, was ");p_int_I(dyPid[group].outVal);print_I(" is now: ");p_int_I(set);print_I(" on DyIO chan: ");p_int_I(dyPid[group].outputChannel);print_I(", "); } dyPid[group].outVal=set; println_I("PID setting output for group: ");p_int_I(group); SetChannelValueCoProc(dyPid[group].outputChannel,dyPid[group].outVal); setPrintLevel(l); }
void LoadPIDvals(AbsPID * pid, DYIO_PID * dy){ LoadEEstore(); BYTE i = pid->channel; if(pidEEPRomVal[i].outputChannel==pidEEPRomVal[i].inputChannel) return; if(pidEEPRomVal[i].outputChannel>=GetNumberOfIOChannels() ||pidEEPRomVal[i].inputChannel>=GetNumberOfIOChannels() ) return; if(pidEEPRomVal[i].outputMode==pidEEPRomVal[i].inputMode) return; println_I("Using values for chan: ");p_int_I(i); pid->Enabled=pidEEPRomVal[i].Enabled; pid->Polarity=pidEEPRomVal[i].Polarity; //pidChans->Async=pidEEPRomVal[i].Async; dy->inputMode=pidEEPRomVal[i].inputMode; dy->outputMode=pidEEPRomVal[i].outputMode; dy->outputChannel=pidEEPRomVal[i].outputChannel; dy->inputChannel=pidEEPRomVal[i].inputChannel; pid->K.P=pidEEPRomVal[i].K.P; pid->K.I=pidEEPRomVal[i].K.I; pid->K.D=pidEEPRomVal[i].K.D; }