Example #1
0
//Main hall effect sensor setup, called from main()
void hallSetup() {
    //Init of PID controller objects
    int i;
    for (i = 0; i < NUM_HALL_PIDS; i++) {
        hallInitPIDObjPos(&(hallPIDObjs[i]), DEFAULT_HALL_KP, DEFAULT_HALL_KI,
                DEFAULT_HALL_KD, DEFAULT_HALL_KAW, DEFAULT_HALL_FF);
        hallPIDObjs[i].minVal = 0;
        hallPIDObjs[i].satValNeg = 0;
        hallPIDObjs[i].maxVal = FULLTHROT;
        hallPIDObjs[i].satValPos = SATTHROT;
    }

    // Controller to PWM channel correspondance
    hallOutputChannels[0] = MC_CHANNEL_PWM1;
    hallOutputChannels[1] = MC_CHANNEL_PWM2;

    //Init for velocity profile objects
    hallInitPIDVelProfile();

    //System setup
    SetupTimer1();
    SetupTimer2(); // used for leg hall effect sensors
    SetupInputCapture(); // setup input capture for hall effect sensors
    int retval;
    retval = sysServiceInstallT1(hallServiceRoutine);

    // returns pointer to queue with 8 move entries
    hallMoveq = mqInit(8);
    hallIdleMove = malloc(sizeof (moveCmdStruct));
    hallIdleMove->inputL = 0;
    hallIdleMove->inputR = 0;
    hallIdleMove->duration = 0;
    hallCurrentMove = hallIdleMove;

    hallManualMove = malloc(sizeof (moveCmdStruct));
    hallManualMove->inputL = 0;
    hallManualMove->inputR = 0;
    hallManualMove->duration = 0;

    lastMoveTime = 0;
    //  initialize PID structures before starting Timer1
    hallPIDSetInput(0, 0, 0);
    hallPIDSetInput(1, 0, 0);

    for (i = 0; i < NUM_HALL_PIDS; i++) {
        hallbemfLast[i] = 0;
        hallbemfHist[i][0] = 0;
        hallbemfHist[i][1] = 0;
        hallbemfHist[i][2] = 0;
    }
}
int main ( void )
{
    fun_queue = queueInit(FUN_Q_LEN);
    rx_pay_queue = pqInit(12); //replace 12 with a #define const later
    test_function tf;

    /* Initialization */
    SetupClock();
    SwitchClocks();
    SetupPorts();

    SetupInterrupts();
    SetupI2C();
    SetupADC();
    SetupTimer1();
    SetupPWM();
    SetupTimer2();
    gyroSetup();
    xlSetup();
    dfmemSetup();

    WordVal pan_id    = {RADIO_PAN_ID};
    WordVal src_addr  = {RADIO_SRC_ADDR};
    WordVal dest_addr = {RADIO_DEST_ADDR};

    radioInit(src_addr, pan_id, RADIO_RXPQ_MAX_SIZE, RADIO_TXPQ_MAX_SIZE);
    radioSetDestAddr(dest_addr);
    radioSetChannel(RADIO_MY_CHAN);

    char j;
    for(j=0; j<3; j++){
        LED_2 = ON;
        delay_ms(500);
        LED_2 = OFF;
        delay_ms(500);
    }

    LED_2 = ON;

    EnableIntT2;
    while(1){
        while(!queueIsEmpty(fun_queue))
        {
            rx_payload = pqPop(rx_pay_queue);
            tf = (test_function)queuePop(fun_queue);
            (*tf)(payGetType(rx_payload), payGetStatus(rx_payload), payGetDataLength(rx_payload), payGetData(rx_payload));
            payDelete(rx_payload);
        }
    }
    return 0;
}
Example #3
0
//--------------------------------------------------------------------------------
//
//  Main program loop.
//
void main()
{
    __disable_interrupt();
    _pulseDataAddress = (char *) EEPROM_PULSE_DATA;
    _numberOfPulses = *_pulseDataAddress++;
    SetupPorts();
    SetupUART();
    SetupTimer2();
    SetupTimer1();
    __enable_interrupt();
    while (1)
    {
        __wait_for_interrupt();
    }
}
Example #4
0
int main() {

	// LEDs as outputs
	DDRC |= (1 << IR_LED_PIN);
	DDRC |= (1 << IR_INDICATOR_LED_PIN);
	DDRC |= (1 << TIMER_INDICATOR_LED_PIN);

	//enable internal pullup resistors
	PORTC |= (1 << MODE_SWITCH_PIN);
	PORTC |= (1 << POTENTIOMETER_PIN);
	PORTD |= (1 << TRIGGER_PIN);
	
	// Pin change interrupt control register - enables interrupt vectors
	// Bit 2 = enable PC vector 2 (PCINT23..16)
	// Bit 1 = enable PC vector 1 (PCINT14..8)
	// Bit 0 = enable PC vector 0 (PCINT7..0)
	PCICR |= (1 << PCIE2);

	// Pin change mask registers decide which pins are enabled as triggers
	PCMSK2 |= (1 << PCINT18);

	adc_init();

	//start up the serial port
	uart_init();
	FILE uart_stream = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
	stdin = stdout = &uart_stream;

	printf_P(PSTR("STARTING\r\n"));
	
	//Check mode switch
	if ((PINC & (1 << MODE_SWITCH_PIN)) == 0) {
		mode = REMOTE;
		blink_timer_led_times(1U);
	} else {
		mode = INTERVALOMETER;
		blink_timer_led_times(2U);
		SetupTimer2();
		timer_interval_ms = get_interval_ms();
	}
	delay_ms(500U);
	
	printf_P(PSTR("Mode %u ms\r\n"), mode);
	
	while (1) {
		if (mode == INTERVALOMETER) {
		
			set_sleep_mode(SLEEP_MODE_PWR_SAVE);		
			cli();
			sleep_enable();
			sei();
			sleep_cpu();
			sleep_disable();
			sei();

			if (the_time_ms >= timer_interval_ms) {
				the_time_ms = 0UL;
				remainder_counter = 0UL;
				click();
				count_down_ms = 5000UL;
				printf_P(PSTR("Waiting for: %lu ms\r\n"), timer_interval_ms);
			} else {		
				//while waiting for next intervalometer click, flash a green LED to show it is still on
				if (the_time_ms > 0UL && (count_down_ms <= 0L)) {		//turn on every 5 seconds
					count_down_ms = 5000UL;
					blink_timer_led_times(1U);
				}
			}
		}
		else {
			set_sleep_mode(SLEEP_MODE_PWR_DOWN);
			cli();
			sleep_enable();
			sei();
			sleep_cpu();
			sleep_disable();
			sei();
		}
	}
	
	return 0;
}