chassis.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (c) 2019, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-07-17 Wu Han The first version
  9. * 2019-08-26 sogwms Add move api
  10. */
  11. #include "chassis.h"
  12. #define DBG_SECTION_NAME "chassis"
  13. #define DBG_LEVEL DBG_LOG
  14. #include <rtdbg.h>
  15. chassis_t chassis_create(wheel_t* c_wheels, kinematics_t c_kinematics)
  16. {
  17. // Malloc memory for new chassis
  18. chassis_t new_chassis = (chassis_t) rt_malloc(sizeof(struct chassis));
  19. if(new_chassis == RT_NULL)
  20. {
  21. LOG_E("Falied to allocate memory for chassis\n");
  22. return RT_NULL;
  23. }
  24. new_chassis -> c_wheels = c_wheels;
  25. new_chassis -> c_kinematics = c_kinematics;
  26. return new_chassis;
  27. }
  28. rt_err_t chassis_destroy(chassis_t chas)
  29. {
  30. RT_ASSERT(chas != RT_NULL);
  31. for(int i = 0; i < chas->c_kinematics->total_wheels; i++)
  32. {
  33. LOG_I("Free wheel %d", i);
  34. wheel_destroy(chas->c_wheels[i]);
  35. }
  36. kinematics_destroy(chas->c_kinematics);
  37. rt_free(chas);
  38. return RT_EOK;
  39. }
  40. rt_err_t chassis_enable(chassis_t chas)
  41. {
  42. RT_ASSERT(chas != RT_NULL);
  43. for(int i = 0; i < chas->c_kinematics->total_wheels; i++)
  44. {
  45. LOG_I("Enabling wheel %d", i);
  46. wheel_enable(chas->c_wheels[i]);
  47. }
  48. return RT_EOK;
  49. }
  50. rt_err_t chassis_disable(chassis_t chas)
  51. {
  52. RT_ASSERT(chas != RT_NULL);
  53. for(int i = 0; i < chas->c_kinematics->total_wheels; i++)
  54. {
  55. LOG_I("Disabling wheel %d", i);
  56. wheel_disable(chas->c_wheels[i]);
  57. }
  58. return RT_EOK;
  59. }
  60. rt_err_t chassis_reset(chassis_t chas)
  61. {
  62. RT_ASSERT(chas != RT_NULL);
  63. for(int i = 0; i < chas->c_kinematics->total_wheels; i++)
  64. {
  65. LOG_I("Reset wheel %d", i);
  66. wheel_reset(chas->c_wheels[i]);
  67. }
  68. kinematics_reset(chas->c_kinematics);
  69. return RT_EOK;
  70. }
  71. rt_err_t chassis_set_velocity(chassis_t chas, struct velocity target_velocity)
  72. {
  73. RT_ASSERT(chas != RT_NULL);
  74. rt_int16_t* res_rpm = kinematics_get_rpm(*chas->c_kinematics, target_velocity);
  75. chassis_set_rpm(chas, res_rpm);
  76. return RT_EOK;
  77. }
  78. rt_err_t chassis_set_rpm(chassis_t chas, rt_int16_t target_rpm[])
  79. {
  80. RT_ASSERT(chas != RT_NULL);
  81. // Set new speed
  82. for(int i = 0; i < chas->c_kinematics->total_wheels; i++)
  83. {
  84. LOG_I("Set wheel %d speed %d rpm", i, target_rpm[i]);
  85. wheel_set_rpm(chas->c_wheels[i], target_rpm[i]);
  86. }
  87. return RT_EOK;
  88. }
  89. rt_err_t chassis_update(chassis_t chas)
  90. {
  91. RT_ASSERT(chas != RT_NULL);
  92. for(int i = 0; i < chas->c_kinematics->total_wheels; i++)
  93. {
  94. wheel_update(chas->c_wheels[i]);
  95. }
  96. return RT_EOK;
  97. }
  98. rt_err_t chassis_straight(chassis_t chas, float linear_x)
  99. {
  100. RT_ASSERT(chas != RT_NULL);
  101. struct velocity target_velocity = {
  102. .linear_x = linear_x,
  103. .linear_y = 0.0f,
  104. .angular_z = 0.0f
  105. };
  106. rt_int16_t* res_rpm = kinematics_get_rpm(*chas->c_kinematics, target_velocity);
  107. return chassis_set_rpm(chas, res_rpm);
  108. }
  109. rt_err_t chassis_move(chassis_t chas, float linear_y)
  110. {
  111. RT_ASSERT(chas != RT_NULL);
  112. struct velocity target_velocity = {
  113. .linear_x = 0.0f,
  114. .linear_y = linear_y,
  115. .angular_z = 0.0f
  116. };
  117. rt_int16_t* res_rpm = kinematics_get_rpm(*chas->c_kinematics, target_velocity);
  118. return chassis_set_rpm(chas, res_rpm);
  119. }
  120. rt_err_t chassis_rotate(chassis_t chas, float angular_z)
  121. {
  122. RT_ASSERT(chas != RT_NULL);
  123. struct velocity target_velocity = {
  124. .linear_x = 0.0f,
  125. .linear_y = 0.0f,
  126. .angular_z = angular_z
  127. };
  128. rt_int16_t* res_rpm = kinematics_get_rpm(*chas->c_kinematics, target_velocity);
  129. return chassis_set_rpm(chas, res_rpm);
  130. }
  131. rt_err_t chassis_set_velocity_x(chassis_t chas, float linear_x)
  132. {
  133. RT_ASSERT(chas != RT_NULL);
  134. chas->c_velocity.linear_x = linear_x;
  135. rt_int16_t* res_rpm = kinematics_get_rpm(*chas->c_kinematics, chas->c_velocity);
  136. return chassis_set_rpm(chas, res_rpm);
  137. }
  138. rt_err_t chassis_set_velocity_y(chassis_t chas, float linear_y)
  139. {
  140. RT_ASSERT(chas != RT_NULL);
  141. chas->c_velocity.linear_y = linear_y;
  142. rt_int16_t* res_rpm = kinematics_get_rpm(*chas->c_kinematics, chas->c_velocity);
  143. return chassis_set_rpm(chas, res_rpm);
  144. }
  145. rt_err_t chassis_set_velocity_z(chassis_t chas, float angular_z)
  146. {
  147. RT_ASSERT(chas != RT_NULL);
  148. chas->c_velocity.angular_z = angular_z;
  149. rt_int16_t* res_rpm = kinematics_get_rpm(*chas->c_kinematics, chas->c_velocity);
  150. return chassis_set_rpm(chas, res_rpm);
  151. }