Пример #1
0
int main(void) {
    NU32_Startup();

    //NU32_WriteUART1("HelloWorld\n");
    char buf[MESSAGE_MAX];
    int c='0';
    char state='x';
    int n=0;

    //allocate struct array of max size
    //struct args control_args = (struct args)malloc(N_MAX * sizeof(struct args));
    struct args control_args[N_MAX];

    
    while (1) {
    	NU32_WriteUART1("Press w to write, r to read, and x to swing\r\n");
        NU32_ReadUART1(buf,MESSAGE_MAX);

        c = buf[0];
        
        if(c=='w'){
            NU32_WriteUART1("Input n: \r\n");
            NU32_ReadUART1(buf, MESSAGE_MAX);
            sscanf(buf,"%d", &n);
            write_swing(n, control_args);
        }
        else if(c=='r'){
            read_swing(n,control_args);
        }
        else if(c=='x'){
            //swing robot
        }
    }
    //free(control_args);
}
Пример #2
0
void write_swing(int n, struct args* control_args){
    //Note: this function will be condensed after testing
    //get input from user and send to the PIC
    int torque, t_swing, t_flight;

    char torque_buf[10], swing_buf[10], fly_buf[10];
    char message[MESSAGE_MAX];
    //struct args control_args[n];

    int i;
    for (i=0; i<n; i++){
        sprintf(message, "Enter data for set %d\r\n", i+1);
        NU32_WriteUART1(message);

        //Torque
        NU32_WriteUART1("Torque: ");
        NU32_ReadUART1(torque_buf, 10);
        sscanf(torque_buf, "%d", &torque);
        sprintf(message, "%d \r\n", torque);
        NU32_WriteUART1(message);
        control_args[i].torque = torque;   

        //Time swinging
        NU32_WriteUART1("Time to swing: ");
        NU32_ReadUART1(swing_buf, 10);
        sscanf(swing_buf, "%d", &t_swing);
        sprintf(message, "%d \r\n", t_swing);
        NU32_WriteUART1(message);
        control_args[i].t_swing = t_swing;
        
        //Time in flight
        NU32_WriteUART1("Time to fly: ");
        NU32_ReadUART1(fly_buf,10);
        sscanf(fly_buf, "%d", &t_flight);
        sprintf(message, "%d \r\n\n", t_flight);
        NU32_WriteUART1(message);
        control_args[i].t_flight = t_flight;
    }
    return 0;
}
int main(void) {

	// Cache on, min flash wait, interrupts on, LED/button init, UART init
  NU32_Startup();

  // Setup LCD and create message to be sent
  LCD_Setup();
  char message[MAX_MESSAGE_LENGTH];

  // Local variables for determining and setting Timer interrupts based on FPS
	int Time1_Side = 0, N_Side = 0, PR_Side = 0, TCKPS_Side = 0;
	int Time1_Top = 0, N_Top = 0, PR_Top = 0, TCKPS_Top = 0;


	// =============================================================================================
	// Change Notification Digital Input Interrupt from PPOD
	// =============================================================================================
  CNPUEbits.CNPUE17 = 0;  						// CN17/RF4 input has no internal pull-up

  oldF = PORTF;           						// all pins of port F are inputs, by default

  __builtin_disable_interrupts(); 		// step 1: disable interrupts
  CNCONbits.ON = 1;               		// step 2: configure peripheral: turn on CN
  CNENbits.CNEN17 = 1; 								//         listen to CN17/RF4
  IPC6bits.CNIP = 3;              		// step 3: set interrupt priority
  IPC6bits.CNIS = 2;              		// step 4: set interrupt subpriority
  IFS1bits.CNIF = 0;              		// step 5: clear the interrupt flag
  IEC1bits.CNIE = 1;              		// step 6: enable the _CN interrupt
  __builtin_enable_interrupts();  		// step 7: CPU enabled for mvec interrupts



	// =============================================================================================
	// PWM and digital output for piezoelectric droplet generator
	// =============================================================================================
  OC1CONbits.OCTSEL = 1;  			 			// Select Timer3 for comparison

  T3CONbits.TCKPS = 0;     						// Timer3 prescaler N=1 (1:1)
  PR3 = 3999;              						// period = (PR3+1) * N * 12.5 ns = 20 kHz
  TMR3 = 0;                						// initial TMR3 count is 0

  OC1CONbits.OCM = 0b110; 						// PWM mode with no fault pin; other OC1CON bits are defaults
  OC1RS = 0;           								// duty cycle = OC1RS/(PR3+1) = 75%
  OC1R = 0;             							// initialize before turning OC1 on; afterward it is read-only

  T3CONbits.ON = 1;        						// turn on Timer3
  OC1CONbits.ON = 1;       						// turn on OC1

	// Set A10/A2 to digital output pins
	TRISAbits.TRISA10 = 0;							// RA10 is an output pin
	TRISAbits.TRISA2 = 0;								// RA2 is an output pin

	//-Vcc: set L1 to HIGH, L2 to LOW, PWMA to >0
	OC1RS = 4000;
	LATAbits.LATA10 = 1;
	LATAbits.LATA2 = 0;



	// =============================================================================================
	// TrackCam ExSync Trigger - Side (A3), Top (A4)
	// =============================================================================================

	// Digital output pin
	TRISAbits.TRISA3 = 0;
	TRISAbits.TRISA4 = 0;

	// Set to HIGH
	LATAbits.LATA3 = 1;
	LATAbits.LATA4 = 1;



	// =============================================================================================
	// Keep program running to look for command from master to start record procedure
	// =============================================================================================
  while(1) {

		// Get message from computer
    NU32_ReadUART1(message, MAX_MESSAGE_LENGTH);

		//Serial message: [NUMIMAGES_Side, FPS_Side, NUMIMAGES_Top, FPS_Top, PULSETIME, DELAYTIME]
		sscanf(message, "%d%*c %d%*c %d%*c %d%*c %d%*c %f", &NUMIMAGES_Side, &FPS_Side, &NUMIMAGES_Top, &FPS_Top, &PULSETIME, &DELAYTIME);		//%*c reads in comma and ignores it


		// -------------------------------------------------------------------------------------------
		// Side Camera Interrupt -  NUMIMAGES_Side Hz - Timer2
		// -------------------------------------------------------------------------------------------

		// Calculate pre-scaler based on FPS_Side
		Time1_Side = (((1.0/FPS_Side*1.0)*1e9)/12.5);
		N_Side = ceil( (Time1_Side*1.0/65535.0) );

		// Select best pre-scaler of 1, 2, 4, 8, 16, 32, 64, or 256 
		if( N_Side == 1 ) {
			N_Side = 1;
			TCKPS_Side = 0;
		}
		else if( N_Side <= 2 ) {
			N_Side = 2;
			TCKPS_Side = 1;
		}
		else if( N_Side <= 4 ) {
			N_Side = 4;
			TCKPS_Side = 2;
		}
		else if( N_Side <= 8 ) {
			N_Side = 8;
			TCKPS_Side = 3;
		}
		else if( N_Side <= 16 ) {
			N_Side = 16;
			TCKPS_Side = 4;
		}
		else if( N_Side <= 32 ) {
			N_Side = 32;
			TCKPS_Side = 5;
		}
		else if( N_Side <= 64 ) {
			N_Side = 64;
			TCKPS_Side = 6;
		}
		else {
			N_Side = 256;
			TCKPS_Side = 7;
		}

		// Calculate period register based on selected prescaler
		PR_Side = (Time1_Side / N_Side) - 1;



		__builtin_disable_interrupts(); 	// INT step 2: disable interrupts at CPU

																			// INT step 3: 	setup TMR2 to call ISR at frequency of 5 kHz
		PR2 = PR_Side;										// 							set period register to 16,000
		TMR2 = 0;													// 							initialize count to 0
		T2CONbits.TCKPS = TCKPS_Side;			// 							set prescaler to 1:1
		T2CONbits.ON = 1; 								// 							turn on Timer2
		IPC2bits.T2IP = 5; 								// INT step 4: 	priority 5
		IPC2bits.T2IS = 2; 								// 							subpriority 2
		IFS0bits.T2IF = 0; 								// INT step 5: 	clear interrupt flag
		IEC0bits.T2IE = 1; 								// INT step 6: 	enable interrupt

		__builtin_enable_interrupts(); 		// INT step 7: 	enable interrupts at CPU



		// -------------------------------------------------------------------------------------------
		// Top Camera Interrupt -  NUMIMAGES_Top Hz - Timer4
		// -------------------------------------------------------------------------------------------

		// Calculate pre-scaler based on FPS_Top
		Time1_Top = (((1.0/FPS_Top)*1e9)/12.5);
		N_Top = ceil( (Time1_Top/65535.0) );

		// Select best pre-scaler of 1, 2, 4, 8, 16, 32, 64, or 256 
		if( N_Top == 1 ) {
			N_Top = 1;
			TCKPS_Top = 0;
		}
		else if( N_Top <= 2 ) {
			N_Top = 2;
			TCKPS_Top = 1;
		}
		else if( N_Top <= 4 ) {
			N_Top = 4;
			TCKPS_Top = 2;
		}
		else if( N_Top <= 8 ) {
			N_Top = 8;
			TCKPS_Top = 3;
		}
		else if( N_Top <= 16 ) {
			N_Top = 16;
			TCKPS_Top = 4;
		}
		else if( N_Top <= 32 ) {
			N_Top = 32;
			TCKPS_Top = 5;
		}
		else if( N_Top <= 64 ) {
			N_Top = 64;
			TCKPS_Top = 6;
		}
		else {
			N_Top = 256;
			TCKPS_Top = 7;
		}

		// Calculate period register based on selected prescaler
		PR_Top = (Time1_Top / N_Top) - 1;



		__builtin_disable_interrupts(); 	// INT step 2: disable interrupts at CPU

																			// INT step 3: 	setup TMR2 to call ISR at frequency of 5 kHz
		PR4 = PR_Top; 										// 							set period register to 16,000
		TMR4 = 0;													// 							initialize count to 0
		T4CONbits.TCKPS = TCKPS_Top; 			// 							set prescaler to 1:1
		T4CONbits.ON = 1; 								// 							turn on Timer2
		IPC4bits.T4IP = 5; 								// INT step 4: 	priority 5
		IPC4bits.T4IS = 0; 								// 							subpriority 0
		IFS0bits.T4IF = 0; 								// INT step 5: 	clear interrupt flag
		IEC0bits.T4IE = 1; 								// INT step 6: 	enable interrupt

		__builtin_enable_interrupts(); 		// INT step 7: 	enable interrupts at CPU




		// Convert DELAYTIME to DelayFrames
		//DelayFrames_Side = DELAYTIME*FPS_Side;

		// Turn RecordFlag ON since we have received from master
		RecordFlag = 1;

		// Display properties on LCD
    LCD_Clear();
    LCD_Move(0,0);
		sprintf(message, "%d, %d, %d", NUMIMAGES_Side, FPS_Side, FPS_Top);
		//sprintf(message, "%d, %d, %d", PR2, N_Side, T2CONbits.TCKPS);
    LCD_WriteString(message);                     	

    LCD_Move(1,0);
		sprintf(message, "%d, %f", PULSETIME, DELAYTIME);
		//sprintf(message, "%d, %d, %d", PR4, N_Top, T4CONbits.TCKPS);
    LCD_WriteString(message);
  }

  return 0;
}
int main(void) {

	// Cache on, min flash wait, interrupts on, LED/button init, UART init
  NU32_Startup();
  LCD_Setup();

  char message[MAX_MESSAGE_LENGTH];


	// =============================================================================================
	// Change Notification Digital Input Interrupt from PPOD
	// =============================================================================================
  CNPUEbits.CNPUE17 = 0;  // CN17/RF4 input has no internal pull-up

  oldF = PORTF;           // all pins of port F are inputs, by default

  __builtin_disable_interrupts(); // step 1: disable interrupts
  CNCONbits.ON = 1;               // step 2: configure peripheral: turn on CN
  CNENbits.CNEN17 = 1; 						//         listen to CN17/RF4
  IPC6bits.CNIP = 3;              // step 3: set interrupt priority
  IPC6bits.CNIS = 2;              // step 4: set interrupt subpriority
  IFS1bits.CNIF = 0;              // step 5: clear the interrupt flag
  IEC1bits.CNIE = 1;              // step 6: enable the _CN interrupt
  __builtin_enable_interrupts();  // step 7: CPU enabled for mvec interrupts



	// =============================================================================================
	// PWM and digital output for piezoelectric droplet generator
	// =============================================================================================
  OC1CONbits.OCTSEL = 1;  			 		// Select Timer3 for comparison

  T3CONbits.TCKPS = 0;     					// Timer3 prescaler N=1 (1:1)
  PR3 = 3999;              					// period = (PR3+1) * N * 12.5 ns = 20 kHz
  TMR3 = 0;                					// initial TMR3 count is 0

  OC1CONbits.OCM = 0b110; 					// PWM mode with no fault pin; other OC1CON bits are defaults
  OC1RS = 0;           							// duty cycle = OC1RS/(PR3+1) = 75%
  OC1R = 0;             						// initialize before turning OC1 on; afterward it is read-only

  T3CONbits.ON = 1;        					// turn on Timer3
  OC1CONbits.ON = 1;       					// turn on OC1

	// Set A10/A2 to digital output pins
	TRISAbits.TRISA10 = 0;						// RA10 is an output pin
	TRISAbits.TRISA2 = 0;							// RA2 is an output pin

	//-Vcc: set L1 to HIGH, L2 to LOW, PWMA to >0
	OC1RS = 4000;
	LATAbits.LATA10 = 1;
	LATAbits.LATA2 = 0;



	// =============================================================================================
	// TrackCam ExSync Trigger
	// =============================================================================================

	// Set A3 to digital output pin
	TRISAbits.TRISA3 = 0;						// RA3 is an output pin

	//Set A3 to HIGH
	LATAbits.LATA3 = 1;



	// =============================================================================================
	// Keep program running to look for command from master to start record procedure
	// =============================================================================================
  while(1) {
		// Get message from computer
    NU32_ReadUART1(message, MAX_MESSAGE_LENGTH);

		//Serial message: [NUMIMAGES, FPS, PULSETIME, DELAYTIME]
		sscanf(message, "%d%*c %d%*c %d%*c %f", &NUMIMAGES, &FPS, &PULSETIME, &DELAYTIME);		//%*c reads in comma and ignores it

		// Convert DELAYTIME to DelayFrames
		DelayFrames = DELAYTIME*FPS;

		// Turn RecordFlag ON since we have received from master
		RecordFlag = 1;

		// Display properties on LCD
    LCD_Clear();
    LCD_Move(0,0);
		sprintf(message, "%d, %d", NUMIMAGES, FPS);
    LCD_WriteString(message);                     	

		sprintf(message, "%d, %f", PULSETIME, DELAYTIME);
    LCD_Move(1,0);
    LCD_WriteString(message);
  }

  return 0;
}