问题描述:

I am working on a quadcopter project with Beaglebone.

I need help with using pwm on Beaglebone through a C program.

I have attached the following code,

#include <stdio.h>

#include <string.h>

#include <unistd.h>

struct pwm

{

char period[100];

char duty[100];

char polarity[100];

char run[100];

}pwm1,pwm2,pwm3,pwm4;

char pwm_1[]="P9_21";

char pwm_2[]="P9_14";

char pwm_3[]="P8_13";

char pwm_4[]="P9_42";

int initialize(struct pwm &pwmi, char pwm_i[])

{

sprintf(path,"echo \"bone_pwm_%s\" >> /sys/devices/bone_capemgr.9/slots",pwm_i);

fp = popen(path,"r");

fflush(fp);

usleep(1000);

sprintf(path,"ls /sys/devices/ocp.3/pwm_test_%s.*/period",pwm_i);

fp = popen(path,"r");

while(fgets(path,100,fp)!=NULL)

strcpy(pwmi.period,path);

fflush(fp);

sprintf(path,"ls /sys/devices/ocp.3/pwm_test_%s.*/duty",pwm_i);

fp = popen(path,"r");

while(fgets(path,100,fp)!=NULL)

strcpy(pwmi.duty,path);

fflush(fp);

sprintf(path,"ls /sys/devices/ocp.3/pwm_test_%s.*/polarity",pwm_i);

fp = popen(path,"r");

while(fgets(path,100,fp)!=NULL)

strcpy(pwmi.polarity,path);

fflush(fp);

sprintf(path,"ls /sys/devices/ocp.3/pwm_test_%s.*/run",pwm_i);

fp = popen(path,"r");

while(fgets(path,100,fp)!=NULL)

strcpy(pwmi.run,path);

fflush(fp);

pclose(fp);

return 0;

printf("%s%s%s%s",pwmi.period,pwmi.duty,pwmi.polarity,pwmi.run)

}

int pwmperiod(struct pwm &pwmi, unsigned int period)

{

sprintf(path,"echo %d > %s", period, pwm.period);

fp = popen(path,"r");

usleep(1000);

pclose(fp);

return 0;

}

int main()

{

unsigned int period = 200000;

initialize(pwm1,pwm_1);

initialize(pwm2,pwm_2);

initialize(pwm3,pwm_3);

initialize(pwm4,pwm_4);

pwmperiod(pwm1,period);

return 0;

}

Now the above code works perfectly fine. But I want to use the pwmperiod() function a little differently. Instead of using popen() all the way, I want to use fopen() and fprintf() for the function pwmperiod() . Something like this,

int pwmperiod(struct pwm &pwmi, unsigned int period)

{

fp = fopen(pwmi.period,"r+");

fseek(fp,0,SEEK_SET);

fprintf(fp,"%d",period);

fclose(fp);

return 0;

}

I tried the modified code but when it attempts to write the period value, it outputs "segmentation fault".

I realized that fopen() takes a const char while pwmi.period is just char. Another problem popen() and sprint() are not compatible with const char.

So is there a way to resolve the conversion?

Also how often is popen() used in C/C++ programs?

PS:

Not an expert coder and I am not from a computer science background. I am learning progressively.

Again, the code works with popen() perfectly. But then I am comfortable with file handling in C. So I would prefer personally fopen() over popen() . Moreover I feel it would be pointless to use popen() in C. Might as well use a shell script for pwm.

网友答案:

Leaving aside for a moment the issue of char* vs. const char* (since a char* can anyway be passed to any function taking a const char*).

Have you checked that the return value from fopen is non-NULL?

Note that the file must exist when fopen is called with r+. If it doesn't, fopen will return NULL, generating a segfault.

Since the file is only being written, not read, consider using

fp = fopen(pwmi.period,"w");

which will create a new file if one doesn't already exist.

相关阅读:
Top