最新版本密胺计量代码
This commit is contained in:
@ -51,11 +51,16 @@ class Controller:
|
||||
|
||||
def start(self):
|
||||
self.running = True
|
||||
|
||||
# 创建线程列表(包含5个并发任务)
|
||||
threads = [
|
||||
# 线程1:重量读取循环
|
||||
threading.Thread(target=self._weight_reading_loop),
|
||||
threading.Thread(target=self._control_loop),
|
||||
threading.Thread(target=self.network.start_server),
|
||||
# 线程4:振动控制
|
||||
threading.Thread(target=self._control_vibrate),
|
||||
# 线程5:看门狗线程(未使用thread()包装)
|
||||
self.watchdog
|
||||
]
|
||||
for t in threads:
|
||||
@ -65,26 +70,27 @@ class Controller:
|
||||
def _weight_reading_loop(self):
|
||||
while self.running:
|
||||
if self.sensor.read_weight():
|
||||
with self.sensor.lock:
|
||||
self.current_weight = self.sensor.current_weight
|
||||
with self.status_lock:
|
||||
with self.sensor.lock: # 进入上下文,获取锁 获取传感器数据锁
|
||||
self.current_weight = self.sensor.current_weight # 在锁保护下操作共享数据
|
||||
# 退出上下文,自动释放锁
|
||||
with self.status_lock: # 获取传感器状态锁
|
||||
self.network.status['current_weight'] = self.current_weight
|
||||
if self.current_weight >= self.network.status['target_weight']:
|
||||
self.network.status['measuring'] = False
|
||||
self.network.status['start_weighting'] = False
|
||||
self.network.status['weighting_isok'] = True
|
||||
self.network.status['weighting_isok'] = True # 标记称重完成
|
||||
|
||||
|
||||
if self.network.status['set_tare'] == True:
|
||||
if self.sensor.tare():
|
||||
self.network.status['set_tare_num_time'] += 1
|
||||
if self.network.status['set_tare'] == True: # 检查是否需要执行去皮操作
|
||||
if self.sensor.tare(): # 执行传感器去皮
|
||||
self.network.status['set_tare_num_time'] += 1 # 增加去皮计数器
|
||||
self.network.status['set_tare'] = False
|
||||
|
||||
# 主动获取重量请求处理
|
||||
if self.network.status['get_weight'] == True:
|
||||
with self.sensor.lock:
|
||||
self.network.status['current_weight'] = self.sensor.current_weight
|
||||
self.network.status['get_weight'] = False
|
||||
time.sleep(0.05) # 20Hz
|
||||
time.sleep(0.05) # 控制循环频率为20Hz(每秒20次循环)
|
||||
# time.sleep(0.1) # 20Hz
|
||||
|
||||
def _control_loop(self):
|
||||
@ -92,24 +98,24 @@ class Controller:
|
||||
with self.status_lock:
|
||||
measuring = self.network.status['measuring']
|
||||
target = self.network.status['target_weight']
|
||||
algorithm = self.network.status['algorithm']
|
||||
direction_control = self.network.status['direction_control']
|
||||
if direction_control== True: # 步进电机方向控制
|
||||
self.gpio._write_value(12, 1)
|
||||
algorithm = self.network.status['algorithm'] # 使用的控制算法类型
|
||||
direction_control = self.network.status['direction_control'] # 电机方向控制标志
|
||||
if direction_control == True: # 步进电机方向控制
|
||||
self.gpio._write_value(12, 1) # 电机方向
|
||||
self.network.status['direction_control'] = False
|
||||
|
||||
if measuring:
|
||||
self._select_algorithm(algorithm)
|
||||
self._select_algorithm(algorithm) # 选择控制算法
|
||||
speed, error = self.algorithm.calculate_speed(self.current_weight, target)
|
||||
rpm = speed # 假设速度单位直接对应RPM
|
||||
pulse_rate = (int(self.config['gpio']['pulse_per_rev']) * rpm) / 60
|
||||
self.gpio.set_speed(8, pulse_rate, error)
|
||||
rpm = speed # 假设速度单位直接对应RPM(转/分钟)
|
||||
pulse_rate = (int(self.config['gpio']['pulse_per_rev']) * rpm) / 60 # 计算脉冲频率
|
||||
self.gpio.set_speed(8, pulse_rate, error) # 电机驱动
|
||||
else:
|
||||
self.gpio.set_speed(8, 0, 0)
|
||||
while not self.running and not self.network.status['measuring']:
|
||||
time.sleep(0.1) # 短暂休眠,避免CPU占用过高
|
||||
|
||||
time.sleep(0.1)
|
||||
time.sleep(0.1) # 以10hz的频率运行
|
||||
|
||||
def _control_vibrate(self): # 振动控制
|
||||
while self.running:
|
||||
@ -124,7 +130,8 @@ class Controller:
|
||||
self.network.status['set_vibrate'] = False
|
||||
self.network.status['set_vibrate_time'] = 0
|
||||
self.network.status['vibrate_isok'] = True
|
||||
time.sleep(1)
|
||||
time.sleep(1) # 每秒检查一次振动请求
|
||||
|
||||
def _select_algorithm(self, name):
|
||||
if name == 'pid':
|
||||
params = self.config['algorithm']['params']['pid']
|
||||
@ -145,8 +152,8 @@ class Controller:
|
||||
|
||||
if not threads_alive:
|
||||
logger.critical("检测到线程异常,触发紧急停止!")
|
||||
self.emergency_stop()
|
||||
break
|
||||
self.emergency_stop() # 执行紧急停止操作
|
||||
break # 退出看门狗循环
|
||||
|
||||
time.sleep(5)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user