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.9KB

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