【Follow me第二季第2期】Arduion UR4 作业提交
[复制链接]
本帖最后由 我的学号 于 2024-11-1 01:54 编辑
一. 视频展示
1.1 物料展示
1.2 视频演示
提交视频
二. 任务实现详情
2.1 入门任务(必做):搭建环境并开启第一步Blink / 串口打印Hello EEWorld!
2.1.1 物料准备&设计思路
Arduino IDE /Arduino UR4 WIFI 开发板/ TYPE-C 连接线
硬件分析:【Follow me第二季第2期】1.初识 Arduino R4
2.1.2 软件流程
2.1.3 代码片段
- void setup()
- {
-
- pinMode(LED_BUILTIN, OUTPUT);
- Serial.begin(9600);
-
- }
-
- void loop()
- {
-
- digitalWrite(LED_BUILTIN, HIGH);
- delay(1000);
- digitalWrite(LED_BUILTIN,LOW);
- delay(1000);
- Serial.println("Hello Wrold!");
- }
任务帖详解:
【Follow me第二季第2期】2.入门任务之点灯和串口通信
2.2 基础任务(必做):驱动12x8点阵LED;用DAC生成正弦波;用OPAMP放大DAC信号;用ADC采集并且打印数据到串口等其他接口可上传到上位机显示曲线
2.2.1 物料准备&设计思路
Arduino IDE /Arduino UR4 WIFI 开发板/ TYPE-C 连接线 /运放反馈线路
2.2.2 软件流程
2.2.3 代码片段
- #include "Arduino_LED_Matrix.h"
- #include "analogWave.h"
- #include "OPAMP.h"
-
-
- ArduinoLEDMatrix LedMatrix;
- analogWave Wave(DAC);
-
- int WaveFre =10;
-
- int i32AdcReadResult =0;
-
-
- unsigned long u32Heart1frame[] =
- {
- 0x3184a484,
- 0x24042081,
- 0x100a0040
- };
-
- unsigned long u32Heart2frame[] =
- {
- 0x0001b024,
- 0x81100a00,
- 0x40000000
- };
-
- void setup()
- {
-
- pinMode(LED_BUILTIN, OUTPUT);
- Serial.begin(115200);
- LedMatrix.begin();
- Wave.sine(WaveFre);
- Wave.start();
- OPAMP.begin(OPAMP_SPEED_HIGHSPEED);
- analogReadResolution(14);
-
- }
-
- void loop()
- {
-
- LedMatrix.loadFrame(u32Heart1frame);
- i32AdcReadResult =analogRead(A4);
- Serial.print("ADC Result is:");
- Serial.print(String(i32AdcReadResult));
- Serial.print("\n");
-
- delay(1);
- }
-
任务帖详解:
【Follow me第二季第2期】3.基础任务之 LED 矩阵
【Follow me第二季第2期】4.基础任务之 DAC/OPAM/ADC
2.3 进阶任务(必做):通过Wi-Fi,利用MQTT协议接入到开源的智能家居平台HA(HomeAssistant)
2.3.1 物料准备&设计思路
装有 HA 的上位机/Arduino IDE /Arduino UR4 WIFI 开发板/ TYPE-C 连接线
2.3.2 软件流程
2.3.3 代码片段
- #include <WiFiS3.h>
- #include <ArduinoHA.h>
-
- #include "arduino_secrets.h"
- #include "analogWave.h"
-
-
- #define LED_RGB_G D5
- #define LED_RGB_R D6
- #define LED_RGB_B D9
-
-
- char ssid[] = SECRET_SSID;
- char pass[] = SECRET_PASS;
- int status = WL_IDLE_STATUS;
-
- WiFiClient client;
- HADevice device(MQTT_CLIENT_ID);
- HAMqtt mqtt(client, device);
-
- HALight light("MyRgbLight", HALight::RGBFeature);
-
- byte u8RgbSwitch =0;
-
- void setup()
- {
-
- pinMode(LED_RGB_R, OUTPUT);
- pinMode(LED_RGB_G, OUTPUT);
- pinMode(LED_RGB_B, OUTPUT);
-
- analogWrite(LED_RGB_R, 260);
- analogWrite(LED_RGB_G, 260);
- analogWrite(LED_RGB_B, 260);
-
-
- Serial.begin(9600);
- while (!Serial) {
- ;
- }
-
-
- if (WiFi.status() == WL_NO_MODULE)
- {
- Serial.println("Communication with WiFi module failed!");
-
- while (true);
- }
-
-
- while (status != WL_CONNECTED) {
- Serial.print("Attempting to connect to WPA SSID: ");
- Serial.println(ssid);
-
- status = WiFi.begin(ssid, pass);
-
-
- delay(10000);
- }
-
-
- Serial.print("You're connected to the network");
-
- device.setName("Arduino Ur4");
- light.setName("Bathroom");
-
-
- light.onStateCommand(onStateCommand);
- light.onRGBColorCommand(onRGBColorCommand);
-
- Serial.println("\nStart connecting to MQTT server");
- if (!mqtt.begin(MQTT_SERVER, MQTT_PORT, MQTT_USERNAME, MQTT_PASSWORD))
- {
- Serial.print("Connection falied");
- Serial.print(mqtt.getState());
- Serial.println("Try again in 5 seconds");
- delay(5000);
- }
- }
-
- void loop() {
-
- mqtt.loop();
- }
-
-
- void onStateCommand(bool state, HALight* sender)
- {
- Serial.print("State: ");
- Serial.println(state);
-
- if(1 ==state)
- {
- u8RgbSwitch =1;
- }
- else
- {
- u8RgbSwitch =0;
- }
-
- sender->setState(state);
- }
-
- void onBrightnessCommand(uint8_t brightness, HALight* sender)
- {
- Serial.print("Brightness: ");
- Serial.println(brightness);
-
- sender->setBrightness(brightness);
- }
-
- void onRGBColorCommand(HALight::RGBColor color, HALight* sender)
- {
- Serial.print("Red: ");
- Serial.println(color.red);
- Serial.print("Green: ");
- Serial.println(color.green);
- Serial.print("Blue: ");
- Serial.println(color.blue);
-
- if(1 ==u8RgbSwitch)
- {
- analogWrite(LED_RGB_R, (color.red*2));
- analogWrite(LED_RGB_G, (color.green*2));
- analogWrite(LED_RGB_B, (color.blue*2));
- }
- else
- {
- analogWrite(LED_RGB_R, 260);
- analogWrite(LED_RGB_G, 260);
- analogWrite(LED_RGB_B, 260);
- }
- sender->setRGBColor(color);
- }
-
-
2.3.4 任务帖详解
Follow me第二季第2期】5.进阶任务之 树莓派 HA+Windows+UR4 控制 RGB
2.4 扩展任务1: 外拓板实现 NTP 实时时钟
2.4.1 硬件展示
考虑到 UR4 板载的资源不算多,为充分体验其功能设计打样并手焊了一块拓展板, 结构如下:
拓展功能有:
添加三个 PWM 引脚控制 RGB; 这里选的是共阳的 RGB
三个按键验证 IO 输入;需要注意 UR4 的最大输入电流 8mA; 使用时将 IO 设置为内部下拉低电平,按键按下后得到高电平
板子到手后发现防抖的电容C2 C3 C4 属于画蛇添足,可以删除
使用 NORFLASH 验证 SPI 功能;GD 的这款FLASH 供电 无法用 5V 供电,利用 NMOS 做了电平转换
digikey 上的传感器模块价格比较高,添加AHT20 验证 IIC 功能;该封装不好焊
无屏不成板,添加了一个 OLED 接口,这里和 温湿度传感器使用的是同一条 IIC 总线
针对 DAC 功能,设计了一个功放线路;SC8002B 的PIN1 在低电平时芯片工作,于是用 NMOS 做了一个开关,逻辑却是反的
D7 给高电平 -->MOS 导通 --> 8002B PIN1 为低电平
针对 ADC 功能,设计了咪头+运放采样
模块线路在网上都可以找到借鉴,物料在淘宝也能买到;原理图电阻电容基本是0805,可以根据手上物料进行修改
绘制PCB 板时需要特别数以尺寸和插针位置;我采用的方法是从官网下载板子的CAD文件,导入立创 EDA 后复制其机械层,得到外框
连接件则使用了四个单排 2.54 插针,其位置可以通过单击CAD 插座首PIN得到XY 坐标
考虑到 LED 灯矩阵显示,拓展板上对应位置做了挖槽处理, 旁边的 UART 接口也一并留空
板子到手后才发现电源插座有干涉,同样挖槽
验证拓展板位置是否正确, 可以打印在 A4 纸上进行对比;确认无误后打板
3D 图效果:
实物图如下所示:
2.4.2 软件流程:
2.4.3 主要代码片段
-
-
-
-
-
-
-
- void TimerInterruptHandle()
- {
- bKey1Status =digitalRead(INPUT_KEY1);
- bKey2Status =digitalRead(INPUT_KEY2);
- bKey3Status =digitalRead(INPUT_KEY3);
-
- if(0 ==u8LedBlinkCnt)
- {
- digitalWrite(LED_BUILTIN, HIGH);
- RgbSetColor(0,0,255);
- LedMatrix.loadFrame(u32Heart1frame);
- u8LedBlinkCnt =1;
- }
- else
- {
- digitalWrite(LED_BUILTIN, LOW);
- RgbSetColor(0,255,0);
- LedMatrix.loadFrame(u32Heart2frame);
- u8LedBlinkCnt =0;
- }
- }
- void loop()
- {
- OledDispUpdate(&t_NTP_Time);
- Aht20Sensor.getEvent(&Ath20HumidityStruct, &Ath20TemperatureStruct);
- NtpClientTimeUpdate(&t_NTP_Time);
- }
显示效果:
播放器加载失败: 未检测到Flash Player,请到 安装
NTP 时钟
2.5 扩展任务2: ST25DV16 NFC 模块的使用
2.5.1 物料准备
Arduino IDE /Arduino UR4 WIFI 开发板/ TYPE-C 连接线 / ST25DV16 NFC 模块
2.5.2 软件流程
2.5.3 代码片段
-
- #include "Arduino.h"
- #include "Wire.h"
- #include "Adafruit_GFX.h"
- #include "CBTimer.h"
- #include "Adafruit_AHTX0.h"
- #include "Arduino_LED_Matrix.h"
- #include "WiFiS3.h"
- #include <SparkFun_ST25DV64KC_Arduino_Library.h>
-
-
-
-
- CBTimer Timer;
- ArduinoLEDMatrix LedMatrix;
-
- SFE_ST25DV64KC_NDEF StmTag;
-
-
-
- byte u8IsrTicker =0;
- byte u8LedBlinkCnt =0;
- byte u8LedMatrixCnt =0;
-
- bool bModuleInitStatus =0;
-
-
- unsigned long u32Heart1frame[] =
- {
- 0x3184a484,
- 0x24042081,
- 0x100a0040
- };
-
- unsigned long u32Heart2frame[] =
- {
- 0x0001b024,
- 0x81100a00,
- 0x40000000
- };
-
- uint8_t u8TagValue[8] ={0};
-
-
-
-
-
-
-
-
- void TimerInterruptHandle()
- {
-
- if(0 ==u8LedBlinkCnt)
- {
- digitalWrite(LED_BUILTIN, HIGH);
- LedMatrix.loadFrame(u32Heart1frame);
- u8LedBlinkCnt =1;
- }
- else
- {
- digitalWrite(LED_BUILTIN, LOW);
- LedMatrix.loadFrame(u32Heart2frame);
- u8LedBlinkCnt =0;
- }
- }
-
- void setup()
- {
-
- pinMode(LED_BUILTIN, OUTPUT);
-
-
- pinMode(D2, OUTPUT);
-
-
- Serial.begin(9600);
-
-
- LedMatrix.begin();
-
-
- Wire1.begin();
- bModuleInitStatus =StmTag.begin(Wire1);
- while(0 ==bModuleInitStatus)
- {
- Serial.println("St25dv init fail");
- delay(100);
- }
- Serial.println("St25dv init success");
-
-
- if(StmTag.getDeviceUID(u8TagValue))
- {
- Serial.println("Device UID is:");
- for(uint8_t i =0; i<8; i++)
- {
- if(u8TagValue[i] <0x0A)
- {
- Serial.print(F("0"));
- }
- Serial.print(u8TagValue[i], HEX);
- Serial.print(" ");
- }
- Serial.println();
- }
- else
- {
- Serial.println("Could not read devive UID!");
- }
-
-
-
- SetSt25dvUriInfo(&StmTag);
-
-
- Timer.begin(1000, TimerInterruptHandle);
- Serial.println("Init Finish");
- }
-
- void loop()
- {
-
- }
-
-
-
-
-
-
-
-
- void SetSt25dvUriInfo(SFE_ST25DV64KC_NDEF *tag)
- {
-
- uint8_t s_u8TagMemory[256];
- memset(s_u8TagMemory, 0 ,256);
- tag->writeEEPROM(0x0, s_u8TagMemory, 256);
-
-
- tag->writeCCFile8Byte();
-
-
- tag->writeNDEFURI("eeworld.com.cn",SFE_ST25DV_NDEF_URI_ID_CODE_HTTPS_WWW);
-
- Serial.println("Uri info write success!");
-
- }
-
-
-
-
2.5.4 任务帖详解
【Follow me第二季第2期】6.进阶任务之 ST25DV16 NFC 模块的使用
三 .可下载代码
【Follow me第二季第2期】Arduion UR4 作业提交代码
四. 心得体会
首先需要感谢EEWORLD 和 Digikey 举办了这么好的一个活动,通过本次活动学会了Arduino 的基本开发,HA 系统的搭建和交互,NTP 实时时钟的完成,这都是之前未研究过的课题,一路上遇到的困难不少,但问题得以解决时也很开心;也通过本次活动复习了画板打板的流程,受益匪浅;略遗憾的是本来计划实现 UR4 和 PICOW 的无线通信最终因时间关系没有完成。Anyway, 正如视频所说,玩板子是件开心的事情,期望后续多多举办类似活动,和大家共同学习成长!
|