I'm currently working on a project with a BeagleBone Black Wireless and in my project i have to control some servomotors among other things.
I've started testing the servos by using the adafruit library on python with the following code that i've found on the internet and everything worked just fine:
Code: Select all
import Adafruit_BBIO.PWM as PWM
servo_pin = "P9_14"
duty_min = 3
duty_max = 14.5
duty_span = duty_max - duty_min
PWM.start(servo_pin, (100-duty_min),60.0)
while True:
angle = raw_input("Angle (0 to 180 x to exit):")
if angle == 'x':
PWM.stop(servo_pin)
PWM.cleanup()
break
angle_f = float(angle)
duty = 100 - ((angle_f / 180) * duty_span + duty_min)
print "Duty is %f"%duty
PWM.set_duty_cycle(servo_pin, duty)
( "c_pinmux.c", "c_pwm.c" , "common.c" with relative .h files ) then i've wrote a test.c file similar to the python script, code as follows:
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include "c_pwm.h"
#include "common.h"
#include "c_pinmux.h"
#include <string.h>
int main(int argc, char**argv){
const char * pwm = "P9_14";
char key[7];
float duty_min = 3.0;
float duty_max = 14.5;
float duty_span = duty_max - duty_min;
float frequency = 2000.0;
int polarity = 0;
float angle_f;
float duty;
char buffer[15];
int err;
err = get_pwm_key(pwm, key);
if (err != BBIO_OK) {
fprintf(stderr, "%s\n", "Invalid PWM key or name.");
return -1;
}
err = pwm_start(key, (100.0 - duty_min), frequency, polarity);
if(err<1){
fprintf(stderr, "%s\n", "Error in pwm start.");
return -1;
}
while(1){
fprintf(stdout, "%s\n", "Select duty angle: ");
scanf("%s", buffer);
while ((getchar()) != '\n');
angle_f = strtof(buffer, NULL);
fprintf(stdout, "Buffer is %s and length is %ld, angle is %f\n", buffer, strlen(buffer), angle_f);
if(strcmp(buffer,"x") == 0){
fprintf(stdout, "%s\n", "Shutting down." );
err = pwm_disable(key);
if(err < 1){
fprintf(stderr, "%s\n", "Error in pwm stop.");
return -1;
}
pwm_cleanup();
return 0;
}
duty = 100 - ((angle_f / 180) * duty_span + duty_min);
printf("Duty is %f\n", duty);
err= pwm_set_duty_cycle(key,duty);
if(err < 1){
fprintf(stderr, "%s\n", "Error in setting duty cycle.");
return -1;
}
}
}
Code: Select all
CC=gcc
INCDIR =./includes
CFLAGS= -c -g -Wall -I$(INCDIR)
#
# Link
#
all : bin/test
obj/test.o: test.c
$(CC) $(CFLAGS) $< -o $@
obj/common.o: common.c
$(CC) $(CFLAGS) $< -o $@
obj/c_pwm.o: c_pwm.c
$(CC) $(CFLAGS) $< -o $@
obj/c_pinmux.o: c_pinmux.c
$(CC) $(CFLAGS) $< -o $@
bin/test: obj/test.o obj/common.o obj/c_pwm.o obj/c_pinmux.o
$(CC) -o $@ $^
clean :
rm -f bin/test
rm -f obj/*
/bin/rm -rf *.dSYM
But when i run the executable
nothing happens. So i look inbin/test
to see what happens behind the scenes and it seems that the function/var/log/syslog
fails to find the pin "P9_14". By looking at the source code of the library i understand that the pin that i want to use must be exported with functionpwm_start(pin,duty,freq,pol)
that normally is called by the functionexport_pwm(pin)
which is called bypwm_setup(pin,duty,freq,pol)
^^' .pwm_start(pin,duty,freq,pol)
It seems that
doesn't find the pin in its multiple calls (by pwm_start, pwm_set_duty_cycles ecc. ) since the structure where the pins should be exported to seems to be always empty.lookup_exported_pwm
So i really i don't understand what i'm missing here, i think that i'm compiling correctly the project and that the dependencies are respected however i have the impression that i miss a function call somewhere in the main of the "test.c" file.
Code: Select all
/var/log/syslog dump
Jan 20 17:05:57 beaglebone test: Adafruit_BBIO: pwm_start: P9_14, 97.000000, 2000.000000, 0
Jan 20 17:05:57 beaglebone test: Adafruit_BBIO: lookup_exported_pwm: couldn't find 'P9_14'
Jan 20 17:05:57 beaglebone test: Adafruit_BBIO: uboot_overlay_enabled() is true
Jan 20 17:05:57 beaglebone test: Adafruit_BBIO: initialize_pwm: OK
Jan 20 17:05:57 beaglebone test: Adafruit_BBIO: uboot_overlay_enabled() is true
Jan 20 17:05:57 beaglebone test: Adafruit_BBIO: pwm_start: P9_14 pwm setup failed: 6
Jan 20 17:05:59 beaglebone test: Adafruit_BBIO: lookup_exported_pwm: couldn't find 'P9_14'
Jan 20 17:05:59 beaglebone test: Adafruit_BBIO: pwm_set_duty_cycle: P9_14 couldn't find key
Jan 20 17:06:02 beaglebone test: Adafruit_BBIO: lookup_exported_pwm: couldn't find 'P9_14'
Jan 20 17:06:02 beaglebone test: Adafruit_BBIO: pwm_set_duty_cycle: P9_14 couldn't find key
I'm available if you need more details or dumps of the execution of the code.
Thank you for your attention,
Best regards!