小安派S1+雷达模组新玩法,想法在这里,让AI为我写程序,大家看看可行吗?

[复制链接]
回帖奖励 1 金钱      回复本帖可获得 1 金钱奖励! 每人限 1 次
查看1518 | 回复10 | 2023-10-17 09:56:09 | 显示全部楼层 |阅读模式
本帖最后由 molun 于 2023-10-17 09:57 编辑
为安信可小安派开发板写一段代码:雷达模组RD-03输出信号给小安派S1,检测到有人移动时,小安派语音播报“您已进入监控区域,请注意自己的言行举止”



猜猜看,以下代码都是哪些平台写出来的?是否可行,具有参考价值?


A.
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <string.h>
  5. #include "freertos/FreeRTOS.h"
  6. #include "freertos/task.h"
  7. #include "driver/gpio.h"
  8. #include "driver/i2c.h"
  9. #include "sensor_api.h"

  10. #define GPIO_RADAR 2
  11. #define GPIO_SPEAKER 15

  12. bool has_detected = false;

  13. void app_main()
  14. {
  15.     // 初始化 GPIO
  16.     gpio_pad_select_gpio(GPIO_RADAR);
  17.     gpio_set_direction(GPIO_RADAR, GPIO_MODE_INPUT);
  18.    
  19.     gpio_pad_select_gpio(GPIO_SPEAKER);
  20.     gpio_set_direction(GPIO_SPEAKER, GPIO_MODE_OUTPUT);
  21.    
  22.     // 初始化 I2C 驱动和雷达模组
  23.     i2c_config_t i2c_conf;
  24.     i2c_conf.scl_pin = GPIO_I2C0_SCL;
  25.     i2c_conf.sda_pin = GPIO_I2C0_SDA;
  26.     i2c_conf.master.clk_speed = 100000;
  27.     i2c_init(&i2c_conf);
  28.    
  29.     // 启动雷达模组
  30.     uint8_t addr = 0x10; // RD-03 的 I2C 地址为 0x10
  31.     uint8_t reg = 0x00; // 读取状态寄存器
  32.     uint8_t data = 0x00; // 先读取状态寄存器的值,确保初始状态为 0x00
  33.     i2c_master_select(addr, true);
  34.     i2c_master_write(&addr, &reg, sizeof(reg));
  35.     i2c_master_read(&addr, &data, sizeof(data));
  36.     i2c_master_select(addr, false);
  37.    
  38.     // 启动 FreeRTOS
  39.     vTaskStartScheduler();
  40. }

  41. void IRAM_ATTR check_radar()
  42. {
  43.     // 检查雷达信号是否变化,如果变化则表示检测到人移动
  44.     if (gpio_get_level(GPIO_RADAR) == 1) { // 如果雷达信号为高电平,表示检测到人移动
  45.         if (!has_detected) { // 如果之前没有检测到人移动,则进行语音播报
  46.             has_detected = true; // 标记已经检测到人移动
  47.             // 控制扬声器播放语音“您已进入监控区域,请注意自己的言行举止”
  48.             // 此处需要使用小安派开发板提供的语音播报 API 进行实现,具体实现方式根据开发板的文档进行编写,这里不再赘述。
  49.         }
  50.     } else { // 如果雷达信号为低电平,表示没有检测到人移动
  51.         if (has_detected) { // 如果之前检测到人移动,则重置标记并处理相应的逻辑(例如重置扬声器)
  52.             has_detected = false; // 重置标记
  53.             // 控制扬声器停止播放语音(具体实现方式根据开发板的文档进行编写,这里不再赘述)
  54.         }
  55.     }
  56. }
复制代码
B.
  1. import machine  
  2. import neopixel  
  3. import utime  
  4.   
  5. # 设置小安派S1的GPIO引脚  
  6. pin = machine.Pin(5, machine.Pin.IN)  
  7.   
  8. # 设置RD-03雷达模组的GPIO引脚  
  9. radar_pin = machine.Pin(4, machine.Pin.IN)  
  10.   
  11. # 初始化语音播报器  
  12. speaker = machine.PWM(machine.Pin(0))  
  13. speaker.freq(1000)  
  14.   
  15. # 初始化RGB灯  
  16. rgb = neopixel.NeoPixel(machine.Pin(2), 1)  
  17. rgb.write((0, 0, 0))  
  18.   
  19. # 等待雷达模组启动完成  
  20. utime.sleep(2)  
  21.   
  22. while True:  
  23.     # 检测雷达模组输出信号  
  24.     if radar_pin.value() == 1:  
  25.         # 检测到有人移动,播报语音提示  
  26.         speaker.duty_u16(500)  
  27.         utime.sleep_ms(100)  
  28.         speaker.duty_u16(1000)  
  29.         utime.sleep_ms(100)  
  30.         speaker.duty_u16(500)  
  31.         utime.sleep_ms(100)  
  32.         speaker.duty_u16(0)  
  33.         # 点亮RGB灯红色  
  34.         rgb.write((255, 0, 0))  
  35.     else:  
  36.         # 没有检测到人移动,熄灭RGB灯  
  37.         rgb.write((0, 0, 0))
复制代码

