【Follow me第二季第2期】Arduion UR4 作业提交
本帖最后由 我的学号 于 2024-11-1 01:54 编辑<p>一. 视频展示</p>
<p>1.1 物料展示</p>
<table align="center" border="1" cellpadding="1" cellspacing="1">
<tbody>
<tr>
<td>1</td>
<td>Arduino UR4 wifi </td>
<td> </td>
</tr>
<tr>
<td>2</td>
<td>
<p>ST25DV16 NFC 模块</p>
</td>
<td> </td>
</tr>
<tr>
<td>3</td>
<td>Raspberry PICOW</td>
<td> </td>
</tr>
</tbody>
</table>
<p> </p>
<p>1.2 视频演示</p>
<p><a href="https://training.eeworld.com.cn/video/41256" target="_blank">提交视频</a></p>
<p> </p>
<p>二. 任务实现详情</p>
<p>2.1 入门任务(必做):搭建环境并开启第一步Blink / 串口打印Hello EEWorld!</p>
<p>2.1.1 物料准备&设计思路</p>
<p>Arduino IDE /Arduino UR4 WIFI 开发板/ TYPE-C 连接线 </p>
<p>硬件分析:<a href="https://bbs.eeworld.com.cn/thread-1291617-1-1.html" target="_blank">【Follow me第二季第2期】1.初识 Arduino R4</a></p>
<p> </p>
<p>2.1.2 软件流程</p>
<p> </p>
<p> </p>
<p>2.1.3 代码片段</p>
<pre>
<code class="language-cpp">void setup()
{
// put your setup code here, to run once:
pinMode(LED_BUILTIN, OUTPUT); //LED IO 输出模式
Serial.begin(9600); //串口波特率9600
}
void loop()
{
// put your main code here, to run repeatedly:
digitalWrite(LED_BUILTIN, HIGH); //LED IO 输出高电平
delay(1000); //延时 1s
digitalWrite(LED_BUILTIN,LOW); //LED IO 输出低电平
delay(1000); //延时 1s
Serial.println("Hello Wrold!"); //串口发送数据
}</code></pre>
<p>任务帖详解:</p>
<p><a href="https://bbs.eeworld.com.cn/thread-1291644-1-1.html" target="_blank">【Follow me第二季第2期】2.入门任务之点灯和串口通信</a></p>
<p>
<p> </p>
</p>
<p>2.2 基础任务(必做):驱动12x8点阵LED;用DAC生成正弦波;用OPAMP放大DAC信号;用ADC采集并且打印数据到串口等其他接口可上传到上位机显示曲线</p>
<p>2.2.1 物料准备&设计思路</p>
<p>Arduino IDE /Arduino UR4 WIFI 开发板/ TYPE-C 连接线 /运放反馈线路</p>
<p> </p>
<p>2.2.2 软件流程</p>
<p> </p>
<p> </p>
<p>2.2.3 代码片段</p>
<pre>
<code class="language-cpp">#include "Arduino_LED_Matrix.h"
#include "analogWave.h"
#include "OPAMP.h"
//模块对象定义
ArduinoLEDMatrixLedMatrix;
analogWave Wave(DAC);
int WaveFre =10;
int i32AdcReadResult =0;
//数组定义
unsigned long u32Heart1frame[] = //大心图案
{
0x3184a484,
0x24042081,
0x100a0040
};
unsigned long u32Heart2frame[] = //小心图案
{
0x0001b024,
0x81100a00,
0x40000000
};
void setup()
{
// put your setup code here, to run once:
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
LedMatrix.begin();
Wave.sine(WaveFre);
Wave.start();
OPAMP.begin(OPAMP_SPEED_HIGHSPEED);
analogReadResolution(14);
}
void loop()
{
// put your main code here, to run repeatedly:
LedMatrix.loadFrame(u32Heart1frame);
i32AdcReadResult =analogRead(A4);
Serial.print("ADC Result is:"); //数据上传提示
Serial.print(String(i32AdcReadResult)); //转换为字符类型
Serial.print("\n"); //数据换行
delay(1); //延时等待1ms
}
</code></pre>
<p>任务帖详解:</p>
<p><a href="https://bbs.eeworld.com.cn/thread-1293080-1-1.html" target="_blank">【Follow me第二季第2期】3.基础任务之 LED 矩阵</a></p>
<p><a href="https://bbs.eeworld.com.cn/thread-1293082-1-1.html" target="_blank">【Follow me第二季第2期】4.基础任务之 DAC/OPAM/ADC</a></p>
<p> </p>
<p>2.3 进阶任务(必做):通过Wi-Fi,利用MQTT协议接入到开源的智能家居平台HA(HomeAssistant)</p>
<p>2.3.1 物料准备&设计思路</p>
<p>装有 HA 的上位机/Arduino IDE /Arduino UR4 WIFI 开发板/ TYPE-C 连接线</p>
<p> </p>
<p>2.3.2 软件流程</p>
<p> </p>
<p> </p>
<p>2.3.3 代码片段</p>
<pre>
<code class="language-cpp">#include <WiFiS3.h>
#include <ArduinoHA.h>
#include "arduino_secrets.h"
#include "analogWave.h"
//RGB PIN 定义
#define LED_RGB_G D5
#define LED_RGB_R D6
#define LED_RGB_B D9
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS; // the WiFi radio's status
WiFiClient client;
HADevice device(MQTT_CLIENT_ID);
HAMqtt mqtt(client, device);
HALight light("MyRgbLight", HALight::RGBFeature);
byte u8RgbSwitch =0;
void setup()
{
//RGB 初始化
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);
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE)
{
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the data:
Serial.print("You're connected to the network");
device.setName("Arduino Ur4");
light.setName("Bathroom");
// handle light states
light.onStateCommand(onStateCommand);
light.onRGBColorCommand(onRGBColorCommand); // optional
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() {
// check the network connection once every 10 seconds:
mqtt.loop();
}
//RGB 相关函数
void onStateCommand(bool state, HALight* sender)
{
Serial.print("State: ");
Serial.println(state);
if(1 ==state) //RGB 开启
{
u8RgbSwitch =1;
}
else //RGB 关闭
{
u8RgbSwitch =0;
}
sender->setState(state); // report state back to the Home Assistant
}
void onBrightnessCommand(uint8_t brightness, HALight* sender)
{
Serial.print("Brightness: ");
Serial.println(brightness);
sender->setBrightness(brightness); // report brightness back to the Home Assistant
}
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); // report color back to the Home Assistant
}
</code></pre>
<p> </p>
<p>2.3.4 任务帖详解</p>
<p><a href="https://bbs.eeworld.com.cn/thread-1295986-1-1.html" target="_blank">Follow me第二季第2期】5.进阶任务之 树莓派 HA+Windows+UR4 控制 RGB</a></p>
<p> </p>
<p>2.4 扩展任务1: 外拓板实现 NTP 实时时钟</p>
<p>2.4.1 硬件展示</p>
<p>考虑到 UR4 板载的资源不算多,为充分体验其功能设计打样并手焊了一块拓展板, 结构如下:</p>
<p> </p>
<p> </p>
<p>拓展功能有:</p>
<p>添加三个 PWM 引脚控制 RGB; 这里选的是共阳的 RGB</p>
<p> </p>
<p>三个按键验证 IO 输入;需要注意 UR4 的最大输入电流 8mA; 使用时将 IO 设置为内部下拉低电平,按键按下后得到高电平</p>
<p>板子到手后发现防抖的电容C2 C3 C4 属于画蛇添足,可以删除</p>
<p> </p>
<p>使用 NORFLASH 验证 SPI 功能;GD 的这款FLASH 供电 无法用 5V 供电,利用 NMOS 做了电平转换</p>
<p></p>
<p> </p>
<p>digikey 上的传感器模块价格比较高,添加AHT20 验证 IIC 功能;该封装不好焊</p>
<p> </p>
<p>无屏不成板,添加了一个 OLED 接口,这里和 温湿度传感器使用的是同一条 IIC 总线</p>
<p> </p>
<p>针对 DAC 功能,设计了一个功放线路;SC8002B 的PIN1 在低电平时芯片工作,于是用 NMOS 做了一个开关,逻辑却是反的</p>
<p>D7 给高电平 -->MOS 导通 --> 8002B PIN1 为低电平 </p>
<p> </p>
<p>针对 ADC 功能,设计了咪头+运放采样</p>
<p> </p>
<p>模块线路在网上都可以找到借鉴,物料在淘宝也能买到;原理图电阻电容基本是0805,可以根据手上物料进行修改</p>
<p>绘制PCB 板时需要特别数以尺寸和插针位置;我采用的方法是从官网下载板子的CAD文件,导入立创 EDA 后复制其机械层,得到外框</p>
<p>连接件则使用了四个单排 2.54 插针,其位置可以通过单击CAD 插座首PIN得到XY 坐标</p>
<p>考虑到 LED 灯矩阵显示,拓展板上对应位置做了挖槽处理, 旁边的 UART 接口也一并留空</p>
<p>板子到手后才发现电源插座有干涉,同样挖槽</p>
<p>验证拓展板位置是否正确, 可以打印在 A4 纸上进行对比;确认无误后打板</p>
<p> </p>
<p> </p>
<p> </p>
<p>3D 图效果:</p>
<p> </p>
<p> </p>
<p>实物图如下所示:</p>
<p> </p>
<div></div>
<p> </p>
<p>2.4.2 软件流程:</p>
<p> </p>
<p>2.4.3 主要代码片段</p>
<pre>
<code class="language-cpp">//////////////////////////////////////////////////////////
//函数名称:TimerInterruptHandle
//函数功能:中断处理函数
//传入参数:无
//传出参数:无
//特别说明:1s 一次中断周期
//////////////////////////////////////////////////////////
void TimerInterruptHandle()
{
bKey1Status =digitalRead(INPUT_KEY1); //获取按键 1 状态
bKey2Status =digitalRead(INPUT_KEY2); //获取按键 2 状态
bKey3Status =digitalRead(INPUT_KEY3); //获取按键 3 状态
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;
}
}</code></pre>
<pre>
<code class="language-cpp">void loop()
{
OledDispUpdate(&t_NTP_Time);
Aht20Sensor.getEvent(&Ath20HumidityStruct, &Ath20TemperatureStruct); // 用时1s
NtpClientTimeUpdate(&t_NTP_Time);
}</code></pre>
<p>显示效果:</p>
<p>3dacc4b9819b1e038c86d4bd595601aa</p>
<p> </p>
<p>2.5 扩展任务2: ST25DV16 NFC 模块的使用</p>
<p>2.5.1 物料准备</p>
<p>Arduino IDE /Arduino UR4 WIFI 开发板/ TYPE-C 连接线 / ST25DV16 NFC 模块</p>
<p> </p>
<p>2.5.2 软件流程</p>
<p> </p>
<p> </p>
<p>2.5.3 代码片段</p>
<pre>
<code class="language-cpp">//头文件定义
#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 ={0};
//////////////////////////////////////////////////////////
//函数名称:TimerInterruptHandle
//函数功能:中断处理函数
//传入参数:无
//传出参数:无
//特别说明:1s 一次中断周期
//////////////////////////////////////////////////////////
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()
{
//LED 初始化
pinMode(LED_BUILTIN, OUTPUT); //LED 输出模式
//测试使用
pinMode(D2, OUTPUT);
//串口初始化
Serial.begin(9600); //设置串口波特率
//LED 矩阵初始化
LedMatrix.begin();
//Qwiic 初始化
Wire1.begin();
bModuleInitStatus =StmTag.begin(Wire1);
while(0 ==bModuleInitStatus)
{
Serial.println("St25dv init fail");
delay(100);
}
Serial.println("St25dv init success");
//获取标签 ID 号
if(StmTag.getDeviceUID(u8TagValue))
{
Serial.println("Device UID is:");
for(uint8_t i =0; i<8; i++)
{
if(u8TagValue <0x0A)
{
Serial.print(F("0"));
}
Serial.print(u8TagValue, HEX);
Serial.print(" ");
}
Serial.println();
}
else
{
Serial.println("Could not read devive UID!");
}
//只在设置时执行
SetSt25dvUriInfo(&StmTag);
//定时器中断初始化
Timer.begin(1000, TimerInterruptHandle);//1000ms 周期运行一遍函数
Serial.println("Init Finish");
}
void loop()
{
}
//////////////////////////////////////////////////////////
//函数名称:SetSt25dvUriInfo
//函数功能:设置 ST25DV 的 URi 信息
//传入参数:
//传出参数:无
//特别说明:无
//////////////////////////////////////////////////////////
void SetSt25dvUriInfo(SFE_ST25DV64KC_NDEF *tag)
{
//清零区域1 256Byte 数据
uint8_t s_u8TagMemory;
memset(s_u8TagMemory, 0 ,256);
tag->writeEEPROM(0x0, s_u8TagMemory, 256);
//写入类型5 CC 文件信息
tag->writeCCFile8Byte();
//写入类型5NDEF URI 信息
tag->writeNDEFURI("eeworld.com.cn",SFE_ST25DV_NDEF_URI_ID_CODE_HTTPS_WWW);
Serial.println("Uri info write success!");
}
</code></pre>
<p> </p>
<p>2.5.4 任务帖详解</p>
<p><a href="https://bbs.eeworld.com.cn/thread-1296317-1-1.html" target="_blank">【Follow me第二季第2期】6.进阶任务之 ST25DV16 NFC 模块的使用</a></p>
<p> </p>
<p>三 .可下载代码</p>
<p><a href="https://download.eeworld.com.cn/detail/%E6%88%91%E7%9A%84%E5%AD%A6%E5%8F%B7/634825" target="_blank">【Follow me第二季第2期】Arduion UR4 作业提交代码</a></p>
<p> </p>
<p>四. 心得体会</p>
<p>首先需要感谢EEWORLD 和 Digikey 举办了这么好的一个活动,通过本次活动学会了Arduino 的基本开发,HA 系统的搭建和交互,NTP 实时时钟的完成,这都是之前未研究过的课题,一路上遇到的困难不少,但问题得以解决时也很开心;也通过本次活动复习了画板打板的流程,受益匪浅;略遗憾的是本来计划实现 UR4 和 PICOW 的无线通信最终因时间关系没有完成。Anyway, 正如视频所说,玩板子是件开心的事情,期望后续多多举办类似活动,和大家共同学习成长!</p>
<p> balenaEtcher 这个工具很神奇,搞定就好</p>
<p>进阶必做任务内容补充:</p>
<p> </p>
<p>2.3 进阶任务(必做):通过Wi-Fi,利用MQTT协议接入到开源的智能家居平台HA(HomeAssistant)</p>
<p>2.3.1 物料准备&设计思路</p>
<p>装有 HA 的树莓派/Win10 主机/Arduino IDE /Arduino UR4 WIFI 开发板/ TYPE-C 连接线</p>
<p> </p>
<p>2.3.2 软件流程</p>
<p> </p>
<p>2.3.3 代码片段</p>
<pre>
<code class="language-cpp">#include <WiFiS3.h>
#include <ArduinoHA.h>
#include "arduino_secrets.h"
#include "analogWave.h"
//RGB PIN 定义
#define LED_RGB_G D5
#define LED_RGB_R D6
#define LED_RGB_B D9
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS; // the WiFi radio's status
WiFiClient client;
HADevice device(MQTT_CLIENT_ID);
HAMqtt mqtt(client, device);
HALight light("MyRgbLight", HALight::RGBFeature);
byte u8RgbSwitch =0;
void setup()
{
//RGB 初始化
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);
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE)
{
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the data:
Serial.print("You're connected to the network");
device.setName("Arduino Ur4");
light.setName("Bathroom");
// handle light states
light.onStateCommand(onStateCommand);
light.onRGBColorCommand(onRGBColorCommand); // optional
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() {
// check the network connection once every 10 seconds:
mqtt.loop();
}
//RGB 相关函数
void onStateCommand(bool state, HALight* sender)
{
Serial.print("State: ");
Serial.println(state);
if(1 ==state) //RGB 开启
{
u8RgbSwitch =1;
}
else //RGB 关闭
{
u8RgbSwitch =0;
}
sender->setState(state); // report state back to the Home Assistant
}
void onBrightnessCommand(uint8_t brightness, HALight* sender)
{
Serial.print("Brightness: ");
Serial.println(brightness);
sender->setBrightness(brightness); // report brightness back to the Home Assistant
}
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); // report color back to the Home Assistant
}
</code></pre>
<p>任务帖详解:<a href="https://bbs.eeworld.com.cn/thread-1295986-1-1.html" target="_blank">【Follow me第二季第2期】5.进阶任务之 树莓派 HA+Windows+UR4 控制 RGB</a></p>
<p>补充:<a href="https://download.eeworld.com.cn/detail/%E6%88%91%E7%9A%84%E5%AD%A6%E5%8F%B7/634825" target="_blank">可供下载代码链接</a></p>
<p>补充:<a href="https://bbs.eeworld.com.cn/thread-1296317-1-1.html" target="_blank">Follow me第二季第2期】6.进阶任务之 ST25DV16 NFC 模块的使用 </a></p>
页:
[1]