C로 구현한 온/오프 제어 (Bang-Bang Control) – 모터제어방법

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> // For usleep

// Function to simulate ON/OFF control
void on_off_control(int target_value, int hysteresis, int duration_ms, int update_interval_ms) {
    int current_value = 0; // Simulated sensor value
    int system_state = 0; // 0: OFF, 1: ON

    printf("Starting ON/OFF Control:\n");
    printf("Target Value: %d\n", target_value);
    printf("Hysteresis: +/- %d\n", hysteresis);

    int cycles = duration_ms / update_interval_ms;

    for (int i = 0; i < cycles; i++) {
        // Simulate reading the current value (e.g., from a sensor)
        current_value = rand() % (target_value + hysteresis * 2); // Random value around the target

        // Control logic with hysteresis
        if (system_state == 0 && current_value < (target_value - hysteresis)) {
            system_state = 1; // Turn ON
            printf("Cycle %d: Current Value = %d, State = ON\n", i, current_value);
        } else if (system_state == 1 && current_value > (target_value + hysteresis)) {
            system_state = 0; // Turn OFF
            printf("Cycle %d: Current Value = %d, State = OFF\n", i, current_value);
        } else {
            printf("Cycle %d: Current Value = %d, State = %s\n", i, current_value, system_state ? "ON" : "OFF");
        }

        usleep(update_interval_ms * 1000); // Wait for the next update
    }

    printf("ON/OFF Control Finished.\n");
}

int main() {
    int target_value, hysteresis, duration_ms, update_interval_ms;

    // Get user input
    printf("Enter target value: ");
    scanf("%d", &target_value);

    printf("Enter hysteresis (+/-): ");
    scanf("%d", &hysteresis);

    printf("Enter duration (ms): ");
    scanf("%d", &duration_ms);

    printf("Enter update interval (ms): ");
    scanf("%d", &update_interval_ms);

    // Run the ON/OFF control function
    on_off_control(target_value, hysteresis, duration_ms, update_interval_ms);

    return 0;
}

위 코드는 **온/오프 제어(On/Off Control)**를 구현한 C 프로그램입니다.


주요 구성 요소:

  1. 입력 매개변수:
    • target_value: 제어 목표 값.
    • hysteresis: 히스테리시스(상하 한계값).
    • duration_ms: 제어 실행 총 시간(ms).
    • update_interval_ms: 상태 업데이트 간격(ms).
  2. 제어 논리:
    • current_value(target_value - hysteresis)보다 작으면 시스템을 ON으로 설정.
    • current_value(target_value + hysteresis)보다 크면 시스템을 OFF로 설정.
    • 히스테리시스 범위 내에서는 현재 상태 유지.
  3. 출력:
    • 각 사이클에서 센서 값(current_value)와 시스템 상태(ON 또는 OFF)를 출력.
  4. 시뮬레이션:
    • rand() 함수로 센서 값을 무작위로 생성하여 시스템 상태 변화를 시뮬레이션.

실행 예제:

입력:

Enter target value: 50
Enter hysteresis (+/-): 5
Enter duration (ms): 1000
Enter update interval (ms): 100

출력:

Starting ON/OFF Control:
Target Value: 50
Hysteresis: +/- 5
Cycle 0: Current Value = 45, State = ON
Cycle 1: Current Value = 52, State = OFF
Cycle 2: Current Value = 48, State = ON
...
ON/OFF Control Finished.

응용:

  • 온도 제어: 히터/냉각기의 단순 온도 유지.
  • 수위 제어: 펌프의 물 공급 ON/OFF.
  • 조명 제어: 특정 밝기 수준에서 조명을 켜거나 끄는 시스템.

이 코드는 간단하지만 효과적인 제어 방식을 제공합니다. 추가적인 최적화나 센서 입력을 실제 하드웨어로 확장할 수 있습니다.