【基于ELF 1开发板完成的“智慧光伏”项目】
“智慧光伏”项目能够智能追踪阳光的移动轨迹。通过内置的光敏传感器和智能控制系统,实时感知周围光源的变化,并驱动太阳能板精准调整角度,确保每一缕阳光都能被最大化地捕捉和利用。下面就和各位小伙伴详细介绍一下这一开源项目是怎样实现的。
1、四路光敏传感器与板卡的连接
2、舵机与板卡的连接
1、实现舵机转动的功能
elf@ubuntu:~/work$ tar -xvf linux-4.1.15-elf1.tar.bz2
(2)修改顶层设备树文件arch/arm/boot/dts/imx6ull.dtsi
elf@ubuntu:~/work$ cd linux-4.1.15-elf1/
elf@ubuntu:~/work/linux-4.1.15-elf1$ vi arch/arm/boot/dts/imx6ull.dtsi
elf@ubuntu:~/work/linux-4.1.15-elf1$ vi arch/arm/boot/dts/imx6ull-elf1-emmc.dts
添加pwm7、pwm8设备节点
&pwm7 {
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_pwm7>;
status = "okay";
};
&pwm8 {
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_pwm8>;
status = "okay";
};
pinctrl_pwm7: pwm7grp {
fsl,pins = <
MX6UL_PAD_CSI_VSYNC__PWM7_OUT 0x110b0
>;
};
pinctrl_pwm8: pwm8grp {
fsl,pins = <
MX6UL_PAD_CSI_HSYNC__PWM8_OUT 0x110b0
>;
};
2、实现获取光照值的功能
elf@ubuntu:~/work$ cd linux-4.1.15-elf1/
elf@ubuntu:~/work/linux-4.1.15-elf1$ vi arch/arm/boot/dts/imx6ull-elf1-emmc.dts
pinctrl_adc1: adc1grp {
fsl,pins = <
MX6UL_PAD_GPIO1_IO05__GPIO1_IO05 0x00b0
MX6UL_PAD_GPIO1_IO01__GPIO1_IO01 0x00b0
MX6UL_PAD_GPIO1_IO02__GPIO1_IO02 0x00b0
MX6UL_PAD_GPIO1_IO03__GPIO1_IO03 0x00b0
MX6UL_PAD_GPIO1_IO04__GPIO1_IO04 0x00b0
>;
};
3、编译并替换设备树
(1)执行环境变量
elf@ubuntu:~/work/linux-4.1.15-elf1$ . /opt/fsl-imx-x11/4.1.15-2.0.0/environment-setup-cortexa7hf-neon-poky-linux-gnueabi
(2)编译设备树
elf@ubuntu:~/work/linux-4.1.15-elf1$ make imx6ull_elf1_defconfig
elf@ubuntu:~/work/linux-4.1.15-elf1$ make dtbs
root@ELF1:~# cp /run/media/sda1/imx6ull-elf1-emmc.dtb /run/media/mmcblk1p1/
(4)保存并重启开发板
root@ELF1:~# sync
root@ELF1:~# reboot
1、程序设计
int main() {
int up = 0;
int down = 0;
int left = 0;
int right = 0;
int duty_cycle6 = DUTY_90_DEG;
int duty_cycle7 = DUTY_90_DEG;
int adcChannels[] = {1, 2, 3, 4};
int adcValues[4] = {0};
init_pwm();
write_file(PWM6_DUTY_CYCLE, duty_cycle6);
write_file(PWM7_DUTY_CYCLE, duty_cycle7);
while (1) {
for (int i = 0; i < 4; ++i) {
adcValues[i] = read_adc_value(adcChannels[i]);
}
up = (adcValues[3] + adcValues[0]) ;
down = (adcValues[2] + adcValues[1]) ;
left = (adcValues[3] + adcValues[2]) ;
right = (adcValues[0] + adcValues[1]) ;
printf("up: %dn", up);
printf("down: %dn", down);
printf("left: %dn", left);
printf("right: %dn", right);
if ((down - up >= 200) && (duty_cycle6 >= DUTY_0_DEG)) {
duty_cycle6 -= 100000;
write_file(PWM6_DUTY_CYCLE, duty_cycle6);
usleep(10000);
}
if ((up - down >= 200) && (duty_cycle6 <= DUTY_180_DEG)) {
duty_cycle6 += 100000;
write_file(PWM6_DUTY_CYCLE, duty_cycle6);
usleep(10000);
}
if ((right - left >= 200) && (duty_cycle7 >= DUTY_0_DEG)) {
duty_cycle7 -= 100000;
write_file(PWM7_DUTY_CYCLE, duty_cycle7);
usleep(10000);
}
if ((left - right >= 200) && (duty_cycle7 <= DUTY_180_DEG)) {
duty_cycle7 += 100000;
write_file(PWM7_DUTY_CYCLE, duty_cycle7);
usleep(10000);
}
usleep(100000);
}
return 0;
}
配置pwm
int init_pwm()
{
write_file(PWM6_EXPORT, 0);
if (-1 == write_file(PWM6_PERIOD, PWM_PERIOD))
return -1;
if (-1 == write_file(PWM6_ENABLE, 1))
return -1;
write_file(PWM7_EXPORT, 0);
if (-1 == write_file(PWM7_PERIOD, PWM_PERIOD))
return -1;
if (-1 == write_file(PWM7_ENABLE, 1))
return -1;
return 0;
}
设置舵机初始位置
write_file(PWM6_DUTY_CYCLE, duty_cycle6);
write_file(PWM7_DUTY_CYCLE, duty_cycle7);
获取光照值
int read_adc_value(int adcNumber) {
int adcValue = 0 ;
FILE* adcFile;
char adcValueFile[50];
char adcValueStr[10] = "0";
snprintf(adcValueFile, sizeof(adcValueFile), "/sys/bus/iio/devices/iio:device0/in_voltage%d_raw", adcNumber);
adcFile = fopen(adcValueFile, "r");
if (adcFile) {
fgets(adcValueStr, sizeof(adcValueStr), adcFile);
adcValue = atoi(adcValueStr);
fclose(adcFile);
}
else {
fprintf(stderr, "Error opening ADC value file %s.n", adcValueFile);
}
return adcValue;
}
根据光照值调整舵机位置
if ((down - up >= 200) && (duty_cycle6 >= DUTY_0_DEG)) {
duty_cycle6 -= 100000;
write_file(PWM6_DUTY_CYCLE, duty_cycle6);
usleep(10000);
}
if ((up - down >= 200) && (duty_cycle6 <= DUTY_180_DEG)) {
duty_cycle6 += 100000;
write_file(PWM6_DUTY_CYCLE, duty_cycle6);
usleep(10000);
}
if ((right - left >= 200) && (duty_cycle7 >= DUTY_0_DEG)) {
duty_cycle7 -= 100000;
write_file(PWM7_DUTY_CYCLE, duty_cycle7);
usleep(10000);
}
if ((left - right >= 200) && (duty_cycle7 <= DUTY_180_DEG)) {
duty_cycle7 += 100000;
write_file(PWM7_DUTY_CYCLE, duty_cycle7);
usleep(10000);
}
2、应用编译
(1)拷贝sun.tar.bz2到开发环境/home/elf/work目录下解压
elf@ubuntu:~/work$ tar xvf sun.tar.bz2
(2)执行环境变量
elf@ubuntu:~/work$ . /opt/fsl-imx-x11/4.1.15-2.0.0/environment-setup-cortexa7hf-neon-poky-linux-gnueabi
elf@ubuntu:~/work$ cd sun/
elf@ubuntu:~/work/sun$ make
(4)拷贝sanapp到开发板/home/root路径下
root@ELF1:~# cp /run/media/sda1/sunapp ./
root@ELF1:~# sync
四、项目测试
1、确保开发板已正确连接四路光敏传感器和舵机
2、执行应用
root@ELF1:~# ./sunapp
此时可以打开手电筒模拟太阳光,调整太阳能板的角度
对于初涉嵌入式开发领域的小伙伴来说,“智慧光伏”无疑是一个兼具趣味与实践的开源项目。通过亲身参与这一开源项目的组装与调试过程,可以直接接触到真实的开发环境、工具链和代码库,从而解决实际问题,帮助初学者更快地成长为合格的嵌入式开发工程师。