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 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 minAngularSpeed = -1.0;
  46. double maxAngularSpeed = 1.0;
  47. double speedRange = maxAngularSpeed - minAngularSpeed;
  48. double minAngle = -90.0;
  49. double maxAngle = 90.0;
  50. double angularRange = maxAngle - minAngle;
  51. double progress = angle - minAngle;
  52. double progressPercent = progress/angularRange;
  53. double speed = minAngularSpeed + progressPercent*speedRange;
  54. motors[0] += speed;
  55. motors[1] -= speed;
  56. motors[2] += speed;
  57. motors[3] -= speed;
  58. }
  59. void ControlModule::unit(){
  60. double max = 1.0E-6;
  61. for(int i = 0; i <= sizeof(motors)/sizeof(int); i++){
  62. if(motors[i] > max)
  63. max = motors[i];
  64. }
  65. //Avoid dividing by zero
  66. if (max > 0.001)
  67. {
  68. for(int i = 0; i < 4; i++){
  69. motors[i] /= max;
  70. }
  71. }
  72. }
  73. void ControlModule::calcSpeeds(int imageColumsMiddle, int contourColumsMiddle, double angle){
  74. std::unique_lock<std::mutex> lock(mtx);
  75. //moveSide(imageColumsMiddle, contourColumsMiddle);
  76. rotate(angle);
  77. unit();
  78. adjustSpeed();
  79. }
  80. std::vector<double> ControlModule::readMotors()
  81. {
  82. std::unique_lock<std::mutex> lock(mtx);
  83. return std::vector<double> {motors[0], motors[1], motors[2], motors[3]};
  84. }