本帖最后由 云天 于 2022-5-12 22:50 编辑
【行空板“行动”起来】
在Python教学过程中,从初级时在屏幕上画各种图形,中级控制LED灯、按键、温湿度等各种传感器,高级我个人认为要让实物动起来,如控制舵机、风扇等一直到小车。再加上摄像头,利用AI控制。
行空板是一款专为Python学习和使用设计的新一代国产开源硬件,采用单板计算机架构,集成LCD彩屏、WiFi蓝牙、多种常用传感器和丰富的拓展接口。同时,其自带Linux操作系统和Python环境,还预装了常用的Python库,让广大师生只需两步就能进行Python教学。
【硬件准备】
金手指:引脚编号兼容micro:bit, 19路独立I/O(支持1路I2C、1路UART、2路SPI、6路12位ADC、5路12位PWM)
2、电机驱动
TB6612FNG微型双路直流电机驱动模块
3、供电降压模块
给行空板稳定5V供电。
DC-DC降压模块6~14V转5V8A 模块输出5V/8A,这是一款40W大功率DC-DC降压型电源模块,电压输入范围是6-14V,最大输出5V/8A,大电流输出,支持所有5V主控板供电,转换效率极高。模块体型小巧,散热极强,可作为贴片元件模组焊接到PCB板上使用。
【硬件组装】
【程序编写】
1、连接物联网siot 利用Mind+图形模块,获取Python代码。
-
- # -*- coding: UTF-8 -*-
-
- # MindPlus
- # Python
- import siot
-
-
- siot.init(client_id="siot_120",server="192.168.137.4",port=1883,user="siot",password="dfrobot")
- siot.connect()
- siot.loop()
- siot.getsubscribe(topic="topic")
- siot.publish(topic="car/control", data="hello")
-
- while True:
- pass
-
复制代码
发布主题是为了在Siot新建项目“car”、设备“control”,主题为:"car/control"
2、订阅主题
自动生成代码中应改为: if (msg.payload.decode("utf-8") == 'G'):
-
-
- import siot
-
- # 事件回调函数
- def on_message_callback(client, userdata, msg):
- if (msg.payload.decode("utf-8") == 'G'):
- pass
-
-
- siot.init(client_id="siot_370",server="192.168.137.4",port=1883,user="siot",password="dfrobot")
- siot.connect()
- siot.loop()
- siot.set_callback(on_message_callback)
- siot.getsubscribe(topic="car/control")
-
- while True:
- pass
复制代码
3、测试控制电机
-
- # -*- coding: UTF-8 -*-
-
- # MindPlus
- # Python
- from pinpong.extension.unihiker import *
- from pinpong.board import Board,Pin
- import time
- from unihiker import GUI
- u_gui=GUI()
- Board().begin()
- p_p8_out=Pin(Pin.P8, Pin.OUT)
- p_p9_out=Pin(Pin.P9, Pin.OUT)
- p_p0_pwm=Pin(Pin.P0, Pin.PWM)
- p_p2_pwm=Pin(Pin.P2, Pin.PWM)
- xianshi=u_gui.draw_text(text="开始",x=50,y=100,font_size=50, color="#0000FF")
- while True:
-
- p_p8_out.write_digital(1)
- p_p0_pwm.write_analog(200) #PWM输出
- p_p9_out.write_digital(1)
- p_p2_pwm.write_analog(200) #PWM输出
- xianshi.config(text="前进")
- time.sleep(5)
-
- p_p8_out.write_digital(0)
- p_p0_pwm.write_analog(200) #PWM输出
- p_p9_out.write_digital(0)
- p_p2_pwm.write_analog(200) #PWM输出
- xianshi.config(text="后退")
- time.sleep(5)
-
复制代码
4、测试电机视频
【完整程序】
电机驱动板DIR1接P8,PWM1接P0,驱动右电机;电机驱动板DIR2接P9,PWM2接P2,驱动左电机;接收物联网命令,“G”前进、“S”停止、“B”后退、“L”向左、“R”向右。
-
- import siot
- from pinpong.extension.unihiker import *
- from pinpong.board import Board,Pin
- import time
- from unihiker import GUI
- u_gui=GUI()
- xianshi=u_gui.draw_text(text="开始",x=50,y=100,font_size=50, color="#0000FF")
- # 事件回调函数
- def on_message_callback(client, userdata, msg):
-
- if (msg.payload.decode("utf-8") == 'G'):
- p_p8_out.write_digital(1)
- p_p0_pwm.write_analog(500) #PWM输出
- p_p9_out.write_digital(1)
- p_p2_pwm.write_analog(500) #PWM输出
- xianshi.config(text="前进")
- if (msg.payload.decode("utf-8")== 'S'):
- p_p8_out.write_digital(1)
- p_p0_pwm.write_analog(0) #PWM输出
- p_p9_out.write_digital(1)
- p_p2_pwm.write_analog(0) #PWM输出
- xianshi.config(text="停止")
-
- if (msg.payload.decode("utf-8") == 'B'):
- p_p8_out.write_digital(0)
- p_p0_pwm.write_analog(200) #PWM输出
- p_p9_out.write_digital(0)
- p_p2_pwm.write_analog(200) #PWM输出
- xianshi.config(text="后退")
-
- if (msg.payload.decode("utf-8") == 'L'):
- p_p8_out.write_digital(1)
- p_p0_pwm.write_analog(500) #PWM输出
- p_p9_out.write_digital(0)
- p_p2_pwm.write_analog(0) #PWM输出
- xianshi.config(text="向左")
-
- if (msg.payload.decode("utf-8") == 'R'):
- p_p8_out.write_digital(0)
- p_p0_pwm.write_analog(0) #PWM输出
- p_p9_out.write_digital(1)
- p_p2_pwm.write_analog(500) #PWM输出
- xianshi.config(text="向右")
-
-
- siot.init(client_id="siot_192",server="192.168.137.4",port=1883,user="siot",password="dfrobot")
- siot.connect()
- siot.loop()
- siot.set_callback(on_message_callback)
- siot.getsubscribe(topic="car/control")
- Board().begin()
- p_p8_out=Pin(Pin.P8, Pin.OUT)
- p_p9_out=Pin(Pin.P9, Pin.OUT)
- p_p0_pwm=Pin(Pin.P0, Pin.PWM)
- p_p2_pwm=Pin(Pin.P2, Pin.PWM)
- while True:
- pass
复制代码
【演示视频】
|