C.
  1. #include <stdio.h>
  2. #include <wiringPi.h>
  3. #include <wiringPiI2C.h>
  4. #include <bcm2835.h>

  5. #define SDA 2
  6. #define SCL 3
  7. #define I2C_ADDR 0x70

  8. void initialize_i2c() {
  9.   wiringPiI2CSetup(SDA, SCL);
  10.   bcm2835_i2c_setSpeed(100000);
  11. }

  12. void write_register(uint8_t reg, uint8_t value) {
  13.   wiringPiI2CWriteReg8(I2C_ADDR, reg, value);
  14. }

  15. void read_register(uint8_t reg, uint8_t* value) {
  16.   wiringPiI2CReadReg8(I2C_ADDR, reg, value);
  17. }

  18. int main() {
  19.   initialize_i2c();

  20.   while(1) {
  21.     uint8_t data = 0;
  22.     read_register(0x00, &data);

  23.     if(data == 1) { //有人移动
  24.       printf("检测到有人移动\n");
  25.       system("espeak '您已进入监控区域,请注意自己的言行举止'");
  26.     }
  27.   }

  28.   return 0;
  29. }
复制代码


D.
  1. import RPi.GPIO as GPIO
  2. import time
  3. import speech_recognition as sr

  4. # 设置雷达模组RD-03的引脚
  5. TRIG = 23
  6. ECHO = 24

  7. # 设置小安派S1的引脚
  8. SPEAKER = 25
  9. MIC = 26

  10. # 初始化GPIO
  11. GPIO.setmode(GPIO.BCM)
  12. GPIO.setup(TRIG, GPIO.OUT)
  13. GPIO.setup(ECHO, GPIO.IN)
  14. GPIO.setup(SPEAKER, GPIO.OUT)
  15. GPIO.setup(MIC, GPIO.IN)

  16. # 初始化语音识别器
  17. r = sr.Recognizer()

  18. def distance():
  19.     GPIO.output(TRIG, True)
  20.     time.sleep(0.00001)
  21.     GPIO.output(TRIG, False)
  22.     while GPIO.input(ECHO) == 0:
  23.         pulse_start = time.time()
  24.     while GPIO.input(ECHO) == 1:
  25.         pulse_end = time.time()
  26.     pulse_duration = pulse_end - pulse_start
  27.     distance = pulse_duration * 17150
  28.     distance = round(distance, 2)
  29.     return distance

  30. try:
  31.     while True:
  32.         dist = distance()
  33.         if dist < 10:  # 假设距离小于10cm时,认为有人进入监控区域
  34.             print("您已进入监控区域,请注意自己的言行举止")
  35.             with sr.Microphone() as source:
  36.                 print("请说话:")
  37.                 audio = r.listen(source)
  38.                 try:
  39.                     text = r.recognize_google(audio, language='zh-CN')
  40.                     print("你说的是: {}".format(text))
  41.                 except Exception as e:
  42.                     print("无法识别语音,请重试。")
  43.             GPIO.output(SPEAKER, GPIO.HIGH)
  44.             time.sleep(1)
  45.             GPIO.output(SPEAKER, GPIO.LOW)
  46.         else:
  47.             print("无人进入监控区域")
  48.         time.sleep(1)
  49. except KeyboardInterrupt:
  50.     print("程序终止")
  51. finally:
  52.     GPIO.cleanup()
复制代码

等风,等雨,也等你!
回复

使用道具 举报

496199544 | 2023-10-17 09:59:39 | 显示全部楼层

回帖奖励 +1 金钱

学习
回复

使用道具 举报

爱笑 | 2023-10-17 10:02:04 | 显示全部楼层

回帖奖励 +1 金钱

学习
用心做好保姆工作
回复

使用道具 举报

ai_mcu | 2023-10-17 10:16:46 | 显示全部楼层

回帖奖励 +1 金钱

开发这个还用上了不同平台版本吗,太强了吧
明天总会更好
回复 支持 反对

使用道具 举报

WangChong | 2023-10-17 10:25:18 | 显示全部楼层

回帖奖励 +1 金钱

羡慕
回复

使用道具 举报

ckdsx.cn | 2023-10-17 10:36:51 | 显示全部楼层

回帖奖励 +1 金钱

杀鸡何必牛刀,感应模块+语音录放模块,应该可以完整这个需求,成本应该不一样!
回复 支持 1 反对 0

使用道具 举报

bzhou830 | 2023-10-17 10:48:55 | 显示全部楼层

回帖奖励 +1 金钱

学习
选择去发光,而不是被照亮
回复

使用道具 举报

bzhou830 | 2023-10-17 10:50:52 | 显示全部楼层
ckdsx.cn 发表于 2023-10-17 10:36
杀鸡何必牛刀,感应模块+语音录放模块,应该可以完整这个需求,成本应该不一样! ...

确实,这样成本节省一大部分
选择去发光,而不是被照亮
回复 支持 反对

使用道具 举报

粉色小风扇 | 2023-10-17 15:01:48 | 显示全部楼层

回帖奖励 +1 金钱

太强了,学习了
回复 支持 反对

使用道具 举报

lsrly | 2023-10-21 09:33:31 | 显示全部楼层

回帖奖励 +1 金钱

太强了,有机会多向大佬学习
好好学习,努力挣钱,专心
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则