Skip to content

Commit

Permalink
uplode: newとdeleteを使用してインスタンスを再生成するように変更
Browse files Browse the repository at this point in the history
  • Loading branch information
CHIHAYATAKU committed Nov 4, 2024
1 parent fee85c2 commit 4977956
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
33 changes: 21 additions & 12 deletions module/Calibrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,18 @@ void Calibrator::selectAndSetCourse()
char buf[SMALL_BUF_SIZE]; // log用にメッセージを一時保持する領域
Logger logger;
bool _isLeftCourse = true;
Measurer* measurer = new Measurer();

logger.log("Select a Course");
logger.log(">> Set Left Course");
// 右ボタンが押されたら確定する
while(!measurer.getRightButton()) {
if(measurer.getLeftButton() && !_isLeftCourse) {
while(!measurer->getRightButton()) {
if(measurer->getLeftButton() && !_isLeftCourse) {
// 左ボタンが押されたときRコースがセットされていれば、Lコースをセットする
_isLeftCourse = true;
logger.log(">> Set Left Course");
timer.sleep(500); // 500ミリ秒スリープ
} else if(measurer.getLeftButton() && _isLeftCourse) {
} else if(measurer->getLeftButton() && _isLeftCourse) {
// 左ボタンが押されたときLコースがセットされていれば、Rコースをセットする
_isLeftCourse = false;
logger.log(">> Set Right Course");
Expand All @@ -54,6 +55,7 @@ void Calibrator::selectAndSetCourse()
logger.logHighlight(buf);

timer.sleep(1000); // 1秒スリープ
delete measurer;
}

void Calibrator::measureAndSetTargetBrightness()
Expand All @@ -63,19 +65,20 @@ void Calibrator::measureAndSetTargetBrightness()
int whiteBrightness = -1;
int blackBrightness = -1;
targetBrightness = -1;
Measurer* measurer = new Measurer();

// 黒線上で左ボタンを押して黒の輝度を取得し、右ボタンで決定する
logger.log("Press the Left Button on the Black");

//
// 左ボタンで輝度を取得し、右ボタンで黒の輝度を決定する
while(blackBrightness < 0 || !measurer.getRightButton()) {
while(blackBrightness < 0 || !measurer->getRightButton()) {
// 左ボタンが押されるまで待機
while(blackBrightness < 0 && !measurer.getLeftButton()) {
while(blackBrightness < 0 && !measurer->getLeftButton()) {
timer.sleep(); // 10ミリ秒スリープ
}
// 輝度取得
blackBrightness = measurer.getBrightness();
blackBrightness = measurer->getBrightness();
snprintf(buf, SMALL_BUF_SIZE, ">> Black Brightness Value is %d", blackBrightness);
logger.log(buf);
timer.sleep(); // 10ミリ秒スリープ
Expand All @@ -86,13 +89,13 @@ void Calibrator::measureAndSetTargetBrightness()

//
// 左ボタンで輝度を取得し、右ボタンで白の輝度を決定する
while(whiteBrightness < 0 || !measurer.getRightButton()) {
while(whiteBrightness < 0 || !measurer->getRightButton()) {
// 左ボタンが押されるまで待機
while(whiteBrightness < 0 && !measurer.getLeftButton()) {
while(whiteBrightness < 0 && !measurer->getLeftButton()) {
timer.sleep(); // 10ミリ秒スリープ
}
// 輝度取得
whiteBrightness = measurer.getBrightness();
whiteBrightness = measurer->getBrightness();
snprintf(buf, SMALL_BUF_SIZE, ">> White Brightness Value is %d", whiteBrightness);
logger.log(buf);
timer.sleep(); // 10ミリ秒スリープ
Expand All @@ -101,32 +104,38 @@ void Calibrator::measureAndSetTargetBrightness()
targetBrightness = (whiteBrightness + blackBrightness) / 2;
snprintf(buf, SMALL_BUF_SIZE, ">> Target Brightness Value is %d", targetBrightness);
logger.log(buf);

delete measurer;
}

bool Calibrator::waitForStart()
{
char buf[SMALL_BUF_SIZE]; // log用にメッセージを一時保持する領域
Logger logger;
Measurer measurer; // measurerの再インスタンス化
constexpr int startDistance = 5; // 手などでスタート合図を出す距離[cm]
int count = 0;

// Measurerの再インスタンス化
Measurer* measurer = new Measurer();

logger.log("On standby.\n");
snprintf(buf, SMALL_BUF_SIZE, "On standby.\n\nSignal within %dcm from Sonar Sensor.",
startDistance);
logger.log(buf);

// startDistance以内の距離に物体がない間待機する
while(measurer.getForwardDistance() > startDistance && !measurer.getLeftButton() && count < 600) {
while(measurer->getForwardDistance() > startDistance && !measurer->getLeftButton() && count < 600) {
timer.sleep(100); // 100ミリ秒スリープ
count++;
// 確認用(コマンドラインにセンサの取得値を出力)
if(count % 50 == 0) {
snprintf(buf, SMALL_BUF_SIZE, "count: %d\nbrightness: %d\nforwardDistance: %d",
count, measurer.getBrightness(), measurer.getForwardDistance());
count, measurer->getBrightness(), measurer->getForwardDistance());
logger.log(buf);
}
}

delete measurer;
return count < 600;
}

Expand Down
1 change: 0 additions & 1 deletion module/Calibrator.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class Calibrator {
bool isLeftCourse; // true:Lコース, false: Rコース
int targetBrightness; // 目標輝度
Timer timer;
Measurer measurer;

/**
* @brief 左右ボタンでLRコースを選択してisLeftCourseをセットする
Expand Down

0 comments on commit 4977956

Please sign in to comment.