int main(void) { /* perform the needed initialization here */ LEDsInit(); Tec1Init(); Tec2Init(); Tec3Init(); Tec4Init(); ADCInit(); DACInit(); UARTInit(); TimerInit(100); for(;;) { if(LeerTec1()==0) { A += 0.1; UARTSendString(msj1); } if(LeerTec2()==0) { A -= 0.1; UARTSendString(msj2); } if(LeerTec3()==0) { A = 0; UARTSendString(msj3); } if(LeerTec4()==0) { UARTSendString(msj4); } } return 0; }
__interrupt void Timer_A (void) { UARTSendString("Capture"); }
/*will send full data about temperature and parameters*/ void sendUARTData(){ sprintf(displayTemp, "now: %i; min: %i; max: %i; thermostate: %i", currentTemp, thermostatSettings.minimalTemp, thermostatSettings.maximalTemp, thermostatSettings.isActive); UARTSendString(displayTemp); UARTSend(0x0A); UARTSend(0x0D); }
uint8_t main(){ char line[ LINE_BUFFER_LENGTH ]; char c; int lineIndex; uint8_t lineIsComment, lineSemiColon; lineIndex = 0; lineSemiColon = 0; lineIsComment = 0; UARTConfig(); STEEPERCONTROLLING_INIT(); UARTSendString( "\n\rWaiting for commands:"); while (1){ c = UARTReceive(); if(( c == '\n') || (c == '\r') ) // End of line reached { if( lineIndex > 0 ) // Line is complete. Then execute! { line[ lineIndex ] = '\0'; // Terminate string #ifdef verbose UARTSendString( "\n\rReceived : "); UARTSendString( line ); #endif // verbose processIncomingLine( line, lineIndex ); lineIndex = 0; } else { // Empty or comment line. Skip block. } lineIsComment = 0; lineSemiColon = 0; UARTSendString("\n\rok"); UARTSendString( "\n\rWaiting for commands:"); } else { if ( (lineIsComment) || (lineSemiColon) ) // Throw away all comment characters { if ( c == ')' ) lineIsComment = 0; // End of comment. Resume line. } else { if ( c <= ' ' ) // Throw away whitepace and control characters { } else if ( c == '/' ) // Block delete not supported. Ignore character. { } else if ( c == '(' ) // Enable comments flag and ignore all characters until ')' or EOL. { lineIsComment = 1; } else if ( c == ';' ) { lineSemiColon = 1; } else if ( lineIndex >= LINE_BUFFER_LENGTH-1 ) { UARTSendString( "\n\rERROR - lineBuffer overflow" ); lineIsComment = 0; lineSemiColon = 0; } else if ( c >= 'a' && c <= 'z' ) // Upcase lowercase { line[ lineIndex++ ] = c-'a'+'A'; } else { line[ lineIndex++ ] = c; } } } } return 0; }
/********************************* * Draw a line from (x0;y0) to (x1;y1). * Bresenham algo from https://www.marginallyclever.com/blog/2013/08/how-to-build-an-2-axis-arduino-cnc-gcode-interpreter/ * int (x1;y1) : Starting coordinates * int (x2;y2) : Ending coordinates **********************************/ void drawLine(float x1, float y1){ #ifdef verbose UARTSendString("\n\rDrawline:fx1, fy1: "); UARTSendFloat(x1,2); UARTSendString(","); UARTSendFloat(y1,2); #endif // verbose // Bring instructions within limits if (x1 >= Xmax) { x1 = Xmax; } if (x1 <= Xmin) { x1 = Xmin; } if (y1 >= Ymax) { y1 = Ymax; } if (y1 <= Ymin) { y1 = Ymin; } #ifdef verbose UARTSendString("\n\rXpos, Ypos: "); UARTSendFloat(Xpos,2); UARTSendString(","); UARTSendFloat(Ypos,2); UARTSendString("\n\rx1, y1: "); UARTSendFloat(x1,2); UARTSendString(","); UARTSendFloat(y1,2); #endif // verbose // Convert coordinates to steps x1 = (int)(x1*StepsPerMillimeterX); y1 = (int)(y1*StepsPerMillimeterY); float x0 = Xpos; float y0 = Ypos; // Let's find out the change for the coordinates long dx = abs(x1-x0); long dy = abs(y1-y0); //int sx = x0<x1 ? StepInc : -StepInc; //int sy = y0<y1 ? StepInc : -StepInc; long i; long over = 0; if (dx > dy) { for (i=0; i<dx; ++i) { x0<x1 ? fowardA(1,steepdelay) : backwardA(1,steepdelay); over+=dy; if (over>=dx) { over-=dx; y0<y1 ? fowardB(1,steepdelay) : backwardB(1,steepdelay); } delay_ms(StepDelay); } } else { for (i=0; i<dy; ++i) { y0<y1 ? fowardB(1,steepdelay) : backwardB(1,steepdelay); over+=dx; if (over>=dy) { over-=dy; x0<x1 ? fowardA(1,steepdelay) : backwardA(1,steepdelay); } delay_ms(StepDelay); } } #ifdef verbose UARTSendString("\n\rdx, dy:"); UARTSendFloat(dx,2); UARTSendString(","); UARTSendFloat(dy,2); UARTSendString("\n\rGoing to ("); UARTSendFloat(x0,2); UARTSendString(","); UARTSendFloat(y0,2); #endif // verbose //Relasing the motor coils set outputs to low MotorX = 0; MororY = 0; MotorZ = 0; // Delay before any next lines are submitted delay_ms(LineDelay); // Update the positions Xpos = x1; Ypos = y1; }
void processIncomingLine( char* line, int charNB ){ int currentIndex = 0; char buffer[ 64 ]; // Hope that 64 is enough for 1 parameter struct point newPos; char *indexY , *indexX; newPos.x = 0.0; newPos.y = 0.0; // Needs to interpret // G1 for moving // G4 P300 (wait 150ms) // G1 X60 Y30 // G1 X30 Y50 // M300 S30 (pen down) // M300 S50 (pen up) // Discard anything with a ( // Discard any other command! while( currentIndex < charNB ) { switch ( line[ currentIndex++ ] ) // Select command, if any { case 'U': upZ(steepdelayZ); break; case 'D': downZ(steepdelayZ); break; case 'G': buffer[0] = line[ currentIndex++ ]; // /!\ Dirty - Only works with 2 digit commands @OPTIMALIZING // buffer[1] = line[ currentIndex++ ]; // buffer[2] = '\0'; buffer[1] = '\0'; switch ( atoi( buffer ) ) // Select G command || line[currentIndex]+48 { case 0: // G00 & G01 - Movement or fast movement. Same here case 1: // /!\ Dirty - Suppose that X is before Y indexX = find_char( line, 'X' ); // Get X/Y position in the string (if any) indexY = find_char( line, 'Y' ); if ( indexY <= 0 ) { newPos.x = atof( indexX + 1); newPos.y = actuatorPos.y; UARTSendString("\n\rGET_---X:"); UARTSendString(indexX); } else if ( indexX <= 0 ) { newPos.y = atof( indexY + 1); newPos.x = actuatorPos.x; UARTSendString("\n\rGET_Y"); } else { #ifdef verbose UARTSendString("\n\rPOS_in_BUFFER_XY--X:"); UARTSendString(indexX); UARTSendString("--Y:"); UARTSendString(indexY); #endif // verbose newPos.y = atof( indexY + 1); indexY = '\0'; newPos.x = atof( indexX + 1); } drawLine(newPos.x, newPos.y ); // Serial.println("ok"); actuatorPos.x = newPos.x; actuatorPos.y = newPos.y; break; } break; case 'M': buffer[0] = line[ currentIndex++ ]; // /!\ Dirty - Only works with 3 digit commands buffer[1] = line[ currentIndex++ ]; buffer[2] = line[ currentIndex++ ]; buffer[3] = '\0'; switch ( atoi( buffer ) ) { case 300: { char* indexS = strchr( line+currentIndex, 'S' ); float Spos = atof( indexS + 1); // Serial.println("ok"); if (Spos == 30) { downZ(steepdelayZ); } if (Spos == 50) { upZ(steepdelayZ); } break; } case 114: // M114 - Repport position UARTSendString( "\n\rAbsolute position : X = " ); UARTSendFloat( actuatorPos.x, 2); UARTSendString( " - Y = " ); UARTSendFloat( actuatorPos.y, 2); break; default: UARTSendString( "\n\rCommand not recognized : M"); UARTSendString( buffer ); } } } }
int main(void) { int value; int junk; millisec = 0; value = SYSTEMConfigWaitStatesAndPB( GetSystemClock() ); // Enable the cache for the best performance CheKseg0CacheOn(); //Setupt input for inteface button JF8 (RA01) (0x02) TRISASET = 0x02; //RED LED - JF9 (RA04) (0x10) TRISACLR = 0x10; ODCACLR = 0x10; LATASET = 0x10; //Green LED -JF7 (RE9) (0x200) TRISECLR = 0x200; ODCECLR = 0x200; LATESET = 0x200; //Setupt Input for DataFlag Button - JF10 - RA5 0x20 TRISASET = 0x20; //Setup Output for Clutch Hold (Launch) JE1 RD14 0x4000 //This function is active low, driving the FET on the PDU TRISDCLR = 0x4000; ODCDCLR = 0x4000; LATDSET = 0x4000; //Default state is high (off) CAN1Init();//CAN1 ACCL 500kbs CAN2Init();//Motec 1mbs DelayInit(); initUART2(); // GPS UART prevButton1 = 0; prevButton2 = 0; millisec = 0; // Configure Timer 2 to request a real-time interrupt once per millisecond. // The period of Timer 2 is (16 * 5000)/(80 MHz) = 1 ms. OpenTimer2(T2_ON | T2_IDLE_CON | T2_SOURCE_INT | T2_PS_1_16 | T2_GATE_OFF, 5000); // Configure the CPU to respond to Timer 2's interrupt requests. INTEnableSystemMultiVectoredInt(); INTSetVectorPriority(INT_TIMER_2_VECTOR, INT_PRIORITY_LEVEL_2); INTClearFlag(INT_T2); INTEnable(INT_T2, INT_ENABLED); //UART GPS Interrupts INTSetVectorPriority(INT_UART_2_VECTOR ,INT_PRIORITY_LEVEL_1); //Make sure UART interrupt is top priority INTClearFlag(INT_U2RX); INTEnable(INT_U2RX, INT_ENABLED); value = OSCCON; while (!(value & 0x00000020)) { value = OSCCON; // Wait for PLL lock to stabilize } deviceAttached = FALSE; //Initialize the stack USBInitialize(0); shouldLog = FALSE; shouldStop = FALSE; //count = 0; angularRateInfoRec = FALSE; accelerationSensorRec = FALSE; HRaccelerationSensorRec = FALSE; //init tim er 3 to convert adc at 100hz OpenTimer3(T3_ON|T3_PS_1_256|T3_SOURCE_INT, 1562); //initialize i2c for the psoc initI2CPSoC(); state = wait; logNum = 0; initI2CEEPROM(); short addy = 0x0000; BYTE num = 0x00; logNum = readEEPROM(addy); if(logNum >= 0xEF) //Address stored in EEPROM if greater than 0xEF reset to zero, limited to a single byte with current code configuration { writeEEPROM(addy, 0x00); } char GroupString[550];//Group Names (Line1) char UnitString[550];//Units (line2) char ParamString[650];//Paramater Names (line3) sprintf(GroupString,"Time,Accelerometer,Accelerometer,Accelerometer,Accelerometer,Accelerometer,Accelerometer,Accelerometer,Accelerometer,Accelerometer,Engine,Engine,Engine,Engine,Engine,Engine,Engine,Engine,Engine,Engine,Drivetrain,Drivetrain,Electrical,Drivetrain,Drivetrain,Drivetrain,Drivetrain,Engine,Engine,Engine,Engine,Electrical,Electrical,Electrical,Electrical,Electrical,Electrical,Suspension,Suspension,Suspension,Suspension,Suspension,Drivetrain,Driver\n"); sprintf(UnitString,"ms,deg/s,deg/s,deg/s,m/s^2,m/s^2,m/s^2,m/s^2,m/s^2,m/s^2,rpm,%,kpa,degF,degF,lambda,psi,degF,na,na,psi,psi,V,mph,mph,mph,mph,s,gal,degF,degBTDC,mV,mV,mV,mV,mV,mV,mV,mV,mV,mV,mV,mV,\n"); sprintf(ParamString, "Millisec,pitch(deg/sec),roll(deg/sec),yaw(deg/sec),lat(m/s^2),long(m/s^2),vert(m/s^2),latHR(m/s^2),longHR(m/s^2),vertHR(m/s^2),rpm,tps(percent),MAP(kpa),AT(degF),ect(degF),lambda,fuel pres,egt(degF),launch,neutral,brake pres,brake pres filtered,BattVolt(V),ld speed(mph), lg speed(mph),rd speed(mph),rg speed(mph),run time(s),fuel used,Oil Temp (deg F), Ignition Adv (degBTDC),Overall Consumption(mV),Overall Production(mV),Fuel Pump(mV),Fuel Injector(mV),Ignition(mV),Vref(mV),Back Left(mV),Back Right(mV),Front Left(mV),Front Right(mV),Steering Angle(mV),Brake Temp(mV),Data Flag,GPRMC,Time,Valid,Lat,N/S,Long,E/W,Speed,Course,Date,Variation,E/W\n"); LATACLR = 0x10; //Turn on Red LED // LATECLR = 0x200; UARTSendString(UART2,PMTK_HOT_RESTART); int i = 0; while(!UARTTransmissionHasCompleted(UART2)){ i++; } while(1) { GPSDataRead(); GPSSentenceParse(); ClutchHold(); //This function handles the venting direction of the clutch actuator DataFlagFunc(); //This function handles the updates of the data flag variable //USB stack process function USBTasks(); switch(state){ case wait: USBTasks(); millisec = 0; if(CheckLogStateChange() == 1){ //start the transition from wait to log state = startLog; } break; case startLog: //if thumbdrive is plugged in if(USBHostMSDSCSIMediaDetect()) { deviceAttached = TRUE; //now a device is attached //See if the device is attached and in the right format if(FSInit()) { //Opening a file in mode "w" will create the file if it doesn't // exist. If the file does exist it will delete the old file // and create a new one that is blank. logNum = readEEPROM(addy); sprintf(nameString, "test%d.csv", logNum); myFile = FSfopen(nameString,"w"); FSfwrite(GroupString,1,strlen(GroupString),myFile); FSfwrite(UnitString,1,strlen(UnitString),myFile); FSfwrite(ParamString,1, strlen(ParamString),myFile); millisec = 0; //LATDSET = 0x4000; //Send sync pulse (aeroprobe) // while(millisec < 1000){} //Wait 1s then move to log, the aeroprobe ADC waits 1s. state = log; LATECLR = 0x200; //Turn on Green LATASET = 0x10; //Turn off Red } } break; case log: //This uses MOTEC as the master timer. Data is only written to the USB after all the motec Data is received if(motec0Read && motec1Read && motec2Read && motec3Read && motec4Read && motec5Read){ WriteToUSB(); } else{}//Wait for motec data to write the next row if(CheckLogStateChange() == 2){ //Start the transition from log to wait state = stopLog; } if(millisec > 2000){ LATDCLR = 0x4000; //After 2 seconds pass no need to keep output high } //Add a function to check for a flag button and set a variable break; case stopLog: //Always make sure to close the file so that the data gets written to the drive. FSfwrite("endFile", 1, 7, myFile); FSfclose(myFile); state = wait; logNum++; writeEEPROM(addy, logNum); LATACLR = 0x10; //Turn on Red LATESET = 0x200; //Turn off Green break; default: state = wait; break; } //CAN Handlers CANRxMessageBuffer* CAN1RxMessage = CAN1RxMsgProcess(); if(CAN1RxMessage){ WriteAccelData(CAN1RxMessage); //Accel is on CAN 1 } CANRxMessageBuffer* CAN2RxMessage = CAN2RxMsgProcess(); if(CAN2RxMessage){ writeCan2Msg(CAN2RxMessage); //Motec is on CAN 2 } } return 0; }