Projektarbeit Line Following Robot bei Prof. Chowanetz im WS22/23
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

control_module.cpp 1.7KB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "control_module.h"
  2. ControlModule::ControlModule(): ControlModule(1.0, 1.0, 1.0)
  3. {
  4. }
  5. ControlModule::ControlModule(float forwardSpeed, float rotateSpeed, float moveSideSpeed)
  6. {
  7. motors[0] = 0.0;
  8. motors[1] = 0.0;
  9. motors[2] = 0.0;
  10. motors[3] = 0.0;
  11. this->forwardSpeed = forwardSpeed;
  12. this->rotateSpeed = rotateSpeed;
  13. this->moveSideSpeed = moveSideSpeed;
  14. }
  15. ControlModule::~ControlModule()
  16. {
  17. }
  18. void ControlModule::moveForward(){
  19. for(int i = 0; i < 4; i++){
  20. motors[i] += forwardSpeed;
  21. }
  22. };
  23. void ControlModule::moveSide(int imageColumsMiddle, int contourColumsMiddle){
  24. float speed = moveSideSpeed * static_cast<float>(contourColumsMiddle - imageColumsMiddle)/static_cast<float>(imageColumsMiddle);
  25. motors[0] += speed;
  26. motors[1] -= speed;
  27. motors[2] -= speed;
  28. motors[3] += speed;
  29. }
  30. void ControlModule::rotate(int angle){
  31. float speed = rotateSpeed * (static_cast<float>(angle) + 90.0f)/90.0f;
  32. motors[0] += speed;
  33. motors[1] -= speed;
  34. motors[2] += speed;
  35. motors[3] -= speed;
  36. }
  37. void ControlModule::unit(){
  38. float max = 1.0E-6;
  39. for(int i = 0; i <= sizeof(motors)/sizeof(int); i++){
  40. if(motors[i] > max)
  41. max = motors[i];
  42. }
  43. //Avoid dividing by zero
  44. if (max > 0.001)
  45. {
  46. for(int i = 0; i < 4; i++){
  47. motors[i] /= max;
  48. }
  49. }
  50. }
  51. void ControlModule::calcSpeeds(int imageColumsMiddle, int contourColumsMiddle, int angle){
  52. std::unique_lock<std::mutex> lock(mtx);
  53. moveForward();
  54. moveSide(imageColumsMiddle, contourColumsMiddle);
  55. rotate(angle);
  56. unit();
  57. }
  58. std::vector<float> ControlModule::readMotors()
  59. {
  60. std::unique_lock<std::mutex> lock(mtx);
  61. return std::vector<float> {motors[0], motors[1], motors[2], motors[3]};
  62. }