pas touché au main4, je modifie le main8 pour améliorer le main4

This commit is contained in:
Gaetan Bruyant 2025-05-11 17:29:31 +02:00
parent 3233ed9245
commit c56a1b66a6
5 changed files with 466 additions and 1 deletions

View File

@ -46,6 +46,8 @@
"streambuf": "cpp",
"thread": "cpp",
"typeinfo": "cpp",
"main3": "cpp"
"main3": "cpp",
"cstring": "cpp",
"iomanip": "cpp"
}
}

141
main5.cpp Normal file
View File

@ -0,0 +1,141 @@
#include <iostream>
#include <vector>
#include <chrono>
#include <thread>
#include <cstdlib> // rand()
#include <ctime> // time()
#include <unistd.h> // usleep()
// === Parameters ===
const int BPM_MIN = 50;
const int BPM_MAX = 120;
const int SAMPLE_INTERVAL_MS = 20; // Sampling every 100 ms
const int THRESHOLD = 550; // Threshold for heartbeat detection
const int NOISE_LEVEL = 100; // Simulated noise level
// === Initialize random generator ===
void initialiserRandom() {
srand(static_cast<unsigned int>(time(0)));
}
// === Simulate buzzer activation ===
void activerBuzzer() {
std::cout << "Buzzer active (ALERTE CARDIAQUE)" << std::endl;
}
// === Simulate vibration motor activation ===
void activerVibreur() {
std::cout << "Vibreur active (ALERTE CARDIAQUE)" << std::endl;
}
// === Simulate Bluetooth transmission ===
void envoyerMessageBluetooth(bool urgence) {
std::cout << "Message Bluetooth envoye: " << (urgence ? "TRUE (urgence)" : "FALSE (normal)") << std::endl;
}
// === Simulate heart signal from sensor with variable BPM and 0-BPM possibility ===
int simulerSignalCardiaque() {
static int t = 0;
static int nextPeakInterval = 0;
static int targetBPM = 75;
static int cyclesPerPeak = 800 / SAMPLE_INTERVAL_MS;
static bool enArretCardiaque = false;
t++;
if (t == 1 || t % nextPeakInterval == 0) {
if (rand() % 100 < 5) {
targetBPM = 0;
enArretCardiaque = true;
std::cout << "[Simulating cardiac arrest: BPM = 0]" << std::endl;
} else {
int variation = (rand() % 21) - 10;
targetBPM += variation;
if (targetBPM < 30) targetBPM = 30;
if (targetBPM > 210) targetBPM = 210;
enArretCardiaque = false;
std::cout << "[Target BPM = " << targetBPM << "]" << std::endl;
}
if (targetBPM == 0) {
nextPeakInterval = 10; // Keep short interval to simulate repeated 0 BPM checks
} else {
int interval_ms = 60000 / targetBPM;
cyclesPerPeak = interval_ms / SAMPLE_INTERVAL_MS;
nextPeakInterval = cyclesPerPeak;
}
return (targetBPM == 0) ? 300
: 700 + rand() % 50;
} else {
return 300;
}
}
// === Main program ===
int main() {
initialiserRandom();
bool battementDetecte = false;
auto dernierBattement = std::chrono::steady_clock::now();
auto dernierMessageArret = std::chrono::steady_clock::now();
bool enArret = false;
std::cout << "Demarrage de la simulation du capteur cardiaque..." << std::endl;
while (true) {
int signal = simulerSignalCardiaque();
auto maintenant = std::chrono::steady_clock::now();
// Heartbeat detected
if (signal > THRESHOLD && !battementDetecte) {
auto intervalle = std::chrono::duration_cast<std::chrono::milliseconds>(maintenant - dernierBattement).count();
if (intervalle > 300) {
int bpm = 60000 / intervalle;
std::cout << "BPM mesure : " << bpm;
bool anomalie = (bpm < BPM_MIN || bpm > BPM_MAX);
if (anomalie) {
std::cout << " [ANOMALIE]";
activerBuzzer();
activerVibreur();
envoyerMessageBluetooth(true);
} else {
envoyerMessageBluetooth(false);
}
enArret = false;
std::cout << std::endl;
dernierBattement = maintenant;
}
battementDetecte = true;
}
// Ready for next detection
if (signal < THRESHOLD) {
battementDetecte = false;
}
// Handle 0 BPM condition (no heartbeat detected for > 1 sec)
auto tempsDepuisDernierBattement = std::chrono::duration_cast<std::chrono::milliseconds>(maintenant - dernierBattement).count();
if (tempsDepuisDernierBattement > 1000) {
if (!enArret) {
std::cout << "Aucune activite cardiaque detectee." << std::endl;
enArret = true;
}
auto tempsDepuisDernierMessage = std::chrono::duration_cast<std::chrono::milliseconds>(maintenant - dernierMessageArret).count();
if (tempsDepuisDernierMessage >= 1000) {
envoyerMessageBluetooth(true); // Emergency
dernierMessageArret = maintenant;
}
}
usleep(SAMPLE_INTERVAL_MS * 1000);
}
return 0;
}

