在当今的项目开发中,无论是大学生的毕设,研究生的课题研究,还是小型仪器设备的控制应用,大家都希望能找到一款功能强大、操作简单的控制主机。而 Python Controller 正是这样一款既具备多功能,又能轻松上手的利器!
Python Controller 作为一种高度灵活且功能强大的硬件模块,不仅适用于各种类型的项目开发,还能在控制系统架构中扮演多种角色。具体来说,它可以作为上位机控制系统的一部分,通过can和485总线控制从机,也可以充当IO控制,PWM输出等功能的中转设备,为整个系统提供高效的管理和协调机制。这些特性使得Python Controller在很多应用场景中可以替代PLC或者工控机,但是却具有前者无可比拟的方便性和易用性,可以极大提高项目的实施速度。
今天,我们将通过一个简单易懂的教程,带你快速上手Python Controller开发,解锁更多玩法!
查看使用手册上的接口说明,根据需要接好线,并使用 type-c 数据线将 Python Controller 连接到电脑。
Micropython 的原生 IDE 是 Thonny,但是该 IDE 界面单一且没有代码补全提示等功能,为了获得更好的编程体验,本教程使用 VSCode 编辑器,安装插件以提供 Micropython 编程支持。
打开 VSCode,在插件管理里搜索并安装 MicroPico 插件:
打开一个文件夹作为 Micropython 项目,然后按 Ctrl+Shift+P 打开命令搜索框,搜索 MicroPico:Configure MicroPico Project 或 MicroPico:Setup MicroPico Project,然后点击配置项目:
当右下角显示Project configuration complete!即代表项目配置成功,同时左下角也会显示MicroPico相关操作
点击左下角的All commands打开MicroPico命令搜索框,或按Ctrl+Shift+P打开命令搜索框,搜索MicroPico:Download project from Pico,然后点击搜索结果开始下载项目
右下角显示Project downloaded.即代表下载完成,同时在项目里面也能看到下载下来的文件
代码写好后,右键点击代码文件,在右键菜单中点击Run current file on Pico或直接点击左下角的Run即可在Python Controller上运行该程序,在输出窗口中可以查看代码运行输出
当项目写完后,需要将主函数代码文件命名为main.py,然后将项目相关文件都通过Upload file to Pico或Upload Project to Pico功能上传到Python Controller中才能让设备上电自动运行该项目
import sys
import pylibrary
import time
class cantest:
def __init__(self):
#初始化一个波特率位125kbps的can对象
self.can = pylibrary.can(125)
def recive_handler(self, pin):
#数据接收检测
is_recive, channel = self.can.check_message()
if is_recive:
#读取数据
frame_id, recive_data = self.can.receive(channel)
hex_data = [f'{x:02x}' for x in recive_data]
sys.stdout.write(f"帧ID: {hex(frame_id)}, 数据: {hex_data}\n")
#发送数据
self.can.send(frame_id, len(recive_data), recive_data)
if __name__ == "__main__":
cantest = cantest()
#注册一个can接收中断,中断处理函数为cantest.recive_handler(pin)
cantest.can.recive_interrupt_init(cantest.recive_handler)
while 1:
time.sleep(5)
import pylibrary
import sys
class rs485test:
def __init__(self):
#初始化一个rs485对象,使用通道1,波特率设置为9600
self.rs485 = pylibrary.rs485(1, 9600)
#默认设置为接收模式
self.rs485.set_recive_mode()
def recive_handler(self):
#直到有接收到数据才继续向下执行
while not self.rs485.is_recive_data():
pass
#读取数据
recive_data = self.rs485.read()
sys.stdout.write(f"接收: {recive_data}\n")
#设置为发送模式
self.rs485.set_send_mode()
#发送数据
self.rs485.write(recive_data)
#直到发送完成才继续向下执行
while not self.rs485.is_send_success():
pass
#设置为接收模式
self.rs485.set_recive_mode()
if __name__ == "__main__":
rs485test = rs485test()
while 1:
rs485test.recive_handler()
import pylibrary as pl
#初始化一个spi对象,极性为0,字节数为8
spi = pl.spi(10000, 0, 0, 8)
#开启spi通信,即拉低CS
spi.start()
#发送并读取数据
value = spi.transmit(2, [0x01, 0x02])
#关闭SPI通信,即拉高CS
spi.stop()
import pylibrary
import time
class mtest:
def __init__(self):
#初始化一个motor对象,使用M1接口
self.motor = pylibrary.motor(1)
if __name__ == "__main__":
mtest = mtest()
#设置方向为正向
mtest.motor.set_direction(1)
#以50%的速度开始转动,即给定电压为最大电压的50%
mtest.motor.start(50)
time.sleep(3)
#停止电机转动
mtest.motor.stop()
time.sleep(1)
#设置方向为反向
mtest.motor.set_direction(0)
#以100%的速度开始转动,即给定电压为最大电压的100%
mtest.motor.start(100)
time.sleep(3)
#停止电机转动
mtest.motor.stop()
import pylibrary
import sys
import time
class adctest:
def __init__(self):
#初始化一个adc对象
self.adc = pylibrary.adc()
if __name__ == "__main__":
adctest = adctest()
while 1:
#读取ADC的值
adc_value = adctest.adc.read()
#以输入为4-20mA为例,计算实际电流值
v_value = (adc_value * 16 / 65535) + 4
sys.stdout.write(f"电流为: {v_value}mA\n")
time.sleep(1)
import sys
import pylibrary
import time
class pwmtest:
def __init__(self):
#初始化一个pwm,使用通道1
self.pwm = pylibrary.pwm(1)
#设置频率为10Hz
self.pwm.set_freq(10)
#设置占空比为50%
self.pwm.set_dutycycle(50)
if __name__ == "__main__":
pwmtest = pwmtest()
while 1:
sys.stdout.write("pwm1输出\n")
time.sleep(5)
import sys
import pylibrary
import time
class gpiotest:
def __init__(self):
#初始化一个gpio对象,使用GPIO-1,设置为输出模式
self.gpio = pylibrary.gpio(1, pylibrary.gpio.OUT)
#输出低电平
self.gpio.bit_reset()
if __name__ == "__main__":
gpiotest = gpiotest()
while 1:
#输出高电平
gpiotest.gpio.bit_set()
sys.stdout.write("输出高电平\n")
time.sleep(3)
#输出低电平
gpiotest.gpio.bit_reset()
sys.stdout.write("输出低电平\n")
time.sleep(3)