chassis.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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[4];
  75. kinematics_get_rpm(*chas->c_kinematics, target_velocity, res_rpm);
  76. chassis_set_rpm(chas, res_rpm);
  77. return RT_EOK;
  78. }
  79. rt_err_t chassis_set_rpm(chassis_t chas, rt_int16_t target_rpm[])
  80. {
  81. RT_ASSERT(chas != RT_NULL);
  82. // Set new speed
  83. for(int i = 0; i < chas->c_kinematics->total_wheels; i++)
  84. {
  85. LOG_I("Set wheel %d speed %d rpm", i, target_rpm[i]);
  86. wheel_set_rpm(chas->c_wheels[i], target_rpm[i]);
  87. }
  88. return RT_EOK;
  89. }
  90. rt_err_t chassis_update(chassis_t chas)
  91. {
  92. RT_ASSERT(chas != RT_NULL);
  93. for(int i = 0; i < chas->c_kinematics->total_wheels; i++)
  94. {
  95. wheel_update(chas->c_wheels[i]);
  96. }
  97. return RT_EOK;
  98. }
  99. rt_err_t chassis_straight(chassis_t chas, float linear_x)
  100. {
  101. RT_ASSERT(chas != RT_NULL);
  102. struct velocity target_velocity = {
  103. .linear_x = linear_x,
  104. .linear_y = 0.0f,
  105. .angular_z = 0.0f
  106. };
  107. rt_int16_t res_rpm[4];
  108. kinematics_get_rpm(*chas->c_kinematics, target_velocity, res_rpm);
  109. return chassis_set_rpm(chas, res_rpm);
  110. }
  111. rt_err_t chassis_move(chassis_t chas, float linear_y)
  112. {
  113. RT_ASSERT(chas != RT_NULL);
  114. struct velocity target_velocity = {
  115. .linear_x = 0.0f,
  116. .linear_y = linear_y,
  117. .angular_z = 0.0f
  118. };
  119. rt_int16_t res_rpm[4];
  120. kinematics_get_rpm(*chas->c_kinematics, target_velocity, res_rpm);
  121. return chassis_set_rpm(chas, res_rpm);
  122. }
  123. rt_err_t chassis_rotate(chassis_t chas, float angular_z)
  124. {
  125. RT_ASSERT(chas != RT_NULL);
  126. struct velocity target_velocity = {
  127. .linear_x = 0.0f,
  128. .linear_y = 0.0f,
  129. .angular_z = angular_z
  130. };
  131. rt_int16_t res_rpm[4];
  132. kinematics_get_rpm(*chas->c_kinematics, target_velocity, res_rpm);
  133. return chassis_set_rpm(chas, res_rpm);
  134. }
  135. rt_err_t chassis_set_velocity_x(chassis_t chas, float linear_x)
  136. {
  137. RT_ASSERT(chas != RT_NULL);
  138. chas->c_velocity.linear_x = linear_x;
  139. rt_int16_t res_rpm[4];
  140. kinematics_get_rpm(*chas->c_kinematics, chas->c_velocity, res_rpm);
  141. return chassis_set_rpm(chas, res_rpm);
  142. }
  143. rt_err_t chassis_set_velocity_y(chassis_t chas, float linear_y)
  144. {
  145. RT_ASSERT(chas != RT_NULL);
  146. chas->c_velocity.linear_y = linear_y;
  147. rt_int16_t res_rpm[4];
  148. kinematics_get_rpm(*chas->c_kinematics, chas->c_velocity, res_rpm);
  149. return chassis_set_rpm(chas, res_rpm);
  150. }
  151. rt_err_t chassis_set_velocity_z(chassis_t chas, float angular_z)
  152. {
  153. RT_ASSERT(chas != RT_NULL);
  154. chas->c_velocity.angular_z = angular_z;
  155. rt_int16_t res_rpm[4];
  156. kinematics_get_rpm(*chas->c_kinematics, chas->c_velocity, res_rpm);
  157. return chassis_set_rpm(chas, res_rpm);
  158. }