Servo motor doesn't respond

49 views Asked by At

I'm trying to "steer" my servo by using pwm. To make my code better, I refactor the FILE type out of the function so that the file is not being opened and closed with every call I make. But this refactor has gone wrong. It doesn't throw an exception or something....

#include "pwm_control.h"

#include <stdio.h>
#include <stdlib.h>

static FILE *pwm_file;

void export_pwm(void) {
    FILE *file;
    file = fopen(PWM_PATH "export", "w");
    if (file == NULL) {
        perror("Error opening enable file");
        exit(EXIT_FAILURE);
    }
    fprintf(file, "0");
    fclose(file);
}

void enable_pwm(void) {
    FILE *file;
    file = fopen(PWM_PATH "pwm0/enable", "w");
    if (file == NULL) {
        perror("Error opening enable file");
        exit(EXIT_FAILURE);
    }
    fprintf(file, "1");
    fclose(file);
}

void disable_pwm(void) {
    FILE *file;
    file = fopen(PWM_PATH "pwm0/enable", "w");
    if (file == NULL) {
        perror("Error opening enable file");
        exit(EXIT_FAILURE);
    }
    fprintf(file, "0");
    fclose(file);
}

void init_duty_cycle_file(void) {
    pwm_file = fopen(PWM_PATH "pwm0/duty_cycle", "w");
}

void close_duty_cyle_file(void) {
    fclose(pwm_file);
}

void set_pwm_duty_cycle(int duty_cycle) {
    if (pwm_file == NULL) {
        perror("Error opening duty_cycle file");
        exit(EXIT_FAILURE);
    }
    fprintf(pwm_file, "%d", duty_cycle * 10000);
}

void set_pwm_period() {
    FILE *file;
    file = fopen(PWM_PATH "pwm0/period", "w");
    if (file == NULL) {
        perror("Error opening period file");
        exit(EXIT_FAILURE);
    }
    fprintf(file, "%d", PERIOD);
    fclose(file);
}
0

There are 0 answers