Skip to content

Commit 6f2770a

Browse files
committed
add: 実機検証用
1 parent cbde8c8 commit 6f2770a

File tree

5 files changed

+366
-1
lines changed

5 files changed

+366
-1
lines changed

Makefile.inc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ SRCLANG := c++
1212

1313
APPL_LIBS += -lm
1414

15-
APPL_DIRS += $(mkfile_path)modules
15+
APPL_DIRS += \
16+
$(mkfile_path)modules\
17+
$(mkfile_path)modules/API
18+
19+
1620

1721
INCLUDES += \
1822
-I$(mkfile_path)modules\
23+
-I$(mkfile_path)modules/API

modules/API/Controller.cpp

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* @file Controller.cpp
3+
* @brief モーター制御に用いる関数をまとめたラッパークラス
4+
* @author takahashitom CHIHAYATAKU
5+
*/
6+
#include "Controller.h"
7+
8+
Controller::Controller()
9+
: rightWheel(Port::PORT_A),
10+
leftWheel(Port::PORT_B, EDirection::COUNTERCLOCKWISE),
11+
armMotor(Port::PORT_C)
12+
{
13+
}
14+
15+
// power値の初期化
16+
int Controller::powerOfRightWheel = 0.0;
17+
int Controller::powerOfLeftWheel = 0.0;
18+
int Controller::powerOfArm = 0.0;
19+
20+
// モータに設定するPWM値の制限
21+
int Controller::limitPowerValue(const int inputPower)
22+
{
23+
if(inputPower > MOTOR_POWER_MAX) {
24+
return MOTOR_POWER_MAX;
25+
} else if(inputPower < MOTOR_POWER_MIN) {
26+
return MOTOR_POWER_MIN;
27+
}
28+
return inputPower;
29+
}
30+
31+
// 右モータにpower値をセット
32+
void Controller::setRightMotorPower(const int power)
33+
{
34+
powerOfRightWheel = limitPowerValue(power);
35+
rightWheel.setPower(powerOfRightWheel);
36+
}
37+
38+
// 左モータにpower値をセット
39+
void Controller::setLeftMotorPower(const int power)
40+
{
41+
powerOfLeftWheel = limitPowerValue(power);
42+
leftWheel.setPower(powerOfLeftWheel);
43+
}
44+
45+
// 右モータのpower値をリセット
46+
void Controller::resetRightMotorPower()
47+
{
48+
powerOfRightWheel = 0;
49+
rightWheel.setPower(powerOfRightWheel);
50+
}
51+
52+
// 左モータのpower値をリセット
53+
void Controller::resetLeftMotorPower()
54+
{
55+
powerOfLeftWheel = 0;
56+
leftWheel.setPower(powerOfLeftWheel);
57+
}
58+
59+
// 右左両モータの状態をリセット
60+
void Controller::resetWheelsMotorPower()
61+
{
62+
powerOfRightWheel = 0;
63+
powerOfLeftWheel = 0;
64+
rightWheel.setPower(powerOfRightWheel);
65+
leftWheel.setPower(powerOfLeftWheel);
66+
}
67+
68+
// タイヤのモータを停止する
69+
void Controller::stopWheelsMotor()
70+
{
71+
powerOfRightWheel = 0;
72+
powerOfLeftWheel = 0;
73+
rightWheel.stop();
74+
leftWheel.stop();
75+
}
76+
77+
// アームのモータにpower値をセット
78+
void Controller::setArmMotorPower(const int power)
79+
{
80+
powerOfArm = limitPowerValue(power);
81+
armMotor.setPower(powerOfArm);
82+
}
83+
84+
// アームのモータのpower値をリセット
85+
void Controller::resetArmMotorPower()
86+
{
87+
powerOfArm = 0;
88+
armMotor.setPower(powerOfArm);
89+
}
90+
91+
// アームのモータを停止する
92+
void Controller::stopArmMotor()
93+
{
94+
powerOfArm = 0;
95+
armMotor.stop();
96+
}
97+
98+
// 右タイヤのpower値を取得する
99+
int Controller::getRightMotorPower()
100+
{
101+
return rightWheel.getPower();
102+
}
103+
104+
// 左タイヤのpower値を取得する
105+
int Controller::getLeftMotorPower()
106+
{
107+
return leftWheel.getPower();
108+
}