103
main6.cpp Normal file
View File

@ -0,0 +1,103 @@
#include <iostream>
#include <vector>
#include <random>
#include <thread>
#include <chrono>
#include <cmath>
#include <iomanip>
const int SAMPLE_RATE_MS = 20;
const int SAMPLES_PER_SECOND = 1000 / SAMPLE_RATE_MS;
const int TOTAL_SECONDS = 60;
std::default_random_engine rng(std::random_device{}());
std::uniform_real_distribution<double> changeDist(-0.25, 0.25);
std::uniform_real_distribution<double> driftDist(-0.05, 0.05);
std::uniform_real_distribution<double> heartAttackChance(0.0, 1.0);
std::uniform_int_distribution<int> startBpmDist(30, 210);
double clampBPM(double bpm) {
if (bpm == 0.0) return 0.0;
if (bpm < 30.0) return 30.0;
if (bpm > 210.0) return 210.0;
return bpm;
}
int main() {
std::vector<int> bpmHistory;
double bpm = startBpmDist(rng);
double drift = driftDist(rng);
int outOfRangeSeconds = 0;
bool emergency = false;
bool emergencyNotified = false;
bool cardiacArrestInjected = false;
std::vector<double> currentSecondSamples;
int secondCounter = 0;
std::cout << "Starting real-time heart rate monitoring...\n" << std::endl;
while (secondCounter < TOTAL_SECONDS) {
double sum = 0.0;
for (int i = 0; i < SAMPLES_PER_SECOND; ++i) {
// Inject cardiac arrest
if (!cardiacArrestInjected && secondCounter >= 20 && secondCounter < 40 && heartAttackChance(rng) < 0.005) {
bpm = 0.0;
cardiacArrestInjected = true;
} else if (bpm != 0.0) {
bpm += changeDist(rng) + drift;
bpm = clampBPM(bpm);
}
currentSecondSamples.push_back(bpm);
std::this_thread::sleep_for(std::chrono::milliseconds(SAMPLE_RATE_MS));
}
drift = driftDist(rng);
// Calculate 1-second average
double sumSecond = 0.0;
for (double val : currentSecondSamples) sumSecond += val;
int avgBPM = static_cast<int>(std::round(sumSecond / currentSecondSamples.size()));
bpmHistory.push_back(avgBPM);
currentSecondSamples.clear();
// Print real-time 1-second BPM
std::cout << "Second " << secondCounter + 1 << " - Calculated Avg BPM: " << avgBPM << std::endl;
// Emergency check
if (avgBPM < 40 || avgBPM > 200) {
outOfRangeSeconds++;
} else {
outOfRangeSeconds = 0;
}
if (!emergency && outOfRangeSeconds >= 3) {
emergency = true;
emergencyNotified = false;
}
if (emergency) {
if (!emergencyNotified) {
std::cout << "[buzzer active]" << std::endl;
std::cout << "[vibrator active]" << std::endl;
emergencyNotified = true;
}
std::cout << "[Bluetooth message sent: urgency = TRUE]" << std::endl;
} else if ((secondCounter + 1) % 10 == 0) {
int count = std::min(60, static_cast<int>(bpmHistory.size()));
int sumLast = 0;
for (int i = bpmHistory.size() - count; i < bpmHistory.size(); ++i) {
sumLast += bpmHistory[i];
}
int avgLast = sumLast / count;
std::cout << "[Bluetooth message sent: urgency = FALSE, avg BPM = " << avgLast << "]" << std::endl;
}
secondCounter++;
}
std::cout << "\nMonitoring session ended.\n";
return 0;
}

