|
| 1 | +// |
| 2 | +// Motor.h |
| 3 | +// |
| 4 | +// Copyright (c) 2025 Embedded Technology Software Design Robot Contest |
| 5 | +// |
| 6 | + |
| 7 | +#ifndef MOTOR_H_ |
| 8 | +#define MOTOR_H_ |
| 9 | + |
| 10 | +#include "spikeapi.h" |
| 11 | +#include "spike/pup/motor.h" |
| 12 | +#include "Port.h" |
| 13 | + |
| 14 | +/** |
| 15 | + * SPIKE モータクラス |
| 16 | + */ |
| 17 | +class Motor { |
| 18 | + public: |
| 19 | + enum class EDirection { |
| 20 | + CLOCKWISE = PUP_DIRECTION_CLOCKWISE, |
| 21 | + COUNTERCLOCKWISE = PUP_DIRECTION_COUNTERCLOCKWISE, |
| 22 | + }; |
| 23 | + |
| 24 | + /** |
| 25 | + * コンストラクタ |
| 26 | + * @param port PUPポートID |
| 27 | + * @param direction モータの回転方向 |
| 28 | + * @param reset_count カウントをリセットするか |
| 29 | + * pup_motor_setup()を複数回呼ぶとハングするため、コンストラクタで一回だけ呼ぶことでエラーを回避する |
| 30 | + */ |
| 31 | + Motor(Port port, EDirection direction = EDirection::CLOCKWISE, bool reset_count = true) |
| 32 | + : mHasError(false) |
| 33 | + { |
| 34 | + pupDevicePointer = pup_motor_get_device(static_cast<pbio_port_id_t>(port)); |
| 35 | + if(!pupDevicePointer) { |
| 36 | + mHasError = true; |
| 37 | + return; |
| 38 | + } |
| 39 | + pbio_error_t ret |
| 40 | + = pup_motor_setup(pupDevicePointer, static_cast<pup_direction_t>(direction), reset_count); |
| 41 | + if(ret != PBIO_SUCCESS) { |
| 42 | + mHasError = true; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * エンコーダをリセットする |
| 48 | + * @return - |
| 49 | + */ |
| 50 | + void resetCount() const { pup_motor_reset_count(pupDevicePointer); } |
| 51 | + |
| 52 | + /** |
| 53 | + * エンコーダの値を取得する |
| 54 | + * @return エンコーダの値 [°] |
| 55 | + */ |
| 56 | + int32_t getCount() const { return pup_motor_get_count(pupDevicePointer); } |
| 57 | + |
| 58 | + /** |
| 59 | + * モータの回転速度を取得する |
| 60 | + * @return 回転速度 [°/秒] |
| 61 | + */ |
| 62 | + int32_t getSpeed() const { return pup_motor_get_speed(pupDevicePointer); } |
| 63 | + |
| 64 | + /** |
| 65 | + * モータの回転速度を設定する |
| 66 | + * @param speed モータの回転速度 [°/秒] |
| 67 | + * @return - |
| 68 | + */ |
| 69 | + void setSpeed(int speed) const { pup_motor_set_speed(pupDevicePointer, speed); } |
| 70 | + |
| 71 | + /** |
| 72 | + * モータのパワー値を取得する |
| 73 | + * @return パワー値(-100 ~ +100) |
| 74 | + */ |
| 75 | + int32_t getPower() const { return pup_motor_get_power(pupDevicePointer); } |
| 76 | + |
| 77 | + /** |
| 78 | + * モータのパワー値を設定する |
| 79 | + * @param power モータのパワー値(-100 ~ +100) |
| 80 | + * @return - |
| 81 | + */ |
| 82 | + void setPower(int power) const { pup_motor_set_power(pupDevicePointer, power); } |
| 83 | + |
| 84 | + /** |
| 85 | + * モータを止める |
| 86 | + * @return - |
| 87 | + */ |
| 88 | + void stop() const { pup_motor_stop(pupDevicePointer); } |
| 89 | + |
| 90 | + /** |
| 91 | + * ブレーキをかけてモータを止める |
| 92 | + * @return - |
| 93 | + */ |
| 94 | + void brake() const { pup_motor_brake(pupDevicePointer); } |
| 95 | + |
| 96 | + /** |
| 97 | + * モータを止めて角度を維持する |
| 98 | + * @return - |
| 99 | + */ |
| 100 | + void hold() const { pup_motor_hold(pupDevicePointer); } |
| 101 | + |
| 102 | + /** |
| 103 | + * モータがストールしているか調べる |
| 104 | + * @return true ストールしている |
| 105 | + * @return false ストールしていない |
| 106 | + */ |
| 107 | + bool isStalled() const { return pup_motor_is_stalled(pupDevicePointer); } |
| 108 | + |
| 109 | + /** |
| 110 | + * モータのデューティ値を下げる |
| 111 | + * @param duty_limit 新しいデューティ値(0-100) |
| 112 | + * @return 元の状態に戻すための最大電圧 |
| 113 | + */ |
| 114 | + int32_t setDutyLimit(int duty_limit) const |
| 115 | + { |
| 116 | + return pup_motor_set_duty_limit(pupDevicePointer, duty_limit); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * モータのデューティ値を元に戻す |
| 121 | + * @param old_value pup_motor_set_duty_limitの戻り値 |
| 122 | + */ |
| 123 | + void restoreDutyLimit(int old_value) const |
| 124 | + { |
| 125 | + pup_motor_restore_duty_limit(pupDevicePointer, old_value); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * インスタンス生成が正常にできたかどうかを確認するための共通メソッド |
| 130 | + */ |
| 131 | + bool hasError() { return mHasError; } |
| 132 | + |
| 133 | + private: |
| 134 | + pup_motor_t* pupDevicePointer; |
| 135 | + bool mHasError; |
| 136 | + |
| 137 | +}; // class Motor |
| 138 | + |
| 139 | +#endif |
0 commit comments