XJターボのタコメーター修理、牛歩の如く進めています(汗)
基板作成&プログラム作成が終わり、机上テストまで完了しました!
残すは実車テスト、ノイズの影響がない事を願います。
今回プログラムの作成には、巷で流行りのChatGPTを使ってみました。
初歩的なミスを含む事も多くありデバック処置が手間でしたが、
それでも超絶的な時間短縮になり、とても助かりました。AIの力はすごい!!
参考にされる方は少ないと思いますが、作成したスケッチを載せておきます。
#include "SwitecX25.h"
//ピンの定義
const int inputPin = 2;
//エンジンパラメーター
const int pulsesPerRevolution = 1; //エンジン1回転あたりのパルス数設定
const int maxRPM = 12000; //エンジンの最大回転数
const int minRPM = 1000; //エンジンの最小回転数
//ステッピングモーター
SwitecX25 motor(315 * 3, 3, 4, 5, 6);
//変数
volatile unsigned long lastInterruptTime = 0; //割り込み時間
volatile unsigned long BeforeInterruptTime = 0; //前回の割り込み時間
unsigned long TIME_OUT = 1000000; // 入力信号が1秒以上ない場合のタイムアウト
volatile int pulseCount = 0; // 入力信号のカウント
int SET_STEP = 0; // ENG_RPMをもとに計算される値
void setup() {
pinMode(inputPin, INPUT); //入力ピン
attachInterrupt(digitalPinToInterrupt(inputPin), rpmInterrupt, RISING);
//ステッピングモーターの0点補正
motor.zero();
//ステッピングモーターのオープニング演出
motor.setPosition(563);
motor.updateBlocking();
delay(300);
motor.setPosition(0);
motor.updateBlocking();
delay(300);
}
void loop() {
//エンジンが停止しているかどうかを判断する
if (micros() - lastInterruptTime > TIME_OUT) { //前回の割り込みから1秒以上経過したらエンジン停止
SET_STEP = 0;
}
if (pulseCount >= 1) {
//エンジンの回転数を計算する
float rpm = 60000000.0 / ((lastInterruptTime - BeforeInterruptTime) * pulsesPerRevolution); //回転数(rpm)
rpm = min(max(rpm, minRPM), maxRPM); //回転数の範囲を最小値から最大値までに制限する
//ステップ数を計算する
SET_STEP = (rpm - 1000) * 0.049 + 24;
pulseCount = 0;
}
motor.setPosition(SET_STEP);
motor.updateBlocking();
}
//外部割り込み
void rpmInterrupt() {
BeforeInterruptTime = lastInterruptTime;
lastInterruptTime = micros(); //割り込み時間を更新
pulseCount++;
}
Posted at 2023/03/16 22:34:59 | |
トラックバック(0) |
工作 | 日記