103
main7.cpp Normal file
View File

@ -0,0 +1,103 @@
#include <iostream>
#include <vector>
#include <random>
#include <thread>
#include <chrono>
#include <cmath>
#include <iomanip>
const int SAMPLE_RATE_MS = 20;
const int SAMPLES_PER_SECOND = 1000 / SAMPLE_RATE_MS;
const int TOTAL_SECONDS = 60;
std::default_random_engine rng(std::random_device{}());
std::uniform_real_distribution<double> changeDist(-0.25, 0.25);
std::uniform_real_distribution<double> driftDist(-0.05, 0.05);
std::uniform_real_distribution<double> heartAttackChance(0.0, 1.0);
std::uniform_int_distribution<int> startBpmDist(30, 210);
double clampBPM(double bpm) {
if (bpm == 0.0) return 0.0;
if (bpm < 30.0) return 30.0;
if (bpm > 210.0) return 210.0;
return bpm;
}
int main() {
std::vector<int> bpmHistory;
double bpm = startBpmDist(rng);
double drift = driftDist(rng);
int outOfRangeSeconds = 0;
bool emergency = false;
bool emergencyNotified = false;
bool cardiacArrestInjected = false;
std::vector<double> currentSecondSamples;
int secondCounter = 0;
std::cout << "Starting real-time heart rate monitoring...\n" << std::endl;
while (secondCounter < TOTAL_SECONDS) {
double sum = 0.0;
for (int i = 0; i < SAMPLES_PER_SECOND; ++i) {
// Inject cardiac arrest
if (!cardiacArrestInjected && secondCounter >= 20 && secondCounter < 40 && heartAttackChance(rng) < 0.005) {
bpm = 0.0;
cardiacArrestInjected = true;
} else if (bpm != 0.0) {
bpm += changeDist(rng) + drift;
bpm = clampBPM(bpm);
}
currentSecondSamples.push_back(bpm);
std::this_thread::sleep_for(std::chrono::milliseconds(SAMPLE_RATE_MS));
}
drift = driftDist(rng);
// Calculate 1-second average
double sumSecond = 0.0;
for (double val : currentSecondSamples) sumSecond += val;
int avgBPM = static_cast<int>(std::round(sumSecond / currentSecondSamples.size()));
bpmHistory.push_back(avgBPM);
currentSecondSamples.clear();
// Print real-time 1-second BPM
std::cout << "Second " << secondCounter + 1 << " - Calculated Avg BPM: " << avgBPM << std::endl;
// Emergency check
if (avgBPM < 40 || avgBPM > 200) {
outOfRangeSeconds++;
} else {
outOfRangeSeconds = 0;
}
if (!emergency && outOfRangeSeconds >= 3) {
emergency = true;
emergencyNotified = false;
}
if (emergency) {
if (!emergencyNotified) {
std::cout << "[buzzer active]" << std::endl;
std::cout << "[vibrator active]" << std::endl;
emergencyNotified = true;
}
std::cout << "[Bluetooth message sent: urgency = TRUE]" << std::endl;
} else if ((secondCounter + 1) % 10 == 0) {
int count = std::min(60, static_cast<int>(bpmHistory.size()));
int sumLast = 0;
for (int i = bpmHistory.size() - count; i < bpmHistory.size(); ++i) {
sumLast += bpmHistory[i];
}
int avgLast = sumLast / count;
std::cout << "[Bluetooth message sent: urgency = FALSE, avg BPM = " << avgLast << "]" << std::endl;
}
secondCounter++;
}
std::cout << "\nMonitoring session ended.\n";
return 0;
}