modules/API/Controller.h

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* @file Controller.h
3+
* @brief モーター制御に用いる関数をまとめたラッパークラス
4+
* @author nishijima515
5+
*/
6+
#ifndef CONTROLLER_H
7+
#define CONTROLLER_H
8+
9+
#include "Motor.h"
10+
11+
class Controller {
12+
public:
13+
/** Power値の上限 */
14+
static constexpr int MOTOR_POWER_MAX = 100;
15+
16+
/** Power値の下限 */
17+
static constexpr int MOTOR_POWER_MIN = -100;
18+
19+
/**
20+
* コンストラクタ
21+
*/
22+
Controller();
23+
24+
/**
25+
* @brief タイヤのモータにPower値をセット
26+
* @param power Power値
27+
*/
28+
void setRightMotorPower(const int power);
29+
void setLeftMotorPower(const int power);
30+
31+
/**
32+
* @brief タイヤのモータのpower値をリセット
33+
*/
34+
void resetRightMotorPower();
35+
void resetLeftMotorPower();
36+
void resetWheelsMotorPower();
37+
38+
/**
39+
* @brief タイヤのモータを停止する
40+
*/
41+
void stopWheelsMotor();
42+
43+
/**
44+
* @brief アームのモータにpower値をセット
45+
* @param power power値
46+
*/
47+
void setArmMotorPower(const int power);
48+
49+
/**
50+
* @brief アームのモータのpower値をリセット
51+
*/
52+
void resetArmMotorPower();
53+
54+
/**
55+
* @brief アームのモータを停止する
56+
*/
57+
void stopArmMotor();
58+
59+
/**
60+
* @brief 右タイヤのpower値を取得する
61+
* @return 右タイヤのpower値
62+
*/
63+
static int getRightMotorPower();
64+
65+
/**
66+
* @brief 左タイヤのpower値を取得する
67+
* @return 左タイヤのpower値
68+
*/
69+
static int getLeftMotorPower();
70+
71+
private:
72+
Motor rightWheel;
73+
Motor leftWheel;
74+
Motor armMotor;
75+
static int powerOfRightWheel; // 右タイヤpower
76+
static int powerOfLeftWheel; // 左タイヤpower
77+
static int powerOfArm; // アームpower
78+
79+
/**
80+
* @brief モータに設定するpower値の制限
81+
* @param inputpower 入力されたpower値
82+
* @return 制限されたpower値
83+
*/
84+
int limitPowerValue(const int inputPower);
85+
};
86+
87+
#endif

modules/API/Motor.h

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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

modules/API/Port.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @file Port.h
3+
* @brief ポート関連定義
4+
* @author HaruArima08
5+
*/
6+
7+
#ifndef PORT_H_
8+
#define PORT_H_
9+
10+
#include "spikeapi.h"
11+
#include "pbio/port.h"
12+
13+
/**
14+
* モータ/センサポート関連定義
15+
*/
16+
17+
enum class Port {
18+
PORT_A = PBIO_PORT_ID_A, /**< SPIKE ポートA */
19+
PORT_B = PBIO_PORT_ID_B, /**< SPIKE ポートB */
20+
PORT_C = PBIO_PORT_ID_C, /**< SPIKE ポートC */
21+
PORT_D = PBIO_PORT_ID_D, /**< SPIKE ポートD */
22+
PORT_E = PBIO_PORT_ID_E, /**< SPIKE ポートE */
23+
PORT_F = PBIO_PORT_ID_F /**< SPIKE ポートF */
24+
};
25+
26+
#endif

0 commit comments

Comments
 (0)