Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
- {
- if (hspi->Instance == SPI1)
- {
- // if (Eight_to_Sixteen(rpi_rx, 0, rpi_rx, 1) != BOARD_ADDR) // Compare RPi received addr to the hard-coded board addr.
- // {
- // wrong_board_addr_flag = TRUE; // If the board addr does not match, then set the appropriate flag and call error handler.
- // Error_Handler();
- // }
- /*
- * Read the motor ON/OFF bits
- */
- if (((rpi_rx[2] >> 7) & 1U) == 1) M1.pi_state = 1;
- else if (((rpi_rx[2] >> 7) & 1U) == 0) M1.pi_state = 0;
- if (((rpi_rx[2] >> 6) & 1U) == 1) M2.pi_state = 1;
- else if (((rpi_rx[2] >> 6) & 1U) == 0) M2.pi_state = 0;
- if (((rpi_rx[2] >> 5) & 1U) == 1) M3.pi_state = 1;
- else if (((rpi_rx[2] >> 5) & 1U) == 0) M3.pi_state = 0;
- //PWM signals are shared between M4 and M5
- if (((rpi_rx[2] >> 4) & 1U) == 1) M4.pi_state = 1;
- else if (((rpi_rx[2] >> 4) & 1U) == 0) M4.pi_state = 0;
- //PWM signals are shared between M4 and M5
- if (((rpi_rx[2] >> 4) & 1U) == 1) M5.pi_state = 1;
- else if (((rpi_rx[2] >> 4) & 1U) == 0) M5.pi_state = 0;
- if (((rpi_rx[2] >> 3) & 1U) == 1) A6.pi_state = 1;
- else if (((rpi_rx[2] >> 3) & 1U) == 0) A6.pi_state = 0;
- if (((rpi_rx[2] >> 2) & 1U) == 1) A7.pi_state = 1;
- else if (((rpi_rx[2] >> 2) & 1U) == 0) A7.pi_state = 0;
- if (((rpi_rx[2] >> 1) & 1U) == 1) STPR.pi_state = 1;
- else if (((rpi_rx[2] >> 1) & 1U) == 0) STPR.pi_state = 0;
- /*
- M1.pi_state = ((rpi_rx[2] >> 7 & 1U) == 1) ? 1 : 0;
- M2.pi_state = ((rpi_rx[2] >> 6 & 1U) == 1) ? 1 : 0;
- M3.pi_state = ((rpi_rx[2] >> 5 & 1U) == 1) ? 1 : 0;
- M4.pi_state = ((rpi_rx[2] >> 4 & 1U) == 1) ? 1 : 0;
- M5.pi_state = ((rpi_rx[2] >> 4 & 1U) == 1) ? 1 : 0;
- A6.pi_state = ((rpi_rx[2] >> 3 & 1U) == 1) ? 1 : 0;
- A7.pi_state = ((rpi_rx[2] >> 2 & 1U) == 1) ? 1 : 0;
- STPR.pi_state = ((rpi_rx[2] >> 1 & 1U) == 1) ? 1 : 0;
- */
- /*
- * Read Motor direction states
- */
- if (((rpi_rx[3] >> 7) & 1U) == 1) M1.dir = 1;
- else if (((rpi_rx[3] >> 7) & 1U) == 0) M1.dir = 0;
- if (((rpi_rx[3] >> 6) & 1U) == 1) M2.dir = 1;
- else if (((rpi_rx[3] >> 6) & 1U) == 0) M2.dir = 0;
- if (((rpi_rx[3] >> 5) & 1U) == 1) M3.dir = 1;
- else if (((rpi_rx[3] >> 5) & 1U) == 0) M3.dir = 0;
- //M5 is a mirror of M4. Direction is inverted in hardware
- if (((rpi_rx[3] >> 4) & 1U) == 1) M4.dir = 1;
- else if (((rpi_rx[3] >> 4) & 1U) == 0) M4.dir = 0;
- //M5 is a mirror of M4. Direction is inverted in hardware
- if (((rpi_rx[3] >> 4) & 1U) == 1) M5.dir = 1;
- else if (((rpi_rx[3] >> 4) & 1U) == 0) M5.dir = 0;
- if (((rpi_rx[3] >> 3) & 1U) == 1) A6.dir = 1;
- else if (((rpi_rx[3] >> 3) & 1U) == 0) A6.dir = 0;
- if (((rpi_rx[3] >> 2) & 1U) == 1) A7.dir = 1;
- else if (((rpi_rx[3] >> 2) & 1U) == 0) A7.dir = 0;
- if (((rpi_rx[3] >> 1) & 1U) == 1) STPR.dir = 1;
- else if (((rpi_rx[3] >> 1) & 1U) == 0) STPR.dir = 0;
- /*
- M1.dir = ((rpi_rx[3] >> 7 & 1U) == 1) ? CCW : CW;
- M2.dir = ((rpi_rx[3] >> 6 & 1U) == 1) ? CCW : CW;
- M3.dir = ((rpi_rx[3] >> 5 & 1U) == 1) ? CCW : CW;
- M4.dir = ((rpi_rx[3] >> 4 & 1U) == 1) ? CCW : CW;
- M5.dir = ((rpi_rx[3] >> 4 & 1U) == 1) ? CCW : CW;
- // M5 direction bit is inverted in hardware.
- // Hence it will be the same as M4 in software.
- A6.dir = ((rpi_rx[3] >> 3 & 1U) == 1) ? CCW : CW;
- A7.dir = ((rpi_rx[3] >> 2 & 1U) == 1) ? CCW : CW;
- STPR.dir = ((rpi_rx[3] >> 1 & 1U) == 1) ? CCW : CW;
- */
- // Check if the Pi is supposed to set the encoder values
- if (rpi_rx[4] == 1) set_rpi_encoder_values = 1;
- else if (rpi_rx[4] == 0) set_rpi_encoder_values = 0;
- // Check if the Pi is supposed to set the feed values
- if (rpi_rx[21] == 1) set_rpi_feed_values = 1;
- else if (rpi_rx[21] == 0) set_rpi_feed_values = 0;
- // Check if current limit values are to be set via the Pi
- if (rpi_rx[36] == 1) set_rpi_current_limit_values = 1;
- else if (rpi_rx[36] == 0) set_rpi_current_limit_values = 0;
- // Check if remote is made operational
- if (rpi_rx[51] == 1) use_remote = 1;
- else if (rpi_rx[51] == 0) use_remote = 0;
- // Check if value is to be stored in the EEPROM
- if (rpi_rx[52] == 1) store_in_eeprom = 1;
- else if (rpi_rx[52] == 0) store_in_eeprom = 0;
- // Check if homing is enabled
- if (rpi_rx[53] == 1) homing_enabled = 1;
- else if (rpi_rx[53] == 0) homing_enabled = 0;
- if (rpi_rx[54] == 1)
- {
- CW = 1;
- CCW = 0;
- }
- // Get real time encoder counts
- M1_new_count = M1.total_count; //Get_Encoder_Count(&M1, &htim1);
- M2_new_count = M2.total_count; //Get_Encoder_Count(&M2, &htim1);
- M3_new_count = M3.total_count; //Get_Encoder_Count(&M3, &htim1);
- M4_new_count = M4.total_count; //Get_Encoder_Count(&M4, &htim1);
- if (set_rpi_encoder_values) // Check if encoder values are to be set by software
- {
- encoder_values_buffer[0] = Eight_to_Sixteen(rpi_rx, 5, rpi_rx, 6); // M1 software set encoder count
- encoder_values_buffer[1] = Eight_to_Sixteen(rpi_rx, 7, rpi_rx, 8); // M2 software set encoder count
- encoder_values_buffer[2] = Eight_to_Sixteen(rpi_rx, 9, rpi_rx, 10); // M3 software set encoder count
- encoder_values_buffer[3] = Eight_to_Sixteen(rpi_rx, 11, rpi_rx, 12); // M4 software set encoder count
- }
- else // if not set by RPi then use hardware encoder values
- {
- encoder_values_buffer[0] = M1_new_count; // M1 hardware set encoder count
- encoder_values_buffer[1] = M2_new_count; // M2 hardware set encoder count
- encoder_values_buffer[2] = M3_new_count; // M3 hardware set encoder count
- encoder_values_buffer[3] = M4_new_count; // M4 hardware set encoder count
- }
- M1_previous_count = M1_new_count;
- M2_previous_count = M2_new_count;
- M3_previous_count = M3_new_count;
- M4_previous_count = M4_new_count;
- /*
- * Populate the EEPROM buffer with the encoder counts
- */
- Sixteen_to_Eight(M1_new_count, eeprom_buffer, 0, 1);
- Sixteen_to_Eight(M2_new_count, eeprom_buffer, 2, 3);
- Sixteen_to_Eight(M3_new_count, eeprom_buffer, 4, 5);
- Sixteen_to_Eight(M4_new_count, eeprom_buffer, 6, 7);
- /*
- * FEED PARSING SECTION
- */
- if (set_rpi_feed_values) // Check if feed values are to be set by the RPi
- {
- // Adjust duty cycle accordingly
- TIM5->CCR1 = ADC_to_Feed(rpi_rx, 22); // M1 software set feed
- TIM5->CCR2 = ADC_to_Feed(rpi_rx, 23); // M2 software set feed
- TIM5->CCR3 = ADC_to_Feed(rpi_rx, 24); // M3 software set feed
- TIM5->CCR4 = ADC_to_Feed(rpi_rx, 25); // M4/5 software set feed
- TIM9->CCR1 = ADC_to_Feed(rpi_rx, 26); // A6 software set feed
- TIM9->CCR2 = ADC_to_Feed(rpi_rx, 27); // A7 software set feed
- TIM12->PSC = ADC_to_Stepper_Feed(rpi_rx, 28); // Stepper software set feed
- //__HAL_TIM_SET_COMPARE(&htim5, TIM_CHANNEL_1, feed_values_buffer[0]);
- //__HAL_TIM_SET_COMPARE(&htim5, TIM_CHANNEL_2, feed_values_buffer[1]);
- //__HAL_TIM_SET_COMPARE(&htim5, TIM_CHANNEL_3, feed_values_buffer[2]);
- //__HAL_TIM_SET_COMPARE(&htim5, TIM_CHANNEL_4, feed_values_buffer[3]);
- //__HAL_TIM_SET_COMPARE(&htim9, TIM_CHANNEL_1, feed_values_buffer[4]);
- //__HAL_TIM_SET_COMPARE(&htim9, TIM_CHANNEL_2, feed_values_buffer[5]);
- //__HAL_TIM_SET_PRESCALER(&htim12, ADC_to_Stepper_Feed(feed_values_buffer, 6));
- // Store values received from the RPi in the eeprom buffer for future storage
- eeprom_buffer[8] = rpi_rx[22]; // Store M1 software set feed in eeprom buffer
- eeprom_buffer[9] = rpi_rx[23]; // Store M2 software set feed in eeprom buffer
- eeprom_buffer[10] = rpi_rx[24]; // Store M3 software set feed in eeprom buffer
- eeprom_buffer[11] = rpi_rx[25]; // Store M4/5 software set feed in eeprom buffer
- eeprom_buffer[12] = rpi_rx[26]; // Store A6 software set feed in eeprom buffer
- eeprom_buffer[13] = rpi_rx[27]; // Store A7 software set feed in eeprom buffer
- eeprom_buffer[14] = rpi_rx[28]; // Store Stepper software set feed in eeprom buffer
- }
- else // else take the hardware feed values
- {
- TIM5->CCR1 = ADC_to_Feed(adc3, 5); // M1 hardware set feed
- TIM5->CCR2 = ADC_to_Feed(adc3, 6); // M2 hardware set feed
- TIM5->CCR3 = ADC_to_Feed(adc3, 7); // M3 hardware set feed
- TIM5->CCR4 = ADC_to_Feed(adc3, 0); // M4/5 hardware set feed
- TIM9->CCR1 = ADC_to_Feed(adc3, 2); // A6 hardware set feed
- TIM9->CCR2 = ADC_to_Feed(adc3, 5); // A7 hardware set feed
- TIM12->PSC = ADC_to_Stepper_Feed(adc1, 5); // Stepper hardware set feed
- //__HAL_TIM_SET_COMPARE(&htim5, TIM_CHANNEL_1, feed_values_buffer[0]);
- //__HAL_TIM_SET_COMPARE(&htim5, TIM_CHANNEL_2, feed_values_buffer[1]);
- //__HAL_TIM_SET_COMPARE(&htim5, TIM_CHANNEL_3, feed_values_buffer[2]);
- //__HAL_TIM_SET_COMPARE(&htim5, TIM_CHANNEL_4, feed_values_buffer[3]);
- //__HAL_TIM_SET_COMPARE(&htim9, TIM_CHANNEL_1, feed_values_buffer[4]);
- //__HAL_TIM_SET_COMPARE(&htim9, TIM_CHANNEL_2, feed_values_buffer[5]);
- //__HAL_TIM_SET_PRESCALER(&htim12, adc1[5]);
- }
- /*
- * CURRENT LIMITING SECTION
- */
- if (set_rpi_current_limit_values) // Check if current limits will be set by RPi
- {
- current_values_buffer[0] = rpi_rx[37]; // Set M1 current limit via software
- current_values_buffer[1] = rpi_rx[38]; // Set M2 current limit via software
- current_values_buffer[2] = rpi_rx[39]; // Set M3 current limit via software
- current_values_buffer[3] = rpi_rx[40]; // Set M4 current limit via software
- current_values_buffer[4] = rpi_rx[41]; // Set M5 current limit via software
- current_values_buffer[5] = rpi_rx[42]; // Set A6 current limit via software
- current_values_buffer[6] = rpi_rx[43]; // Set A7 current limit via software
- eeprom_buffer[15] = rpi_rx[37]; // Store M1 current limit set via software in eeprom buffer for future storage
- eeprom_buffer[16] = rpi_rx[38]; // Store M2 current limit set via software in eeprom buffer for future storage
- eeprom_buffer[17] = rpi_rx[39]; // Store M3 current limit set via software in eeprom buffer for future storage
- eeprom_buffer[18] = rpi_rx[40]; // Store M4 current limit set via software in eeprom buffer for future storage
- eeprom_buffer[19] = rpi_rx[41]; // Store M5 current limit set via software in eeprom buffer for future storage
- eeprom_buffer[20] = rpi_rx[42]; // Store A6 current limit set via software in eeprom buffer for future storage
- eeprom_buffer[21] = rpi_rx[43]; // Store A7 current limit set via software in eeprom buffer for future storage
- // Stop motors if current limits are exceeded
- if (adc3[3] >= current_values_buffer[0]) Stop_M1();
- if (adc3[4] >= current_values_buffer[1]) Stop_M2();
- if (adc1[1] >= current_values_buffer[2]) Stop_M3();
- if (adc1[2] >= current_values_buffer[3]) Stop_M4_5();
- if (adc1[3] >= current_values_buffer[4]) Stop_M4_5();
- if (adc1[0] >= current_values_buffer[5]) Stop_A6();
- if (adc1[4] >= current_values_buffer[6]) Stop_A7();
- }
- else // else read hardware current values
- {
- // Stop motors if hardware current limits are exceeded
- if (adc3[3] >= 250) Stop_M1();
- if (adc3[4] >= 250) Stop_M2();
- if (adc1[1] >= 250) Stop_M3();
- if (adc1[2] >= 250) Stop_M4_5();
- if (adc1[3] >= 250) Stop_M4_5();
- if (adc1[0] >= 250) Stop_A6();
- if (adc1[4] >= 250) Stop_A7();
- }
- /*
- * RPI TX BUFFER
- */
- //Encoder counts (split as two byte numbers)
- Sixteen_to_Eight(M1_new_count, rpi_tx, 13, 14);
- Sixteen_to_Eight(M2_new_count, rpi_tx, 15, 16);
- Sixteen_to_Eight(M3_new_count, rpi_tx, 17, 18);
- Sixteen_to_Eight(M4_new_count, rpi_tx, 19, 20);
- // Real time feed values
- rpi_tx[29] = adc3[5];
- rpi_tx[30] = adc3[6];
- rpi_tx[31] = adc3[7];
- rpi_tx[32] = adc3[0];
- rpi_tx[33] = adc3[1];
- rpi_tx[34] = adc3[2];
- rpi_tx[35] = adc1[5];
- // Real time current sense values
- rpi_tx[44] = adc3[3];
- rpi_tx[45] = adc3[4];
- rpi_tx[46] = adc1[1];
- rpi_tx[47] = adc1[2];
- rpi_tx[48] = adc1[3];
- rpi_tx[49] = adc1[0];
- rpi_tx[50] = adc1[4];
- /*
- * Endstop status reporting
- */
- if (M1.endstop_status == TRIGGERED) rpi_tx[55] |= (1UL << 7);
- else if (M1.endstop_status == UNTRIGGERED) rpi_tx[55] &= ~(1UL << 7);
- if (M2.endstop_status == TRIGGERED) rpi_tx[55] |= (1UL << 6);
- else if (M2.endstop_status == UNTRIGGERED) rpi_tx[55] &= ~(1UL << 6);
- if (M3.endstop_status == TRIGGERED) rpi_tx[55] |= (1UL << 5);
- else if (M3.endstop_status == UNTRIGGERED) rpi_tx[55] &= ~(1UL << 5);
- //M4 and M5 share the same endstop state bit
- if (M4.endstop_status == TRIGGERED) rpi_tx[55] |= (1UL << 4);
- else if (M4.endstop_status == UNTRIGGERED) rpi_tx[55] &= ~(1UL << 4);
- //M4 and M5 share the same endstop state bit
- if (M5.endstop_status == TRIGGERED) rpi_tx[55] |= (1UL << 4);
- else if (M5.endstop_status == UNTRIGGERED) rpi_tx[55] &= ~(1UL << 4);
- if (A6.endstop_status == TRIGGERED) rpi_tx[55] |= (1UL << 3);
- else if (A6.endstop_status == UNTRIGGERED) rpi_tx[55] &= ~(1UL << 3);
- if (A7.endstop_status == TRIGGERED) rpi_tx[55] |= (1UL << 2);
- else if (A7.endstop_status == UNTRIGGERED) rpi_tx[55] &= ~(1UL << 2);
- if (STPR.endstop_status == TRIGGERED) rpi_tx[55] |= (1UL << 1);
- else if (STPR.endstop_status == UNTRIGGERED) rpi_tx[55] &= ~(1UL << 1);
- /*
- * EEPROM
- */
- if (store_in_eeprom) ee24_write(0, eeprom_buffer, 22, 1000);
- /*
- * HOMING
- */
- if ((rpi_rx[53] & 0xFF) == 1)
- {
- if (((rpi_rx[53] >> 7) & 0xFF) == 1)
- {
- Home_Motor(&M1, CCW);
- }
- else if (((rpi_rx[53] >> 7) & 0xFF) == 0)
- {
- Home_Motor(&M1, CW);
- }
- if (((rpi_rx[53] >> 6) & 0xFF) == 1)
- {
- Home_Motor(&M2, CCW);
- }
- else if (((rpi_rx[53] >> 6) & 0xFF) == 0)
- {
- Home_Motor(&M2, CW);
- }
- if (((rpi_rx[53] >> 5) & 0xFF) == 1)
- {
- Home_Motor(&M3, CCW);
- }
- else if (((rpi_rx[53] >> 5) & 0xFF) == 0)
- {
- Home_Motor(&M3, CW);
- }
- if (((rpi_rx[53] >> 4) & 0xFF) == 1)
- {
- Home_Motor(&M4, CCW);
- }
- else if (((rpi_rx[53] >> 4) & 0xFF) == 0)
- {
- Home_Motor(&M4, CW);
- }
- }
- Parse_Pi_Motor_Commands();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment