Skip to content

Commit e0e541d

Browse files
committed
Merge branch 'main' into ticket-KL25-20
1 parent 80f3908 commit e0e541d

File tree

10 files changed

+978
-4
lines changed

10 files changed

+978
-4
lines changed

CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ set(PROJECT_TEST_DIR ${PROJECT_SOURCE_DIR}/tests)
3333
set(INCLUDE_DIRS
3434
${PROJECT_MODULE_DIR}
3535
${PROJECT_MODULE_DIR}/calculators
36+
${PROJECT_MODULE_DIR}/API
37+
${PROJECT_TEST_DIR}
38+
${PROJECT_TEST_DIR}/dummy
3639
${PROJECT_TEST_DIR}/helpers
3740
)
3841

@@ -55,6 +58,11 @@ file(GLOB TEST_SOURCES "${PROJECT_TEST_DIR}/*Test.cpp")
5558

5659
# テスト実行可能ファイルの作成
5760
add_executable(${PROJECT_NAME}_test ${TEST_SOURCES})
61+
target_include_directories(${PROJECT_NAME}_test
62+
PRIVATE
63+
${PROJECT_TEST_DIR}/dummy # spikeapi ダミー用
64+
)
65+
5866
target_link_libraries(${PROJECT_NAME}_test
5967
PRIVATE
6068
${PROJECT_NAME}_impl
@@ -63,4 +71,4 @@ target_link_libraries(${PROJECT_NAME}_test
6371

6472
# テストの登録
6573
include(GoogleTest)
66-
gtest_discover_tests(${PROJECT_NAME}_test)
74+
gtest_discover_tests(${PROJECT_NAME}_test)

Makefile.inc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ USE_RASPIKE_ART=1
22

33
mkfile_path := $(dir $(lastword $(MAKEFILE_LIST)))
44

5-
65
# Shellスクリプトで、moduleディレクトリ中のソースコード名をオブジェクトファイル名に変換している
76
SRCS := $(shell find ${mkfile_path}modules -name '*.cpp')
87
OBJS := $(notdir $(SRCS:.cpp=.o))
@@ -12,9 +11,12 @@ SRCLANG := c++
1211

1312
APPL_LIBS += -lm
1413

15-
APPL_DIRS += $(mkfile_path)modules
16-
APPL_DIRS += $(mkfile_path)modules/calculators
14+
APPL_DIRS += \
15+
$(mkfile_path)modules\
16+
$(mkfile_path)modules/API\
17+
$(mkfile_path)modules/calculators
1718

1819
INCLUDES += \
1920
-I$(mkfile_path)modules\
21+
-I$(mkfile_path)modules/API\
2022
-I$(mkfile_path)modules/calculators

modules/API/Motor.h

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/**
2+
* @file Motor.h
3+
* @brief spikeapiのモータラッパークラス
4+
* @author nishijima515
5+
*/
6+
7+
#ifndef MOTOR_H
8+
#define MOTOR_H
9+
10+
#include "spike/pup/motor.h"
11+
#include "Port.h"
12+
13+
/**
14+
* SPIKE モータラッパークラス
15+
* モータの回転方向(時計回り、反時計回り)を指定するための列挙型
16+
* CLOCKWISEは時計回り、COUNTERCLOCKWISEは反時計回りを表す
17+
*/
18+
class Motor {
19+
public:
20+
enum class EDirection {
21+
CLOCKWISE = PUP_DIRECTION_CLOCKWISE,
22+
COUNTERCLOCKWISE = PUP_DIRECTION_COUNTERCLOCKWISE,
23+
};
24+
25+
/**
26+
* コンストラクタ
27+
* @param port PUPポートID
28+
* @param direction モータの回転方向
29+
* @param reset_count カウントをリセットするか
30+
* pup_motor_setup()を複数回呼ぶとハングするため、コンストラクタで一回だけ呼ぶことでエラーを回避する
31+
*/
32+
Motor(Port port, EDirection direction = EDirection::CLOCKWISE, bool reset_count = true)
33+
: mHasError(false)
34+
{
35+
pupDevicePointer = pup_motor_get_device(static_cast<pbio_port_id_t>(port));
36+
if(!pupDevicePointer) {
37+
mHasError = true;
38+
return;
39+
}
40+
pbio_error_t ret
41+
= pup_motor_setup(pupDevicePointer, static_cast<pup_direction_t>(direction), reset_count);
42+
if(ret != PBIO_SUCCESS) {
43+
mHasError = true;
44+
}
45+
}
46+
47+
/**
48+
* エンコーダをリセットする
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+
*/
68+
void setSpeed(int speed) const { pup_motor_set_speed(pupDevicePointer, speed); }
69+
70+
/**
71+
* モータのパワー値を取得する
72+
* @return パワー値(-100 ~ +100)
73+
*/
74+
int32_t getPower() const { return pup_motor_get_power(pupDevicePointer); }
75+
76+
/**
77+
* モータのパワー値を設定する
78+
* @param power モータのパワー値(-100 ~ +100)
79+
*/
80+
void setPower(int power) const { pup_motor_set_power(pupDevicePointer, power); }
81+
82+
/**
83+
* モータを止める
84+
*/
85+
void stop() const { pup_motor_stop(pupDevicePointer); }
86+
87+
/**
88+
* ブレーキをかけてモータを止める
89+
*/
90+
void brake() const { pup_motor_brake(pupDevicePointer); }
91+
92+
/**
93+
* モータを止めて角度を維持する
94+
*/
95+
void hold() const { pup_motor_hold(pupDevicePointer); }
96+
97+
/**
98+
* モータがストールしているか調べる
99+
* @return true ストールしている
100+
* @return false ストールしていない
101+
*/
102+
bool isStalled() const { return pup_motor_is_stalled(pupDevicePointer); }
103+
104+
/**
105+
* モータのデューティ値を下げる
106+
* @param duty_limit 新しいデューティ値(0-100)
107+
* @return 元の状態に戻すための最大電圧
108+
*/
109+
int32_t setDutyLimit(int duty_limit) const
110+
{
111+
return pup_motor_set_duty_limit(pupDevicePointer, duty_limit);
112+
}
113+
114+
/**
115+
* モータのデューティ値を元に戻す
116+
* @param old_value pup_motor_set_duty_limitの戻り値
117+
*/
118+
void restoreDutyLimit(int old_value) const
119+
{
120+
pup_motor_restore_duty_limit(pupDevicePointer, old_value);
121+
}
122+
123+
/**
124+
* インスタンス生成が正常にできたかどうかを確認するための共通メソッド
125+
* @return true インスタンス生成に失敗した
126+
* @return false インスタンス生成に成功した
127+
*/
128+
bool hasError() { return mHasError; }
129+
130+
private:
131+
pup_motor_t* pupDevicePointer; // SPIKE API のモーター構造体へのポインタ
132+
// フォーマットエラー解消のための文章です
133+
bool mHasError; // インスタンス生成に失敗したかどうかを示すフラグ
134+
135+
}; // class Motor
136+
137+
#endif

modules/API/MotorController.cpp

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/**
2+
* @file MotorController.cpp
3+
* @brief モータ制御に用いる関数をまとめたラッパークラス
4+
* @author nishijima515
5+
*/
6+
#include "MotorController.h"
7+
8+
MotorController::MotorController()
9+
: rightWheel(Port::PORT_A),
10+
leftWheel(Port::PORT_B, Motor::EDirection::COUNTERCLOCKWISE),
11+
armMotor(Port::PORT_C, Motor::EDirection::COUNTERCLOCKWISE)
12+
{
13+
}
14+
15+
// モータに設定するpower値の制限
16+
int MotorController::limitPowerValue(int inputPower)
17+
{
18+
if(inputPower > MOTOR_POWER_MAX) {
19+
return MOTOR_POWER_MAX;
20+
} else if(inputPower < MOTOR_POWER_MIN) {
21+
return MOTOR_POWER_MIN;
22+
}
23+
return inputPower;
24+
}
25+
26+
// 右モータにpower値をセット
27+
void MotorController::setRightMotorPower(int power)
28+
{
29+
rightWheel.setPower(limitPowerValue(power));
30+
}
31+
32+
// 左モータにpower値をセット
33+
void MotorController::setLeftMotorPower(int power)
34+
{
35+
leftWheel.setPower(limitPowerValue(power));
36+
}
37+
38+
// 右モータのpower値をリセット
39+
void MotorController::resetRightMotorPower()
40+
{
41+
rightWheel.setPower(0);
42+
}
43+
44+
// 左モータのpower値をリセット
45+
void MotorController::resetLeftMotorPower()
46+
{
47+
leftWheel.setPower(0);
48+
}
49+
50+
// 右左両モータの状態をリセット
51+
void MotorController::resetWheelsMotorPower()
52+
{
53+
rightWheel.setPower(0);
54+
leftWheel.setPower(0);
55+
}
56+
57+
// 右タイヤのモータに回転速度をセット
58+
void MotorController::setRightMotorSpeed(int speed)
59+
{
60+
rightWheel.setSpeed(speed);
61+
}
62+
63+
// 左タイヤのモータに回転速度をセット
64+
void MotorController::setLeftMotorSpeed(int speed)
65+
{
66+
leftWheel.setSpeed(speed);
67+
}
68+
69+
// 両タイヤのモータを停止する
70+
void MotorController::stopWheelsMotor()
71+
{
72+
rightWheel.stop();
73+
leftWheel.stop();
74+
}
75+
76+
// ブレーキをかけてタイヤのモータを停止する
77+
void MotorController::brakeWheelsMotor()
78+
{
79+
rightWheel.brake();
80+
leftWheel.brake();
81+
}
82+
83+
// アームのモータにpower値をセット
84+
void MotorController::setArmMotorPower(int power)
85+
{
86+
armMotor.setPower(limitPowerValue(power));
87+
}
88+
89+
// アームのモータのpower値をリセット
90+
void MotorController::resetArmMotorPower()
91+
{
92+
armMotor.setPower(0);
93+
}
94+
95+
// アームのモータを停止する
96+
void MotorController::stopArmMotor()
97+
{
98+
armMotor.stop();
99+
}
100+
101+
// アームモータを止めて角度を維持する
102+
void MotorController::holdArmMotor()
103+
{
104+
armMotor.hold();
105+
}
106+
107+
// 右タイヤのpower値を取得する
108+
int MotorController::getRightMotorPower()
109+
{
110+
return rightWheel.getPower();
111+
}
112+
113+
// 左タイヤのpower値を取得する
114+
int MotorController::getLeftMotorPower()
115+
{
116+
return leftWheel.getPower();
117+
}
118+
119+
// アームモータのpower値を取得する
120+
int MotorController::getArmMotorPower()
121+
{
122+
return armMotor.getPower();
123+
}
124+
125+
// 右モータの角位置を取得する
126+
int32_t MotorController::getRightMotorCount()
127+
{
128+
return rightWheel.getCount();
129+
}
130+
131+
// 左モータの角位置を取得する
132+
int32_t MotorController::getLeftMotorCount()
133+
{
134+
return leftWheel.getCount();
135+
}
136+
137+
// アームモータの角位置を取得する
138+
int32_t MotorController::getArmMotorCount()
139+
{
140+
return armMotor.getCount();
141+
}
142+
143+
// 右タイヤモータの回転速度を取得する
144+
int32_t MotorController::getRightMotorSpeed()
145+
{
146+
return rightWheel.getSpeed();
147+
}
148+
149+
// 左タイヤモータの回転速度を取得する
150+
int32_t MotorController::getLeftMotorSpeed()
151+
{
152+
return leftWheel.getSpeed();
153+
}

0 commit comments

Comments
 (0)