/** * @file manualControl.cpp * @author Alexander Klein (alex@kleiax.de) * @brief Implementation of the class manualControl.h * @version 0.1 * @date 2021-12-13 * * @copyright Copyright (c) 2021 * */ #include "manualControl.h" ManualControl::ManualControl(MoveControl *moveControl, const ControlPadInput *input) { this->moveControl = moveControl; this->input = input; this->moveControl->setDrivingStatus(MoveControl::Status::Drive); } ManualControl::~ManualControl() { this->moveControl->setSpeed(0); this->moveControl->setRotationSpeed(0); this->moveControl->setDrivingStatus(MoveControl::Status::Stop); } void ManualControl::loop() { if (this->caliCompass) this->caliCompass->loop(); if (millis() - this->lastMillis < delay) return; switch (this->inputMode) { case InputMode::Analog : this->analogControl(); break; case InputMode::Digital : this->digitalControl(); break; default: break; } this->lastMillis = millis(); } void ManualControl::switchInputMode() { this->inputMode = (this->inputMode == InputMode::Analog) ? InputMode::Digital : InputMode::Analog; } void ManualControl::analogControl() { // Have to be int16_t to avoid overflow (int8_t = 255 - 127 = -128) int16_t y = this->input->x - 127; int16_t x = this->input->y - 127; // if (x || y) { // std::cout << "x: " << (int) this->input->x << " y: " << (int) this->input->y << std::endl; // std::cout << "x: " << (int) x << " y: " << (int) y << std::endl; // } double value_per_step = this->max_speed * 2 / UINT8_MAX; this->moveControl->setSpeed(-y * value_per_step); value_per_step = this->max_rotation * 2 / UINT8_MAX; this->moveControl->setRotationSpeed(x * value_per_step); } void ManualControl::digitalControl() { int16_t y = this->input->x - 127; int16_t x = this->input->y - 127; if (y > 120) this->moveControl->setSpeed(-this->max_speed); else if (y < -120) this->moveControl->setSpeed(this->max_speed); else this->moveControl->setSpeed(0); if (x > 120) this->moveControl->setRotationSpeed(this->max_rotation); else if (x < -120) this->moveControl->setRotationSpeed(-this->max_rotation); else this->moveControl->setRotationSpeed(0); if (this->directionChangeWrapper && (x > 120 || x < -120)) this->lastLoopTurned = true; else if (this->lastLoopTurned) { if (this->directionChangeWrapper) this->directionChangeWrapper->action(); this->lastLoopTurned = false; } }