int main(void) { can_message_ msg; GPIO_init(); CAN_init(125000, 0, 0, 8, &CAN_Message_Received); USB_Init(); USB_Connect(TRUE); A3984_init(); A3984_set_state(A3984_STATE_WAKEUP); A3984_set_state(A3984_STATE_ACTIVE); A3984_set_stepping(A3984_STEP_FULL); A3984_set_direction(A3984_DIR_CW); A3984_set_idle_current(100); GPIO_clock_out(TRUE); CAN_Message_(&msg, 0x0123, 0, 5, 'R', 'E', 'A', 'D', 'Y'); CAN_send(1, msg); while (1) { #if A3984_CURRENT_ISR == 1 A3984_set_current_handler(); #endif } return 0 ; }
/** * @brief Put the controller in normal mode and make it send the same message * over and over again */ void CAN_test_normal_send() { CAN_init(MCP_MODE_NORMAL); CanMessage_t message; message.id = 15; message.length = 4; message.data[0] = 'T'; message.data[1] = 'E'; message.data[2] = 'S'; message.data[3] = 'T'; while (1) { CAN_send(&message); printf("Message sent!\n"); //_delay_ms(200); } }
/** * @brief Put the CAN controller in loopback mode and test local features */ void CAN_test_loopback() { CAN_init(MCP_MODE_LOOPBACK); CanMessage_t message; CanMessage_t resp; message.id = 15; message.length = 4; message.data[0] = 'T'; message.data[1] = 'E'; message.data[2] = 'S'; message.data[3] = 'T'; while (1) { CAN_send(&message); _delay_ms(200); resp = CAN_receive(); CAN_print_message(&resp); } }
int main(void){ //Initialization of UART module UART_init(); //Enable interrupts DDRE &= ~(1 << PE4); EIMSK |= (1 << INT4); EICRB |= (1 << ISC41); sei(); //Initialization of CAN module SPI_MasterInit(); CANInit_normal(); //Initialization of PWM module PWM_init(); //Initialization of ADC module ADC_init(); //Initialization of Solenoid module solenoid_init(); //Initialization of Motor module motor_init(); //Variables uint8_t ready = 0; uint8_t game_over = 0; uint8_t some_counter = 0; // Used for delay on IR, so that not a single low read causes game to end uint8_t some_other_counter = 0; //Solenoid should not spam. It kills everything. Mad solenoid //This is where new received joystick values are stored joystick_position joy_pos; //Message is passed back to node 1 upon game over message.length = 1; message.ID = 0x01; while(1){ /* PHASE 0: INITIALIZATION */ ready = 0; game_over = 0; joy_pos.x = 128; joy_pos.y = 128; joy_pos.button_pressed = 0; //Set to some start value PWM_set_value(joy_pos); motor_send(joy_pos); /* PHASE 1: PRE-GAME */ /* Wait for node 1 to make contact */ printf("Waiting for node 1 to initiate game\n"); while(!ready){ cli(); if(CAN_received){ received = CAN_read(); CAN_received = 0; if(received.data[3] == 1){ //If this is set, node 1 wants to start a game received.data[3] = 0; ready = 1; } } sei(); _delay_ms(5); } printf("Node 1 is ready to go!\n"); printf("Starting game\n"); /* PHASE 2: IN-GAME */ /* Follow input over CAN and return a message when we have a game over. It is not a matter of if, only when! */ while(!game_over){ cli(); //Interrupts are disabled while we read from the CAN bus. Bad values were sometimes introduced otherwise if(CAN_received){ //If updated controller info is ready, read it into our struct received = CAN_read(); CAN_received = 0; joy_pos.y = received.data[0]; //first spot contains y value joy_pos.x = received.data[1]; //second spot contains x value joy_pos.button_pressed = received.data[2]; //third spot contains button value } if(joy_pos.y > 128 - margin && joy_pos.y < 128 + margin){ joy_pos.y = 128; //This is done to avoid noisy PWM input } /* Apply new numbers to the system */ //Use information available to control PWM, motor and solenoid some_other_counter++; /********************************************************/ /* Solenoid pulse function is omitted to avoid spamming. Spamming is more likely to introduce errors */ if(joy_pos.button_pressed){ solenoid_push(); some_other_counter = 0; //solenoid_pulse(); //Apply solenoid pulse. Good luck joy_pos.button_pressed = 0; } if(some_other_counter > 10){ solenoid_pull(); } /**********************************************************/ PWM_set_value(joy_pos); //update servo motor_send(joy_pos); //update motor if(ADC_check_goal()){ //Evaluates to 1 if goal is detected some_counter++; if(some_counter == 20){ some_counter = 0; game_over = 1; //Start procedure all over CAN_reset(); //Tell node 1 about the failure message.data[0] = 1; CAN_send(message); } } sei(); //Allow interrupts to occur _delay_ms(20); } /* GAME ENDED. WAIT FOR NEW ONE TO START */ } }