diff --git a/app/raspberry_pi/repository/packages/p/pca9685/xmake.lua b/app/raspberry_pi/repository/packages/p/pca9685/xmake.lua index 09305bf0..d38a7f07 100644 --- a/app/raspberry_pi/repository/packages/p/pca9685/xmake.lua +++ b/app/raspberry_pi/repository/packages/p/pca9685/xmake.lua @@ -7,7 +7,7 @@ package("pca9685") add_versions("2017.12.07", "6f9794d888f77b863884c3eac933b75a07101347") - on_install("linux", "macosx", function (package) + on_install("linux", function (package) io.writefile("xmake.lua", [[ add_rules("mode.debug", "mode.release") target("pca9685") diff --git a/app/raspberry_pi/tests/pca9685/test_front_wheels.cpp b/app/raspberry_pi/tests/pca9685/test_front_wheels.cpp new file mode 100644 index 00000000..d7aa85d6 --- /dev/null +++ b/app/raspberry_pi/tests/pca9685/test_front_wheels.cpp @@ -0,0 +1,90 @@ +// Includes for PCA9685 +#include "PCA9685.h" +#include +#include +//#include +#include // std::thread +#include // std::vector +#include +#include + + +#include + +#define MIN_PULSE_WIDTH 900 +#define MAX_PULSE_WIDTH 2100 +#define FREQUENCY 50 + +using namespace std; + +int offset = 0; + +//motor channels +int chanh = 0; +int chanv = 1; + +//Declaration of Functions used ================================== +int setAngle(int& angle, PCA9685 pwm, int& channel); + + +//def map(self, x, in_min, in_max, out_min, out_max): +int map (int x, int in_min, int in_max, int out_min, int out_max) { + return ((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min); +} + +//def _angle_to_analog(self, angle): +int setAngleToAnalog(int angle) { + float pulse_wide; + int analog_value; + + pulse_wide = map(angle,0,180,MIN_PULSE_WIDTH,MAX_PULSE_WIDTH); + analog_value = int(float(pulse_wide) / 1000000 * FREQUENCY * 4096); + return (analog_value); +} + +int setAngle(int& angle, PCA9685 pwm, int& channel) { + int val = 0; + + if (angle > 180) { + angle = 179; + } + if (angle < 0) { + angle = 1; + } + + val = setAngleToAnalog(angle); + //not sure what offset does + val += offset; + + //setPWM(self, channel, on, off + //channel: The channel that should be updated with the new values (0..15) + //on: The tick (between 0..4095) when the signal should transition from low to high + //off:the tick (between 0..4095) when the signal should transition from high to low + + pwm.setPWM(channel,0,val); + //usleep(30); + cout << "Channel: " << channel << "\tSet to angle: " << angle << "\tVal: " << val << endl; + return(0); +} + + +int main () { + + int homeh = 90; + int homev = 90; + + //make sure you use the right address values. + PCA9685 pwm; + pwm.init(1,0x40); + usleep(1000 * 100); + cout << "Setting frequency: " << FREQUENCY << endl; + pwm.setPWMFreq (FREQUENCY); + usleep(1000 * 1000); + + cout << "Returning to HOME position." << endl; + setAngle(homeh, pwm, chanh); + setAngle(homev, pwm, chanv); + + + return 0; +} \ No newline at end of file diff --git a/app/raspberry_pi/xmake.lua b/app/raspberry_pi/xmake.lua index 86b6ebab..8ea0b3c4 100644 --- a/app/raspberry_pi/xmake.lua +++ b/app/raspberry_pi/xmake.lua @@ -21,7 +21,7 @@ add_requires("imath") if is_plat("linux", "macosx") then -- For the SunFounder Car - add_requires("pca9685") + add_requires("pca9685", {configs = {shared = true}}) end -- For Functional Programming? @@ -56,4 +56,19 @@ target("raspberry_pi") set_configdir("$(buildir)/$(plat)/$(arch)/$(mode)") add_configfiles("settings/config.jsonc", {onlycopy = true, prefixdir = "settings"}) - \ No newline at end of file + +-- From xmake sample code: + +if is_plat("linux", "macosx") then + for _, file in ipairs(os.files("tests/pca9685/test_*.cpp")) do + local name = path.basename(file) + target(name) + set_kind("binary") + set_default(false) + + add_packages("pca9685") + + add_files("tests/pca9685/" .. name .. ".cpp") + target_end() + end +end \ No newline at end of file