19浏览
查看: 19|回复: 0

[教程] C4002在货运停车场景中的应用

[复制链接]
本帖最后由 御坂10032号 于 2026-5-22 21:28 编辑

简介


我这里对C4002的具体参数不做额外的阐述,具体请参考C4002, 而是什么吸引我报名参加这次的评测活动呢? 主要是因为这个传感器的输出吸引了我。详情请参考下图。
它这里想对与传统的雷达传感器多了很多的输出参数,比如说光照的强度、运动距离(Motion distance 物体的运动距离) 运动速度(Motion speed)和移动方向(Motion direction)存在倒计时(Presence countdown)这些传感器数据的输出是比较有意思的,不同的数据的输出为不同场景的应用创造了可能。我的创意是:很多大货车司机经常会跑长途运输,在服务器或者某些地段停车休息的时候会经常出现加完一箱油然后被偷的情况。 根据C4002的参数,其探测距离是达到了10M。将C4002固定在大车窗上其前门车窗到油箱的距离正好是在10M内,所以结合对应的传感器或者设备可以做到通知报警的功能。 比如说结合蜂鸣器可以实现报警、结合MQTT可以做到远程报警通知等等。






C4002在货运停车场景中的应用图1


焊接好的传感器的实物拍摄

C4002在货运停车场景中的应用图2

C004 上一共有5个PIN,分别是VCC,GND, RX, TX 和 OUT, VCC 支持3.6~5.5V RX和TX的默认的波特率是115200,Out的话则是高低电平输出代表了当前的被检测物体的运动状态。




官方提供的Arduino的库如下所示

C4002在货运停车场景中的应用图3

由于国内在Arduino安装ESP32的库的时候经常会出现网络中断从而导致安装失败,因此我这次并没有按照C004上的教程那样使用Arduino的库,而是选择了使用树莓派Pico作为主控使用Micropython的方式进行驱动。根据上述的串口输出的图片得知,Arduino的库主要做的内容就是把串口发送的Hex数据解析****可以阅读的数据然后进行展示。

下图为这次测评使用的树莓派Pico
C4002在货运停车场景中的应用图4

所以我使用了Minimax结合Claude对原本Arduino的库进行了读取并且制作出了一个Micropython的版本。如下所示

C4002在货运停车场景中的应用图5

之后使用Thonny创建对应的库文件并且将库的内容复制进去,然后根据库文件的内容写好自己的main.c的代码
  1. import time
  2. import machine
  3. # Import the Pico library
  4. from DFRobot_C4002 import DFRobot_C4002, PresenceTarget, MotionTarget
  5. # Initialize UART0 on GP0(TX) and GP1(RX)
  6. # Default baudrate: 115200
  7. c4002 = DFRobot_C4002(uart_num=0, baud=115200, tx=0, rx=1)
  8. def setup():
  9.     while c4002.begin() != True:
  10.         print("C4002 begin failed!")
  11.         time.sleep_ms(500)
  12.     print("C4002 begin success!")
  13.     time.sleep_ms(50)
  14.     # Turn off LEDs
  15.     if c4002.set_run_led_state(c4002.LED_OFF):
  16.         print("Set run led success!")
  17.     else:
  18.         print("Set run led failed!")
  19.     if c4002.set_out_led_state(c4002.LED_OFF):
  20.         print("Set out led success!")
  21.     else:
  22.         print("Set out led failed!")
  23.     # Set resolution mode (80cm resolution, supports 15 distance gates)
  24.     if c4002.set_resolution_mode(c4002.RESOLUTION_80CM):
  25.         print("Set resolution mode success!")
  26.     else:
  27.         print("Set resolution mode failed!")
  28.     # Set detection range 0-1100cm
  29.     if c4002.set_detect_range(0, 1100):
  30.         print("Set detect range success!")
  31.     else:
  32.         print("Set detect range failed!")
  33.     # Set light threshold (0 = always detect regardless of light)
  34.     if c4002.set_light_thresh(0):
  35.         print("Set light threshold success!")
  36.     else:
  37.         print("Set light threshold failed!")
  38.     # Enable all distance gates
  39.     gate_state = [
  40.         c4002.C4002_ENABLE, c4002.C4002_ENABLE, c4002.C4002_ENABLE,
  41.         c4002.C4002_ENABLE, c4002.C4002_ENABLE, c4002.C4002_ENABLE,
  42.         c4002.C4002_ENABLE, c4002.C4002_ENABLE, c4002.C4002_ENABLE,
  43.         c4002.C4002_ENABLE, c4002.C4002_ENABLE, c4002.C4002_ENABLE,
  44.         c4002.C4002_ENABLE, c4002.C4002_ENABLE, c4002.C4002_ENABLE,
  45.     ]
  46.     if c4002.configure_gate(c4002.MOTION_DISTANCE_GATE, gate_state):
  47.         print("Enable motion distance gate success!")
  48.     if c4002.configure_gate(c4002.PRESENCE_DISTANCE_GATE, gate_state):
  49.         print("Enable presence distance gate success!")
  50.     # Set report period (10 * 0.1s = 1s)
  51.     if c4002.set_report_period(10):
  52.         print("Set report period success!")
  53. def loop():
  54.     ret_result = c4002.get_note_info()
  55.     if ret_result.note_type == c4002.RESULT:
  56.         print("------- Get all results --------")
  57.         # Light intensity (lux)
  58.         light = c4002.get_light_intensity()
  59.         print("Light: {:.2f} lux".format(light))
  60.         # Target state
  61.         target_state = c4002.get_target_state()
  62.         print("Target state: ", end="")
  63.         if target_state == c4002.NO_TARGET:
  64.             print("No Target")
  65.         elif target_state == c4002.PRESENCE:
  66.             print("Static Presence")
  67.         elif target_state == c4002.MOTION:
  68.             print("Motion")
  69.         # Presence countdown
  70.         presence_count_down = c4002.get_presence_count_down()
  71.         print("Presence countdown: {} s".format(presence_count_down))
  72.         # Presence target info
  73.         presence_target = c4002.get_presence_target_info()
  74.         print("Presence distance: {:.2f} m".format(presence_target.distance))
  75.         print("Presence energy: {}".format(presence_target.energy))
  76.         # Motion target info
  77.         motion_target = c4002.get_motion_target_info()
  78.         print("Motion distance: {:.2f} m".format(motion_target.distance))
  79.         print("Motion energy: {}".format(motion_target.energy))
  80.         print("Motion speed: {:.2f} m/s".format(motion_target.speed))
  81.         print("Motion direction: ", end="")
  82.         if motion_target.direction == c4002.AWAY:
  83.             print("Away!")
  84.         elif motion_target.direction == c4002.APPROACHING:
  85.             print("Approaching!")
  86.         elif motion_target.direction == c4002.NODIRECTION:
  87.             print("No Direction!")
  88.         print("--------------------------------")
  89. def main():
  90.     setup()
  91.     while True:
  92.         loop()
  93.         time.sleep_ms(100)
  94. if __name__ == "__main__":
  95.     main()