116
main8.cpp Normal file
View File

@ -0,0 +1,116 @@
#include <iostream>
#include <vector>
#include <chrono>
#include <cstdlib> // rand()
#include <ctime> // time()
#include <windows.h>
// === Parameters ===
//const int BPM_MIN = 50;
//const int BPM_MAX = 120;
const int SAMPLE_INTERVAL_MS = 1000; // Sampling every 10 ms
const int THRESHOLD_CARDIAC_ARREST = 2;
//const int NOISE_LEVEL = 100; // Simulated noise level
// === Initialize random generator ===// === Simulate heart signal from sensor ===
int generateRandomHeartRate() {
//int bpm = 30 + rand() % 111; // génère un BPM entre 30 et 140
int bpm = rand() % 141; // génère un BPM entre 0 et 140
return bpm;
}
// === Simulate buzzer activation ===
void activerBuzzer() {
std::cout << " Buzzer active (ALERTE CARDIAQUE)!" << std::endl;
}
// === Simulate vibration motor activation ===
void activerVibreur() {
std::cout << " Vibreur active (ALERTE CARDIAQUE)!" << std::endl;
}
// === Simulate Bluetooth transmission ===
void envoyerMessageBluetooth(bool urgence) {
std::cout << " Message Bluetooth envoye: " << (urgence ? "TRUE (urgence)" : "FALSE (normal)") << std::endl;
}
// send avg bvpm of the last minute
void sendAvgBPM(int avg){
std::cout << " Average BPM on the last minute sent by bluetooth : " << avg;
}
// === Main program ===
int main() {
bool battementDetecte = false;
auto dernierBattement = std::chrono::steady_clock::now();
std::cout << " Demarrage de la simulation du capteur cardiaque...\n";
int i=0;
int lastBPM;
int j = 0;
int time = 0;
int lastTime = 0;
std::vector<int> heartRateHistory;
heartRateHistory.reserve(1728000); //20*24*60*60 secondes de data (20 jours)
int avgBPM = 0;
while (true) {
//int signal = simulerSignalCardiaque();
//std::cout << "Signal: " << signal << std::endl; // verify the signal detected
// Détection de battement
if (!battementDetecte) { //signal > THRESHOLD &&
auto maintenant = std::chrono::steady_clock::now();
auto intervalle = std::chrono::duration_cast<std::chrono::milliseconds>(maintenant - dernierBattement).count();
int bpm = generateRandomHeartRate();
std::cout << " BPM mesure : " << bpm;
if (bpm < 55 && bpm > 40){
std::cout << " [BRADYCARDIA]" << std::endl;
activerVibreur();
} else if(bpm < 140 && bpm > 110) {
std::cout << " [TACHYCARDIA]" << std::endl;
activerVibreur();
}else if(bpm < 110 && bpm > 55){
std::cout << " [NORMAL]" << std::endl;
envoyerMessageBluetooth(false);
} else if (bpm < 40){
if (i>THRESHOLD_CARDIAC_ARREST && lastBPM < 40){
i=0;
std::cout << " [CARDIAC ARREST]" << std::endl;
activerBuzzer();
activerVibreur();
envoyerMessageBluetooth(true);
}else{
i+=1;
}
}
std::cout << std::endl;
dernierBattement = maintenant;
battementDetecte = true;
heartRateHistory.push_back(bpm);
lastBPM =bpm;
}else {
battementDetecte = false;// Prêt pour détecter le prochain battement
}
time++;
if (time>(lastTime - 60)){
for (j = lastTime; j<time; j++){
avgBPM = avgBPM + heartRateHistory[j];
}
avgBPM = avgBPM/j;
sendAvgBPM(avgBPM);
lastTime = time;
}
Sleep(SAMPLE_INTERVAL_MS); // expects milliseconds
}
return 0;
}