electrotwelve

Untitled

Mar 20th, 2021
657
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.82 KB | None | 0 0
  1. void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
  2. {
  3.     if (hspi->Instance == SPI1)
  4.     {
  5.         //  if (Eight_to_Sixteen(rpi_rx, 0, rpi_rx, 1) != BOARD_ADDR) // Compare RPi received addr to the hard-coded board addr.
  6.         //  {
  7.         //      wrong_board_addr_flag = TRUE; // If the board addr does not match, then set the appropriate flag and call error handler.
  8.         //      Error_Handler();
  9.         //  }
  10.  
  11.         /*
  12.          * Read the motor ON/OFF bits
  13.          */
  14.  
  15.         if (((rpi_rx[2] >> 7) & 1U) == 1) M1.pi_state = 1;
  16.         else if (((rpi_rx[2] >> 7) & 1U) == 0) M1.pi_state = 0;
  17.  
  18.         if (((rpi_rx[2] >> 6) & 1U) == 1) M2.pi_state = 1;
  19.         else if (((rpi_rx[2] >> 6) & 1U) == 0) M2.pi_state = 0;
  20.  
  21.         if (((rpi_rx[2] >> 5) & 1U) == 1) M3.pi_state = 1;
  22.         else if (((rpi_rx[2] >> 5) & 1U) == 0) M3.pi_state = 0;
  23.  
  24.         //PWM signals are shared between M4 and M5
  25.         if (((rpi_rx[2] >> 4) & 1U) == 1) M4.pi_state = 1;
  26.         else if (((rpi_rx[2] >> 4) & 1U) == 0) M4.pi_state = 0;
  27.  
  28.         //PWM signals are shared between M4 and M5
  29.         if (((rpi_rx[2] >> 4) & 1U) == 1) M5.pi_state = 1;
  30.         else if (((rpi_rx[2] >> 4) & 1U) == 0) M5.pi_state = 0;
  31.  
  32.         if (((rpi_rx[2] >> 3) & 1U) == 1) A6.pi_state = 1;
  33.         else if (((rpi_rx[2] >> 3) & 1U) == 0) A6.pi_state = 0;
  34.  
  35.         if (((rpi_rx[2] >> 2) & 1U) == 1) A7.pi_state = 1;
  36.         else if (((rpi_rx[2] >> 2) & 1U) == 0) A7.pi_state = 0;
  37.  
  38.         if (((rpi_rx[2] >> 1) & 1U) == 1) STPR.pi_state = 1;
  39.         else if (((rpi_rx[2] >> 1) & 1U) == 0) STPR.pi_state = 0;
  40.  
  41.         /*
  42.          M1.pi_state = ((rpi_rx[2] >> 7 & 1U) == 1) ? 1 : 0;
  43.          M2.pi_state = ((rpi_rx[2] >> 6 & 1U) == 1) ? 1 : 0;
  44.          M3.pi_state = ((rpi_rx[2] >> 5 & 1U) == 1) ? 1 : 0;
  45.          M4.pi_state = ((rpi_rx[2] >> 4 & 1U) == 1) ? 1 : 0;
  46.          M5.pi_state = ((rpi_rx[2] >> 4 & 1U) == 1) ? 1 : 0;
  47.          A6.pi_state = ((rpi_rx[2] >> 3 & 1U) == 1) ? 1 : 0;
  48.          A7.pi_state = ((rpi_rx[2] >> 2 & 1U) == 1) ? 1 : 0;
  49.          STPR.pi_state = ((rpi_rx[2] >> 1 & 1U) == 1) ? 1 : 0;
  50.          */
  51.  
  52.         /*
  53.          * Read Motor direction states
  54.          */
  55.  
  56.         if (((rpi_rx[3] >> 7) & 1U) == 1) M1.dir = 1;
  57.         else if (((rpi_rx[3] >> 7) & 1U) == 0) M1.dir = 0;
  58.  
  59.         if (((rpi_rx[3] >> 6) & 1U) == 1) M2.dir = 1;
  60.         else if (((rpi_rx[3] >> 6) & 1U) == 0) M2.dir = 0;
  61.  
  62.         if (((rpi_rx[3] >> 5) & 1U) == 1) M3.dir = 1;
  63.         else if (((rpi_rx[3] >> 5) & 1U) == 0) M3.dir = 0;
  64.  
  65.         //M5 is a mirror of M4. Direction is inverted in hardware
  66.         if (((rpi_rx[3] >> 4) & 1U) == 1) M4.dir = 1;
  67.         else if (((rpi_rx[3] >> 4) & 1U) == 0) M4.dir = 0;
  68.  
  69.         //M5 is a mirror of M4. Direction is inverted in hardware
  70.         if (((rpi_rx[3] >> 4) & 1U) == 1) M5.dir = 1;
  71.         else if (((rpi_rx[3] >> 4) & 1U) == 0) M5.dir = 0;
  72.  
  73.         if (((rpi_rx[3] >> 3) & 1U) == 1) A6.dir = 1;
  74.         else if (((rpi_rx[3] >> 3) & 1U) == 0) A6.dir = 0;
  75.  
  76.         if (((rpi_rx[3] >> 2) & 1U) == 1) A7.dir = 1;
  77.         else if (((rpi_rx[3] >> 2) & 1U) == 0) A7.dir = 0;
  78.  
  79.         if (((rpi_rx[3] >> 1) & 1U) == 1) STPR.dir = 1;
  80.         else if (((rpi_rx[3] >> 1) & 1U) == 0) STPR.dir = 0;
  81.  
  82.         /*
  83.          M1.dir = ((rpi_rx[3] >> 7 & 1U) == 1) ? CCW : CW;
  84.          M2.dir = ((rpi_rx[3] >> 6 & 1U) == 1) ? CCW : CW;
  85.          M3.dir = ((rpi_rx[3] >> 5 & 1U) == 1) ? CCW : CW;
  86.  
  87.          M4.dir = ((rpi_rx[3] >> 4 & 1U) == 1) ? CCW : CW;
  88.          M5.dir = ((rpi_rx[3] >> 4 & 1U) == 1) ? CCW : CW;
  89.          // M5 direction bit is inverted in hardware.
  90.          // Hence it will be the same as M4 in software.
  91.  
  92.          A6.dir = ((rpi_rx[3] >> 3 & 1U) == 1) ? CCW : CW;
  93.          A7.dir = ((rpi_rx[3] >> 2 & 1U) == 1) ? CCW : CW;
  94.          STPR.dir = ((rpi_rx[3] >> 1 & 1U) == 1) ? CCW : CW;
  95.          */
  96.  
  97.         // Check if the Pi is supposed to set the encoder values
  98.         if (rpi_rx[4] == 1) set_rpi_encoder_values = 1;
  99.         else if (rpi_rx[4] == 0) set_rpi_encoder_values = 0;
  100.  
  101.         // Check if the Pi is supposed to set the feed values
  102.         if (rpi_rx[21] == 1) set_rpi_feed_values = 1;
  103.         else if (rpi_rx[21] == 0) set_rpi_feed_values = 0;
  104.  
  105.         // Check if current limit values are to be set via the Pi
  106.         if (rpi_rx[36] == 1) set_rpi_current_limit_values = 1;
  107.         else if (rpi_rx[36] == 0) set_rpi_current_limit_values = 0;
  108.  
  109.         // Check if remote is made operational
  110.         if (rpi_rx[51] == 1) use_remote = 1;
  111.         else if (rpi_rx[51] == 0) use_remote = 0;
  112.  
  113.         // Check if value is to be stored in the EEPROM
  114.         if (rpi_rx[52] == 1) store_in_eeprom = 1;
  115.         else if (rpi_rx[52] == 0) store_in_eeprom = 0;
  116.  
  117.         // Check if homing is enabled
  118.         if (rpi_rx[53] == 1) homing_enabled = 1;
  119.         else if (rpi_rx[53] == 0) homing_enabled = 0;
  120.  
  121.         if (rpi_rx[54] == 1)
  122.         {
  123.             CW = 1;
  124.             CCW = 0;
  125.         }
  126.  
  127.         // Get real time encoder counts
  128.         M1_new_count = M1.total_count; //Get_Encoder_Count(&M1, &htim1);
  129.         M2_new_count = M2.total_count; //Get_Encoder_Count(&M2, &htim1);
  130.         M3_new_count = M3.total_count; //Get_Encoder_Count(&M3, &htim1);
  131.         M4_new_count = M4.total_count; //Get_Encoder_Count(&M4, &htim1);
  132.  
  133.         if (set_rpi_encoder_values) // Check if encoder values are to be set by software
  134.         {
  135.             encoder_values_buffer[0] = Eight_to_Sixteen(rpi_rx, 5, rpi_rx, 6);   // M1 software set encoder count
  136.             encoder_values_buffer[1] = Eight_to_Sixteen(rpi_rx, 7, rpi_rx, 8);   // M2 software set encoder count
  137.             encoder_values_buffer[2] = Eight_to_Sixteen(rpi_rx, 9, rpi_rx, 10);  // M3 software set encoder count
  138.             encoder_values_buffer[3] = Eight_to_Sixteen(rpi_rx, 11, rpi_rx, 12); // M4 software set encoder count
  139.         }
  140.         else // if not set by RPi then use hardware encoder values
  141.         {
  142.             encoder_values_buffer[0] = M1_new_count; // M1 hardware set encoder count
  143.             encoder_values_buffer[1] = M2_new_count; // M2 hardware set encoder count
  144.             encoder_values_buffer[2] = M3_new_count; // M3 hardware set encoder count
  145.             encoder_values_buffer[3] = M4_new_count; // M4 hardware set encoder count
  146.         }
  147.  
  148.         M1_previous_count = M1_new_count;
  149.         M2_previous_count = M2_new_count;
  150.         M3_previous_count = M3_new_count;
  151.         M4_previous_count = M4_new_count;
  152.  
  153.         /*
  154.          * Populate the EEPROM buffer with the encoder counts
  155.          */
  156.  
  157.         Sixteen_to_Eight(M1_new_count, eeprom_buffer, 0, 1);
  158.         Sixteen_to_Eight(M2_new_count, eeprom_buffer, 2, 3);
  159.         Sixteen_to_Eight(M3_new_count, eeprom_buffer, 4, 5);
  160.         Sixteen_to_Eight(M4_new_count, eeprom_buffer, 6, 7);
  161.  
  162.         /*
  163.          * FEED PARSING SECTION
  164.          */
  165.  
  166.         if (set_rpi_feed_values) // Check if feed values are to be set by the RPi
  167.         {
  168.             // Adjust duty cycle accordingly
  169.             TIM5->CCR1 = ADC_to_Feed(rpi_rx, 22); // M1 software set feed
  170.             TIM5->CCR2 = ADC_to_Feed(rpi_rx, 23); // M2 software set feed
  171.             TIM5->CCR3 = ADC_to_Feed(rpi_rx, 24); // M3 software set feed
  172.             TIM5->CCR4 = ADC_to_Feed(rpi_rx, 25); // M4/5 software set feed
  173.             TIM9->CCR1 = ADC_to_Feed(rpi_rx, 26); // A6 software set feed
  174.             TIM9->CCR2 = ADC_to_Feed(rpi_rx, 27); // A7 software set feed
  175.             TIM12->PSC = ADC_to_Stepper_Feed(rpi_rx, 28); // Stepper software set feed
  176.  
  177.             //__HAL_TIM_SET_COMPARE(&htim5, TIM_CHANNEL_1, feed_values_buffer[0]);
  178.             //__HAL_TIM_SET_COMPARE(&htim5, TIM_CHANNEL_2, feed_values_buffer[1]);
  179.             //__HAL_TIM_SET_COMPARE(&htim5, TIM_CHANNEL_3, feed_values_buffer[2]);
  180.             //__HAL_TIM_SET_COMPARE(&htim5, TIM_CHANNEL_4, feed_values_buffer[3]);
  181.             //__HAL_TIM_SET_COMPARE(&htim9, TIM_CHANNEL_1, feed_values_buffer[4]);
  182.             //__HAL_TIM_SET_COMPARE(&htim9, TIM_CHANNEL_2, feed_values_buffer[5]);
  183.             //__HAL_TIM_SET_PRESCALER(&htim12, ADC_to_Stepper_Feed(feed_values_buffer, 6));
  184.  
  185.             // Store values received from the RPi in the eeprom buffer for future storage
  186.             eeprom_buffer[8] = rpi_rx[22];  // Store M1 software set feed in eeprom buffer
  187.             eeprom_buffer[9] = rpi_rx[23];  // Store M2 software set feed in eeprom buffer
  188.             eeprom_buffer[10] = rpi_rx[24]; // Store M3 software set feed in eeprom buffer
  189.             eeprom_buffer[11] = rpi_rx[25]; // Store M4/5 software set feed in eeprom buffer
  190.             eeprom_buffer[12] = rpi_rx[26]; // Store A6 software set feed in eeprom buffer
  191.             eeprom_buffer[13] = rpi_rx[27]; // Store A7 software set feed in eeprom buffer
  192.             eeprom_buffer[14] = rpi_rx[28]; // Store Stepper software set feed in eeprom buffer
  193.         }
  194.         else // else take the hardware feed values
  195.         {
  196.             TIM5->CCR1 = ADC_to_Feed(adc3, 5); // M1 hardware set feed
  197.             TIM5->CCR2 = ADC_to_Feed(adc3, 6); // M2 hardware set feed
  198.             TIM5->CCR3 = ADC_to_Feed(adc3, 7); // M3 hardware set feed
  199.             TIM5->CCR4 = ADC_to_Feed(adc3, 0); // M4/5 hardware set feed
  200.             TIM9->CCR1 = ADC_to_Feed(adc3, 2); // A6 hardware set feed
  201.             TIM9->CCR2 = ADC_to_Feed(adc3, 5); // A7 hardware set feed
  202.             TIM12->PSC = ADC_to_Stepper_Feed(adc1, 5); // Stepper hardware set feed
  203.  
  204.             //__HAL_TIM_SET_COMPARE(&htim5, TIM_CHANNEL_1, feed_values_buffer[0]);
  205.             //__HAL_TIM_SET_COMPARE(&htim5, TIM_CHANNEL_2, feed_values_buffer[1]);
  206.             //__HAL_TIM_SET_COMPARE(&htim5, TIM_CHANNEL_3, feed_values_buffer[2]);
  207.             //__HAL_TIM_SET_COMPARE(&htim5, TIM_CHANNEL_4, feed_values_buffer[3]);
  208.             //__HAL_TIM_SET_COMPARE(&htim9, TIM_CHANNEL_1, feed_values_buffer[4]);
  209.             //__HAL_TIM_SET_COMPARE(&htim9, TIM_CHANNEL_2, feed_values_buffer[5]);
  210.             //__HAL_TIM_SET_PRESCALER(&htim12, adc1[5]);
  211.         }
  212.  
  213.         /*
  214.          * CURRENT LIMITING SECTION
  215.          */
  216.  
  217.         if (set_rpi_current_limit_values) // Check if current limits will be set by RPi
  218.         {
  219.             current_values_buffer[0] = rpi_rx[37]; // Set M1 current limit via software
  220.             current_values_buffer[1] = rpi_rx[38]; // Set M2 current limit via software
  221.             current_values_buffer[2] = rpi_rx[39]; // Set M3 current limit via software
  222.             current_values_buffer[3] = rpi_rx[40]; // Set M4 current limit via software
  223.             current_values_buffer[4] = rpi_rx[41]; // Set M5 current limit via software
  224.             current_values_buffer[5] = rpi_rx[42]; // Set A6 current limit via software
  225.             current_values_buffer[6] = rpi_rx[43]; // Set A7 current limit via software
  226.  
  227.             eeprom_buffer[15] = rpi_rx[37]; // Store M1 current limit set via software in eeprom buffer for future storage
  228.             eeprom_buffer[16] = rpi_rx[38]; // Store M2 current limit set via software in eeprom buffer for future storage
  229.             eeprom_buffer[17] = rpi_rx[39]; // Store M3 current limit set via software in eeprom buffer for future storage
  230.             eeprom_buffer[18] = rpi_rx[40]; // Store M4 current limit set via software in eeprom buffer for future storage
  231.             eeprom_buffer[19] = rpi_rx[41]; // Store M5 current limit set via software in eeprom buffer for future storage
  232.             eeprom_buffer[20] = rpi_rx[42]; // Store A6 current limit set via software in eeprom buffer for future storage
  233.             eeprom_buffer[21] = rpi_rx[43]; // Store A7 current limit set via software in eeprom buffer for future storage
  234.  
  235.             // Stop motors if current limits are exceeded
  236.             if (adc3[3] >= current_values_buffer[0]) Stop_M1();
  237.             if (adc3[4] >= current_values_buffer[1]) Stop_M2();
  238.             if (adc1[1] >= current_values_buffer[2]) Stop_M3();
  239.             if (adc1[2] >= current_values_buffer[3]) Stop_M4_5();
  240.             if (adc1[3] >= current_values_buffer[4]) Stop_M4_5();
  241.             if (adc1[0] >= current_values_buffer[5]) Stop_A6();
  242.             if (adc1[4] >= current_values_buffer[6]) Stop_A7();
  243.         }
  244.         else // else read hardware current values
  245.         {
  246.             // Stop motors if hardware current limits are exceeded
  247.             if (adc3[3] >= 250) Stop_M1();
  248.             if (adc3[4] >= 250) Stop_M2();
  249.             if (adc1[1] >= 250) Stop_M3();
  250.             if (adc1[2] >= 250) Stop_M4_5();
  251.             if (adc1[3] >= 250) Stop_M4_5();
  252.             if (adc1[0] >= 250) Stop_A6();
  253.             if (adc1[4] >= 250) Stop_A7();
  254.         }
  255.  
  256.         /*
  257.          * RPI TX BUFFER
  258.          */
  259.  
  260.         //Encoder counts (split as two byte numbers)
  261.         Sixteen_to_Eight(M1_new_count, rpi_tx, 13, 14);
  262.         Sixteen_to_Eight(M2_new_count, rpi_tx, 15, 16);
  263.         Sixteen_to_Eight(M3_new_count, rpi_tx, 17, 18);
  264.         Sixteen_to_Eight(M4_new_count, rpi_tx, 19, 20);
  265.  
  266.         // Real time feed values
  267.         rpi_tx[29] = adc3[5];
  268.         rpi_tx[30] = adc3[6];
  269.         rpi_tx[31] = adc3[7];
  270.         rpi_tx[32] = adc3[0];
  271.         rpi_tx[33] = adc3[1];
  272.         rpi_tx[34] = adc3[2];
  273.         rpi_tx[35] = adc1[5];
  274.  
  275.         // Real time current sense values
  276.         rpi_tx[44] = adc3[3];
  277.         rpi_tx[45] = adc3[4];
  278.         rpi_tx[46] = adc1[1];
  279.         rpi_tx[47] = adc1[2];
  280.         rpi_tx[48] = adc1[3];
  281.         rpi_tx[49] = adc1[0];
  282.         rpi_tx[50] = adc1[4];
  283.  
  284.         /*
  285.          * Endstop status reporting
  286.          */
  287.  
  288.         if (M1.endstop_status == TRIGGERED) rpi_tx[55] |= (1UL << 7);
  289.         else if (M1.endstop_status == UNTRIGGERED) rpi_tx[55] &= ~(1UL << 7);
  290.  
  291.         if (M2.endstop_status == TRIGGERED) rpi_tx[55] |= (1UL << 6);
  292.         else if (M2.endstop_status == UNTRIGGERED) rpi_tx[55] &= ~(1UL << 6);
  293.  
  294.         if (M3.endstop_status == TRIGGERED) rpi_tx[55] |= (1UL << 5);
  295.         else if (M3.endstop_status == UNTRIGGERED) rpi_tx[55] &= ~(1UL << 5);
  296.  
  297.         //M4 and M5 share the same endstop state bit
  298.         if (M4.endstop_status == TRIGGERED) rpi_tx[55] |= (1UL << 4);
  299.         else if (M4.endstop_status == UNTRIGGERED) rpi_tx[55] &= ~(1UL << 4);
  300.  
  301.         //M4 and M5 share the same endstop state bit
  302.         if (M5.endstop_status == TRIGGERED) rpi_tx[55] |= (1UL << 4);
  303.         else if (M5.endstop_status == UNTRIGGERED) rpi_tx[55] &= ~(1UL << 4);
  304.  
  305.         if (A6.endstop_status == TRIGGERED) rpi_tx[55] |= (1UL << 3);
  306.         else if (A6.endstop_status == UNTRIGGERED) rpi_tx[55] &= ~(1UL << 3);
  307.  
  308.         if (A7.endstop_status == TRIGGERED) rpi_tx[55] |= (1UL << 2);
  309.         else if (A7.endstop_status == UNTRIGGERED) rpi_tx[55] &= ~(1UL << 2);
  310.  
  311.         if (STPR.endstop_status == TRIGGERED) rpi_tx[55] |= (1UL << 1);
  312.         else if (STPR.endstop_status == UNTRIGGERED) rpi_tx[55] &= ~(1UL << 1);
  313.  
  314.         /*
  315.          * EEPROM
  316.          */
  317.         if (store_in_eeprom) ee24_write(0, eeprom_buffer, 22, 1000);
  318.  
  319.         /*
  320.          * HOMING
  321.          */
  322.  
  323.         if ((rpi_rx[53] & 0xFF) == 1)
  324.         {
  325.             if (((rpi_rx[53] >> 7) & 0xFF) == 1)
  326.             {
  327.                 Home_Motor(&M1, CCW);
  328.             }
  329.             else if (((rpi_rx[53] >> 7) & 0xFF) == 0)
  330.             {
  331.                 Home_Motor(&M1, CW);
  332.             }
  333.  
  334.             if (((rpi_rx[53] >> 6) & 0xFF) == 1)
  335.             {
  336.                 Home_Motor(&M2, CCW);
  337.             }
  338.             else if (((rpi_rx[53] >> 6) & 0xFF) == 0)
  339.             {
  340.                 Home_Motor(&M2, CW);
  341.             }
  342.  
  343.             if (((rpi_rx[53] >> 5) & 0xFF) == 1)
  344.             {
  345.                 Home_Motor(&M3, CCW);
  346.             }
  347.             else if (((rpi_rx[53] >> 5) & 0xFF) == 0)
  348.             {
  349.                 Home_Motor(&M3, CW);
  350.             }
  351.  
  352.             if (((rpi_rx[53] >> 4) & 0xFF) == 1)
  353.             {
  354.                 Home_Motor(&M4, CCW);
  355.             }
  356.             else if (((rpi_rx[53] >> 4) & 0xFF) == 0)
  357.             {
  358.                 Home_Motor(&M4, CW);
  359.             }
  360.         }
  361.  
  362.         Parse_Pi_Motor_Commands();
  363.     }
  364. }
Advertisement
Add Comment
Please, Sign In to add comment