复制代码
如上述代码所示,其代码的输出和Arduino的库完全保持一致。 其输出的结果如下。

C4002在货运停车场景中的应用图6

3D打印外壳

由于之前用于快速测试使用的是杜邦线把PICO进行连接的,在测试的时候经常会出现倾斜的情况。于是我便使用Fusion 360来简单的设计了一个外壳,来把传感器和PIco使用热熔胶固定

C4002在货运停车场景中的应用图7

USB-Typec面

C4002在货运停车场景中的应用图8
成品如下所示

C4002在货运停车场景中的应用图9

Usb-typeC口方向

C4002在货运停车场景中的应用图10


测试
然后进行测试,我这里测试的时候是将开发板固定到了离我距离50CM的地方。

C4002在货运停车场景中的应用图11


接近

C4002在货运停车场景中的应用图12

远离

C4002在货运停车场景中的应用图13

测试起来精度还是非常不错的,但是似乎数据有一点延迟,可以查看我第二个截图中的数据,当前OUT已经输出了高电平,但是移动的状态还是静止,等到下一次数据更新的话就恢复了。当然也有可能是我代码写的不是很好。

额外补充

相信大家也注意到了上述多了一个GOIO2的输出状态,这是因为我将C4002传感器的OUT接到了GPIO2上。需要注意的是由于这个IO为可以配置的IO,所以它一共支持三种输出的方式(这也是这个传感器强大的一点)



C4002在货运停车场景中的应用图14

其中模式一为: 只有目标运动的时候才输出高电平,模式二为只有目标静止的时候才输出高电平。 模式三为只有目标静止和运动的时候才输出高电平。可以通过下面的函数来改变OUT的输出状态。
  1. c4002.set_out_pin_mode(c4002.OUT_PIN_MODE1)
复制代码


