user.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * user.c
  3. *
  4. * Created on: 2 июн. 2023 г.
  5. * Author: Andrey Koryagin https://blog.avislab.com/
  6. */
  7. #include "u_foc.h"
  8. #include "position.h"
  9. extern volatile SettingsStruct Settings;
  10. void USER_Init(void) {
  11. MC_ProtocolTimeoutDisable(); // Disable motor stopping on U-FOC Protocol timeout
  12. Position_Init();
  13. }
  14. void USER_While(void) {
  15. HAL_Delay(10);
  16. if (MC_GetState() == MC_STATE_IDLE) {
  17. MC_Start();
  18. }
  19. if (MC_GetState() == MC_STATE_FAULT) {
  20. MC_ResetFault();
  21. }
  22. }
  23. void USER_Tasks(void) {
  24. Position_Process();
  25. }
  26. #ifdef USER_PROTOCOL_ENABLE
  27. #define USER_PACKET_SET_POSITION 60
  28. #define USER_PACKET_GET_POSITION 61
  29. extern uint8_t PACKET_REQUEST[PACKET_LENGTH];
  30. extern uint8_t PACKET_RESPONSE[PACKET_LENGTH];
  31. uint8_t USER_Packet_Handling(void) {
  32. if (PACKET_REQUEST[1] == USER_PACKET_SET_POSITION) { // Set Position
  33. int16_t position;
  34. position = (PACKET_REQUEST[2]<<8) | PACKET_REQUEST[3];
  35. Position_Set(position);
  36. return 0;
  37. }
  38. if (PACKET_REQUEST[1] == USER_PACKET_GET_POSITION) { // Get Position
  39. PACKET_RESPONSE[0]= Settings.DevID;
  40. PACKET_RESPONSE[1]= PACKET_REQUEST[1] + 127;
  41. uint16_t position;
  42. position = Position_Get_Real();
  43. PACKET_RESPONSE[2]= ((position & 0x0000FF00) >> 8);
  44. PACKET_RESPONSE[3]= (position & 0x000000FF);
  45. position = Position_Get();
  46. PACKET_RESPONSE[4]= ((position & 0x0000FF00) >> 8);
  47. PACKET_RESPONSE[5]= (position & 0x000000FF);
  48. PACKET_RESPONSE[6]= 0;
  49. PACKET_RESPONSE[7]= 0;
  50. return 1;
  51. }
  52. return 0;
  53. }
  54. #endif