Skip to content

Commit a453c78

Browse files
committed
Merge branch 'main' into ticket-KL25-19
2 parents 319e837 + 44e1116 commit a453c78

File tree

13 files changed

+1487
-5
lines changed

13 files changed

+1487
-5
lines changed

CMakeLists.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ set(PROJECT_TEST_DIR ${PROJECT_SOURCE_DIR}/tests)
3636
set(INCLUDE_DIRS
3737
${PROJECT_MODULE_DIR}
3838
${PROJECT_MODULE_DIR}/CameraCapture
39+
${PROJECT_MODULE_DIR}/calculators
40+
${PROJECT_MODULE_DIR}/API
41+
${PROJECT_TEST_DIR}
42+
${PROJECT_TEST_DIR}/dummy
3943
${PROJECT_TEST_DIR}/helpers
4044
/usr/include/opencv4
4145
)
@@ -44,6 +48,7 @@ set(INCLUDE_DIRS
4448
file(GLOB_RECURSE PROJECT_SOURCES
4549
"${PROJECT_MODULE_DIR}/*.cpp"
4650
"${PROJECT_MODULE_DIR}/CameraCapture/*.cpp"
51+
"${PROJECT_MODULE_DIR}/calculators/*.cpp"
4752
"${PROJECT_TEST_DIR}/helpers/*.cpp"
4853
)
4954

@@ -61,6 +66,11 @@ file(GLOB TEST_SOURCES "${PROJECT_TEST_DIR}/*Test.cpp")
6166

6267
# テスト実行可能ファイルの作成
6368
add_executable(${PROJECT_NAME}_test ${TEST_SOURCES})
69+
target_include_directories(${PROJECT_NAME}_test
70+
PRIVATE
71+
${PROJECT_TEST_DIR}/dummy # spikeapi ダミー用
72+
)
73+
6474
target_link_libraries(${PROJECT_NAME}_test
6575
PRIVATE
6676
${PROJECT_NAME}_impl
@@ -70,4 +80,4 @@ target_link_libraries(${PROJECT_NAME}_test
7080

7181
# テストの登録
7282
include(GoogleTest)
73-
gtest_discover_tests(${PROJECT_NAME}_test)
83+
gtest_discover_tests(${PROJECT_NAME}_test)

Makefile.inc

Lines changed: 7 additions & 4 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))
@@ -13,15 +12,19 @@ SRCLANG := c++
1312
APPL_LIBS += -lm
1413

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

1920
INCLUDES += \
20-
-I$(mkfile_path)modules \
21+
-I$(mkfile_path)modules\
22+
-I$(mkfile_path)modules/API\
23+
-I$(mkfile_path)modules/calculators\
2124
-I$(mkfile_path)modules/CameraCapture \
2225
-I/usr/include/opencv4
2326

2427
APPL_LIBS += $(shell pkg-config --libs opencv4)
2528

2629
CXXFLAGS += -std=c++11 -pthread
27-
LDFLAGS += -pthread
30+
LDFLAGS += -pthread

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)