带有OUT输出的main.py 代码如下所示
  1. import time
  2. import machine
  3. # Import the Pico library
  4. from DFRobot_C4002 import DFRobot_C4002, PresenceTarget, MotionTarget
  5. # Initialize UART0 on GP0(TX) and GP1(RX)
  6. # Default baudrate: 115200
  7. c4002 = DFRobot_C4002(uart_num=0, baud=115200, tx=0, rx=1)
  8. # Initialize GPIO2 as input pin for radar output
  9. radar_output_pin = machine.Pin(2, machine.Pin.IN, machine.Pin.PULL_DOWN)
  10. c4002.set_out_pin_mode(c4002.OUT_PIN_MODE1)
  11. def setup():
  12.     while c4002.begin() != True:
  13.         print("C4002 begin failed!")
  14.         time.sleep_ms(500)
  15.     print("C4002 begin success!")
  16.     time.sleep_ms(50)
  17.     # Turn off LEDs
  18.     if c4002.set_run_led_state(c4002.LED_OFF):
  19.         print("Set run led success!")
  20.     else:
  21.         print("Set run led failed!")
  22.     if c4002.set_out_led_state(c4002.LED_OFF):
  23.         print("Set out led success!")
  24.     else:
  25.         print("Set out led failed!")
  26.     # Set resolution mode (80cm resolution, supports 15 distance gates)
  27.     if c4002.set_resolution_mode(c4002.RESOLUTION_80CM):
  28.         print("Set resolution mode success!")
  29.     else:
  30.         print("Set resolution mode failed!")
  31.     # Set detection range 0-1100cm
  32.     if c4002.set_detect_range(0, 1100):
  33.         print("Set detect range success!")
  34.     else:
  35.         print("Set detect range failed!")
  36.     # Set light threshold (0 = always detect regardless of light)
  37.     if c4002.set_light_thresh(0):
  38.         print("Set light threshold success!")
  39.     else:
  40.         print("Set light threshold failed!")
  41.     # Enable all distance gates
  42.     gate_state = [
  43.         c4002.C4002_ENABLE, c4002.C4002_ENABLE, c4002.C4002_ENABLE,
  44.         c4002.C4002_ENABLE, c4002.C4002_ENABLE, c4002.C4002_ENABLE,
  45.         c4002.C4002_ENABLE, c4002.C4002_ENABLE, c4002.C4002_ENABLE,
  46.         c4002.C4002_ENABLE, c4002.C4002_ENABLE, c4002.C4002_ENABLE,
  47.         c4002.C4002_ENABLE, c4002.C4002_ENABLE, c4002.C4002_ENABLE,
  48.     ]
  49.     if c4002.configure_gate(c4002.MOTION_DISTANCE_GATE, gate_state):
  50.         print("Enable motion distance gate success!")
  51.     if c4002.configure_gate(c4002.PRESENCE_DISTANCE_GATE, gate_state):
  52.         print("Enable presence distance gate success!")
  53.     # Set report period (10 * 0.1s = 1s)
  54.     if c4002.set_report_period(10):
  55.         print("Set report period success!")
  56. def get_gpio2_status():
  57.     """读取GPIO2的状态并返回描述"""
  58.     pin_value = radar_output_pin.value()
  59.     if pin_value == 1:
  60.         return "HIGH (检测到目标)"
  61.     else:
  62.         return "LOW (未检测到目标)"
  63. def loop():
  64.     ret_result = c4002.get_note_info()
  65.     if ret_result.note_type == c4002.RESULT:
  66.         print("------- Get all results --------")
  67.         # Light intensity (lux)
  68.         light = c4002.get_light_intensity()
  69.         print("Light: {:.2f} lux".format(light))
  70.         # Target state
  71.         target_state = c4002.get_target_state()
  72.         print("Target state: ", end="")
  73.         if target_state == c4002.NO_TARGET:
  74.             print("No Target")
  75.         elif target_state == c4002.PRESENCE:
  76.             print("Static Presence")
  77.         elif target_state == c4002.MOTION:
  78.             print("Motion")
  79.         # Presence countdown
  80.         presence_count_down = c4002.get_presence_count_down()
  81.         print("Presence countdown: {} s".format(presence_count_down))
  82.         # Presence target info
  83.         presence_target = c4002.get_presence_target_info()
  84.         print("Presence distance: {:.2f} m".format(presence_target.distance))
  85.         print("Presence energy: {}".format(presence_target.energy))
  86.         # Motion target info
  87.         motion_target = c4002.get_motion_target_info()
  88.         print("Motion distance: {:.2f} m".format(motion_target.distance))
  89.         print("Motion energy: {}".format(motion_target.energy))
  90.         print("Motion speed: {:.2f} m/s".format(motion_target.speed))
  91.         print("Motion direction: ", end="")
  92.         if motion_target.direction == c4002.AWAY:
  93.             print("Away!")
  94.         elif motion_target.direction == c4002.APPROACHING:
  95.             print("Approaching!")
  96.         elif motion_target.direction == c4002.NODIRECTION:
  97.             print("No Direction!")
  98.         # GPIO2 status
  99.         print("GPIO2 Output: {}".format(get_gpio2_status()))
  100.         print("--------------------------------")
  101. def main():
  102.     setup()
  103.     while True:
  104.         loop()
  105.         time.sleep_ms(10)
  106. if __name__ == "__main__":
  107.     main()
复制代码



总结

总体测试下来C4002的可配置性、精度、实用性、易用度都表现不错。在传感器参数输出的方面上支持多种格式的数据输出,在不同应用场景下支持简单到负责的传感器读取模式(配置输出寄存器读取OUT或者直接解码UART)。由于灵活性较高,相信在应用选型上也会占有一席之地。



附件


下载附件code.zip

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

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4 备案 沪公网安备31011502402448

© 2013-2026 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail