ADD PedalDevice
This commit is contained in:
parent
a7c28c0e52
commit
7b62a3634b
72
static_scheduling/pedal_device.cpp
Normal file
72
static_scheduling/pedal_device.cpp
Normal file
@ -0,0 +1,72 @@
|
||||
/****************************************************************************
|
||||
* @file pedal_device.cpp
|
||||
* @author Rémi Heredero <remi@heredero.ch>
|
||||
* @author Yann Sierro <yannsierro.pro@gmail.com>
|
||||
*
|
||||
* @brief Pedal Device implementation (static scheduling)
|
||||
* @date 2024-11-09
|
||||
* @version 0.1.0
|
||||
****************************************************************************/
|
||||
|
||||
#include "pedal_device.hpp"
|
||||
|
||||
// from disco_h747i/wrappers
|
||||
#include <chrono>
|
||||
|
||||
#include "joystick.hpp"
|
||||
#include "mbed_trace.h"
|
||||
|
||||
#if MBED_CONF_MBED_TRACE_ENABLE
|
||||
#define TRACE_GROUP "PedalDevice"
|
||||
#endif // MBED_CONF_MBED_TRACE_ENABLE
|
||||
|
||||
namespace static_scheduling {
|
||||
|
||||
static constexpr std::chrono::microseconds kTaskRunTime = 200000us;
|
||||
|
||||
PedalDevice::PedalDevice(Timer& timer) : _timer(timer) {}
|
||||
|
||||
|
||||
std::chrono::milliseconds PedalDevice::getCurrentRotationTime() {
|
||||
// TODO
|
||||
std::chrono::microseconds initialTime = _timer.elapsed_time();
|
||||
std::chrono::microseconds elapsedTime = std::chrono::microseconds::zero();
|
||||
// we bound the change to one increment/decrement per call
|
||||
bool hasChanged = false;
|
||||
while (elapsedTime < kTaskRunTime) {
|
||||
if (!hasChanged) {
|
||||
disco::Joystick::State joystickState = disco::Joystick::getInstance().getState();
|
||||
|
||||
switch (joystickState) {
|
||||
case disco::Joystick::State::LeftPressed:
|
||||
if (_pedalRotationTime < bike_computer::kMaxPedalRotationTime) {
|
||||
decreaseRotationSpeed();
|
||||
hasChanged = true;
|
||||
}
|
||||
break;
|
||||
|
||||
case disco::Joystick::State::DownPressed:
|
||||
if (_pedalRotationTime > bike_computer::kMinPedalRotationTime) {
|
||||
decreaseRotationSpeed();
|
||||
hasChanged = true;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
elapsedTime = _timer.elapsed_time() - initialTime;
|
||||
}
|
||||
return _pedalRotationTime;
|
||||
}
|
||||
|
||||
void PedalDevice::increaseRotationSpeed() {
|
||||
_pedalRotationTime -= bike_computer::kDeltaPedalRotationTime;
|
||||
}
|
||||
|
||||
void PedalDevice::decreaseRotationSpeed() {
|
||||
_pedalRotationTime += bike_computer::kDeltaPedalRotationTime;
|
||||
}
|
||||
|
||||
}
|
54
static_scheduling/pedal_device.hpp
Normal file
54
static_scheduling/pedal_device.hpp
Normal file
@ -0,0 +1,54 @@
|
||||
// Copyright 2022 Haute école d'ingénierie et d'architecture de Fribourg
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
/****************************************************************************
|
||||
* @file pedal_device.hpp
|
||||
* @author Serge Ayer <serge.ayer@hefr.ch>
|
||||
*
|
||||
* @brief Pedal System header file (static scheduling)
|
||||
*
|
||||
* @date 2023-08-20
|
||||
* @version 1.0.0
|
||||
***************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "constants.hpp"
|
||||
#include "mbed.h"
|
||||
|
||||
namespace static_scheduling {
|
||||
|
||||
class PedalDevice {
|
||||
public:
|
||||
explicit PedalDevice(Timer& timer); // NOLINT(runtime/references)
|
||||
|
||||
// make the class non copyable
|
||||
PedalDevice(PedalDevice&) = delete;
|
||||
PedalDevice& operator=(PedalDevice&) = delete;
|
||||
|
||||
// method called for updating the bike system
|
||||
std::chrono::milliseconds getCurrentRotationTime();
|
||||
|
||||
private:
|
||||
// private methods
|
||||
void increaseRotationSpeed();
|
||||
void decreaseRotationSpeed();
|
||||
|
||||
// data members
|
||||
std::chrono::milliseconds _pedalRotationTime =
|
||||
bike_computer::kInitialPedalRotationTime;
|
||||
Timer& _timer;
|
||||
};
|
||||
|
||||
} // namespace static_scheduling
|
Loading…
x
Reference in New Issue
Block a user