-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #186 from rogy-AquaLab/device
- Loading branch information
Showing
6 changed files
with
215 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#ifndef OMNIBOAT_ROBOKIT_DEVICE_INPUT_HPP | ||
#define OMNIBOAT_ROBOKIT_DEVICE_INPUT_HPP | ||
|
||
#include <array> | ||
#include <utility> | ||
|
||
#include "AnalogIn.h" | ||
|
||
#include "MPU6050.h" | ||
#include "packet/input.hpp" | ||
|
||
namespace device { | ||
|
||
/// センサー類まとめ | ||
class InputModules { | ||
private: | ||
/// ジョイスティックのピン2つ | ||
std::pair<mbed::AnalogIn, mbed::AnalogIn> joy; | ||
|
||
/// ツマミ | ||
mbed::AnalogIn volume; | ||
|
||
/// 慣性計測ユニット(imu) | ||
MPU6050 mpu; | ||
|
||
/// ジョイスティックの値を読む | ||
auto read_joy() -> std::pair<float, float>; | ||
|
||
/// IMUの値を読む | ||
auto read_gyro() -> std::array<float, 3>; | ||
|
||
public: | ||
/** | ||
* @brief Construct a new Input Modules object | ||
* @param joy_pins ジョイスティックのピン2つ | ||
* @param volume_pin ツマミのピン | ||
* @param mpu_pins MPU6050のピン (sda, scl) | ||
*/ | ||
InputModules( | ||
const std::pair<PinName, PinName>& joy_pins, const PinName& volume_pin, | ||
const std::pair<PinName, PinName>& mpu_pins); | ||
|
||
InputModules() = delete; | ||
~InputModules() = default; | ||
InputModules(const InputModules&) = delete; | ||
auto operator=(const InputModules&) -> InputModules& = delete; | ||
InputModules(InputModules&&) = default; | ||
auto operator=(InputModules&&) -> InputModules& = default; | ||
|
||
/** | ||
* @brief MPU6050のWHOAMI `MPU6050::testConnection` 参照 | ||
* @return true success | ||
* @return false failure | ||
*/ | ||
auto mpu_whoami() -> bool; | ||
|
||
/** | ||
* @brief センサー類の値を読む | ||
* @return packet::InputValues 読んだ値まとめ | ||
*/ | ||
auto read() -> packet::InputValues; | ||
}; | ||
|
||
} // namespace device | ||
|
||
#endif // OMNIBOAT_ROBOKIT_DEVICE_INPUT_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#ifndef OMNIBOAT_ROBOKIT_DEVICE_OUTPUT_HPP | ||
#define OMNIBOAT_ROBOKIT_DEVICE_OUTPUT_HPP | ||
|
||
#include <utility> | ||
|
||
#include "PwmOut.h" | ||
|
||
#include "packet/output.hpp" | ||
|
||
namespace device { | ||
|
||
/// アクチュエーター類 | ||
class OutputModules { | ||
private: | ||
/// サーボ2つ | ||
std::pair<mbed::PwmOut, mbed::PwmOut> servo; | ||
|
||
/// DCモーター (につながっているFET) 2つ | ||
std::pair<mbed::PwmOut, mbed::PwmOut> dc_motor; | ||
|
||
/** | ||
* @brief サーボに出力する | ||
* @param values 出力する値 `OutputValues::servo` と同じ | ||
*/ | ||
auto write_servo(const std::pair<float, float>& values) -> void; | ||
|
||
/** | ||
* @brief DCモーターに出力する | ||
* @param values 出力する値 `OutputModules::dc_motor` と同じ | ||
*/ | ||
auto write_dc_motor(const std::pair<float, float>& values) -> void; | ||
|
||
public: | ||
/** | ||
* @brief Construct a new Output Modules object | ||
* @param servo_pins サーボのピン2つ | ||
* @param dc_motor_pins DCモーター (につながっているFET) のピン2つ | ||
*/ | ||
OutputModules( | ||
const std::pair<PinName, PinName>& servo_pins, | ||
const std::pair<PinName, PinName>& dc_motor_pins); | ||
|
||
OutputModules() = delete; | ||
~OutputModules() = default; | ||
OutputModules(const OutputModules&) = delete; | ||
auto operator=(const OutputModules&) -> OutputModules& = delete; | ||
OutputModules(OutputModules&&) = default; | ||
auto operator=(OutputModules&&) -> OutputModules& = default; | ||
|
||
/** | ||
* @brief セットアップ処理 | ||
* @details PWMの周期設定 | ||
*/ | ||
auto init() -> void; | ||
|
||
/** | ||
* @brief アクチュエーターに出力する | ||
* @param output 出力する値 | ||
*/ | ||
auto write(const packet::OutputValues& output) -> void; | ||
}; | ||
|
||
} // namespace device | ||
|
||
#endif // OMNIBOAT_ROBOKIT_DEVICE_OUTPUT_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include "device/input.hpp" | ||
|
||
auto device::InputModules::read_joy() -> std::pair<float, float> { | ||
return {this->joy.first.read(), this->joy.second.read()}; | ||
} | ||
|
||
auto device::InputModules::read_gyro() -> std::array<float, 3> { | ||
std::array<float, 3> gyro; | ||
this->mpu.getGyro(gyro.data()); | ||
return gyro; | ||
} | ||
|
||
// NOLINTBEGIN(bugprone-easily-swappable-parameters) | ||
device::InputModules::InputModules( | ||
const std::pair<PinName, PinName>& joy_pins, const PinName& volume_pin, | ||
const std::pair<PinName, PinName>& mpu_pins) : | ||
joy(joy_pins.first, joy_pins.second), | ||
volume(volume_pin), | ||
mpu(mpu_pins.first, mpu_pins.second) {} | ||
// NOLINTEND(bugprone-easily-swappable-parameters) | ||
|
||
auto device::InputModules::mpu_whoami() -> bool { | ||
return this->mpu.testConnection(); | ||
} | ||
|
||
auto device::InputModules::read() -> packet::InputValues { | ||
const std::pair<float, float> joy = this->read_joy(); | ||
const float volume = this->volume.read(); | ||
const std::array<float, 3> gyro = this->read_gyro(); | ||
return packet::InputValues(joy, volume, gyro); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include "device/output.hpp" | ||
#include "utils.hpp" | ||
|
||
// サーボモータ出力値(pulse width) | ||
// FIXME: もともと550, 2350だったので要検証 | ||
constexpr int minor_pulsewidth_us = 500; | ||
constexpr int major_pulsewidth_us = 2400; | ||
|
||
/// ラジアン → PWM pulsewidth_us | ||
constexpr auto servo_value_map(float radian) -> int { | ||
return static_cast<int>( | ||
(major_pulsewidth_us - minor_pulsewidth_us) * (radian / utils::PI) + minor_pulsewidth_us); | ||
} | ||
|
||
auto device::OutputModules::write_servo(const std::pair<float, float>& values) -> void { | ||
this->servo.first.pulsewidth_us(servo_value_map(values.first)); | ||
this->servo.second.pulsewidth_us(servo_value_map(values.second)); | ||
} | ||
|
||
auto device::OutputModules::write_dc_motor(const std::pair<float, float>& values) -> void { | ||
this->dc_motor.first.write(values.first); | ||
this->dc_motor.second.write(values.second); | ||
} | ||
|
||
// NOLINTBEGIN(bugprone-easily-swappable-parameters) | ||
device::OutputModules::OutputModules( | ||
const std::pair<PinName, PinName>& servo_pins, | ||
const std::pair<PinName, PinName>& dc_motor_pins) : | ||
servo(servo_pins.first, servo_pins.second), | ||
dc_motor(dc_motor_pins.first, dc_motor_pins.second) {} | ||
// NOLINTEND(bugprone-easily-swappable-parameters) | ||
|
||
auto device::OutputModules::init() -> void { | ||
constexpr int pwm_period_ms = 20; | ||
|
||
this->servo.first.period_ms(pwm_period_ms); | ||
this->servo.second.period_ms(pwm_period_ms); | ||
} | ||
|
||
auto device::OutputModules::write(const packet::OutputValues& output) -> void { | ||
this->write_servo(output.servo); | ||
this->write_dc_motor(output.dc_motor); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters