controller.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include "controller.h"
  2. #define DBG_SECTION_NAME "controller"
  3. #define DBG_LEVEL DBG_LOG
  4. #include <rtdbg.h>
  5. // single intput and single output system
  6. controller_t controller_create(rt_size_t size, rt_uint16_t sample_time)
  7. {
  8. // TODO
  9. // Malloc memory and initialize PID
  10. controller_t new_controller = (controller_t)rt_malloc(size);
  11. if(new_controller == RT_NULL)
  12. {
  13. LOG_E("Failed to malloc memory for automatic controller\n");
  14. return RT_NULL;
  15. }
  16. new_controller->sample_time = sample_time;
  17. new_controller->enable = RT_FALSE;
  18. return new_controller;
  19. }
  20. rt_err_t controller_update(controller_t controller, float current_point)
  21. {
  22. RT_ASSERT(controller != RT_NULL);
  23. if (controller->enable)
  24. {
  25. return controller->update(controller, current_point);
  26. }
  27. else
  28. {
  29. controller->output = controller->target;
  30. }
  31. return RT_EOK;
  32. }
  33. rt_err_t controller_destroy(controller_t controller)
  34. {
  35. RT_ASSERT(controller != RT_NULL);
  36. return controller->destroy(controller);
  37. }
  38. rt_err_t controller_enable(controller_t controller)
  39. {
  40. // TODO
  41. RT_ASSERT(controller != RT_NULL);
  42. controller->enable = RT_TRUE;
  43. return RT_EOK;
  44. }
  45. rt_err_t controller_disable(controller_t controller)
  46. {
  47. // TODO
  48. RT_ASSERT(controller != RT_NULL);
  49. controller->enable = RT_FALSE;
  50. return RT_EOK;
  51. }
  52. rt_err_t controller_reset(controller_t controller)
  53. {
  54. // TODO
  55. RT_ASSERT(controller != RT_NULL);
  56. return controller->reset(controller);
  57. }
  58. rt_err_t controller_set_target(controller_t controller, rt_int16_t target)
  59. {
  60. RT_ASSERT(controller != RT_NULL);
  61. controller->target = target;
  62. return RT_EOK;
  63. }
  64. rt_err_t controller_set_sample_time(controller_t controller, rt_uint16_t sample_time)
  65. {
  66. RT_ASSERT(controller != RT_NULL);
  67. controller->sample_time = sample_time;
  68. return RT_EOK;
  69. }
  70. rt_err_t controller_set_param(controller_t controller, controller_param_t param)
  71. {
  72. RT_ASSERT(controller != RT_NULL);
  73. return controller->set_param(controller, param);
  74. }