예제 #1
0
int main() {
	// Create IP connection
	IPConnection ipcon;
	ipcon_create(&ipcon);

	// Create device object
	Stepper stepper;
	stepper_create(&stepper, UID, &ipcon); 

	// Connect to brickd
	if(ipcon_connect(&ipcon, HOST, PORT) < 0) {
		fprintf(stderr, "Could not connect\n");
		exit(1);
	}
	// Don't use device before ipcon is connected

	stepper_set_motor_current(&stepper, 800); // 800mA
	stepper_set_step_mode(&stepper, 8); // 1/8 step mode
	stepper_set_max_velocity(&stepper, 2000); // Velocity 2000 steps/s

	// Slow acceleration (500 steps/s^2), 
	// Fast deacceleration (5000 steps/s^2)
	stepper_set_speed_ramping(&stepper, 500, 5000);

	stepper_enable(&stepper);
	stepper_set_steps(&stepper, 60000); // Drive 60000 steps forward

	printf("Press key to exit\n");
	getchar();
	ipcon_destroy(&ipcon); // Calls ipcon_disconnect internally
}
예제 #2
0
void set_step_mode(uint8_t com, const SetStepMode *data) {
	stepper_set_step_mode(data->mode);
}
예제 #3
0
void stepper_init(void) {
	Pin pins_stepper[] = {PINS_STEPPER};
	PIO_Configure(pins_stepper, PIO_LISTSIZE(pins_stepper));

	Pin stepper_power_management_pins[] = {VOLTAGE_STACK_PIN,
	                                       VOLTAGE_EXTERN_PIN,
	                                       VOLTAGE_STACK_SWITCH_PIN,
	                                       STEPPER_CURRENT_PIN};
	PIO_Configure(stepper_power_management_pins,
	              PIO_LISTSIZE(stepper_power_management_pins));

	// Initialize and enable DACC to set VREF and DECAY pins
    DACC_Initialize(DACC,
                    ID_DACC,
                    0, // Hardware triggers are disabled
                    0, // External trigger
                    0, // Half-Word Transfer
                    0, // Normal Mode (not sleep mode)
                    BOARD_MCK,
                    1, // refresh period
                    0, // Channel 0 selection
                    1, // Tag Selection Mode enabled
                    16); //  value of the start up time
    DACC_EnableChannel(DACC, VREF_CHANNEL);
    DACC_EnableChannel(DACC, DECAY_CHANNEL);


    // Enable peripheral clock for TC
    PMC->PMC_PCER0 = 1 << ID_TC0;

    // Configure and enable TC interrupts
	NVIC_DisableIRQ(TC0_IRQn);
	NVIC_ClearPendingIRQ(TC0_IRQn);
	NVIC_SetPriority(TC0_IRQn, PRIORITY_STEPPER_TC0);
	NVIC_EnableIRQ(TC0_IRQn);

	tc_channel_init(&STEPPER_TC_CHANNEL,
	                TC_CMR_TCCLKS_TIMER_CLOCK5 | TC_CMR_CPCTRG);

    // Interrupt in compare
    tc_channel_interrupt_set(&STEPPER_TC_CHANNEL, TC_IER_CPCS);

    PMC->PMC_PCER0 = 1 << ID_TC1;
	tc_channel_init(&SINGLE_SHOT_TC_CHANNEL,
	                TC_CMR_WAVE |
	                TC_CMR_TCCLKS_TIMER_CLOCK4 |
	                TC_CMR_EEVT_XC0 |
	                TC_CMR_ASWTRG_SET |
	                TC_CMR_ACPC_CLEAR |
	                TC_CMR_WAVSEL_UP |
	                TC_CMR_CPCDIS |
	                TC_CMR_CPCSTOP);

	SINGLE_SHOT_COUNTER = 1;
	tc_channel_start(&SINGLE_SHOT_TC_CHANNEL);

    stepper_set_output_current(VREF_DEFAULT_CURRENT);
    stepper_set_step_mode(STEP_MODE_EIGTH);
    stepper_set_decay(DECAY_DEFAULT_VALUE);

	adc_channel_enable(VOLTAGE_EXTERN_CHANNEL);
	adc_channel_enable(VOLTAGE_STACK_CHANNEL);
	adc_channel_enable(STEPPER_CURRENT_CHANNEL);
}