static void sensor_data_task(void *args) { xQueueHandle sensor_queue = (xQueueHandle)args; static D_MsgData msg_data; static D_SensorData sensor_data; static D_SensorData sensor_data1; static D_S16 sensor_value; while(1) { if(xQueueReceive(sensor_queue, &msg_data,( TickType_t ) pdMS_TO_TICKS(1000))) { if (msg_data.msg_type == D_MSG_TYPE_AD_DATA) { d_drv_exit_low_power(); /*get pressure data*/ if (!d_drv_is_low_power()) { d_adc_sample_data(D_ADC_CHANNEL_SENSOR_DATA, &sensor_value); //D_DUMP("[sensor_data_task]: batterry_ad_value = %d", sensor_value); /*battery level check*/ if(sensor_value > D_ADC_BAT_HIPOWER) { sensor_data.battery_level = 100; } else if(sensor_value < D_ADC_BAT_LOPOWER) { sensor_data.battery_level = 0; } else { sensor_data.battery_level = (sensor_value - D_ADC_BAT_LOPOWER)*100/ (D_ADC_BAT_HIPOWER - D_ADC_BAT_LOPOWER); } D_DUMP("[sensor_data_task]: batterry_ad_value = %d, battery_level = %d", sensor_value, sensor_data.battery_level); sensor_data.sensor_type = D_SENSOR_TEMPERATURE; sensor_data.sensor_info.value = d_ds18b20_get_data(); warn_check(D_SENSOR_TEMPERATURE, &sensor_data.sensor_info); //D_DUMP("[sensor_data_task]: temperature_sensor_value = %d", sensor_data.sensor_info.value); sensor_data1.battery_level = sensor_data.battery_level; sensor_data1.sensor_type = D_SENSOR_VALVE; sensor_data1.sensor_info.value = (((D_U8)config_info.valve_controlled_type) << 4) |((D_U8)(10 - config_info.valve_status)); //D_DUMP("[sensor_data_task]: valve_sensor_value = %d", sensor_data1.sensor_info.value); } if (sensor_data_event_user_callback != NULL) { sensor_data_event_user_callback(&sensor_data); nrf_delay_us(1000); sensor_data_event_user_callback(&sensor_data1); } d_drv_enter_low_power(); } } } } 啥意思
时间: 2023-08-13 12:04:44 浏览: 160
这段代码是一个名为"sensor_data_task"的静态函数,它是一个任务函数。在这个任务中,它通过接收一个队列中的数据来获取传感器数据。首先,它会检查接收到的消息类型是否为AD数据类型。如果是AD数据类型,则会执行一系列操作。
首先,它会退出低功耗模式,然后获取压力传感器的数据值。接着,它会根据获取的AD值来判断电池电量的等级,并将结果保存到sensor_data结构体的battery_level字段中。
然后,它会设置sensor_data结构体的sensor_type字段为D_SENSOR_TEMPERATURE,并调用d_ds18b20_get_data()函数来获取温度传感器的数据值,并将结果保存到sensor_info结构体的value字段中。紧接着,它会调用warn_check()函数来检查温度传感器的数据,并进行相应的处理。
接下来,它会设置sensor_data1结构体的battery_level字段为sensor_data结构体中保存的电池电量值,并设置sensor_data1结构体的sensor_type字段为D_SENSOR_VALVE。然后,它会将阀门控制类型和阀门状态信息保存到sensor_info结构体的value字段中。
接着,它会检查是否注册了sensor_data_event_user_callback回调函数。如果注册了,则会依次调用该回调函数,并将sensor_data和sensor_data1作为参数传入。最后,它会进入低功耗模式。
总体来说,这段代码的功能是获取传感器数据,并通过回调函数将数据传递给其他模块进行处理。
相关问题
如果要把下列代码中的yaw_current,depth_current,temperature变成可以用rqt_configure动态调参的参数,应该如何对他们进行动态参数重配置呢:#! /usr/bin/env python import rospy from guidance_navigation_control.msg import controlCommand from guidance_navigation_control.msg import sensorInfo_actuatorStatus def gnc_data(gnc_data): print (gnc_data) while True: rospy.init_node('SENSORS_ACTUATORS') sensorActuator_pub = rospy.Publisher('sensorInfo_actuatorStatus', sensorInfo_actuatorStatus, queue_size=10) rospy.Subscriber('controlCommand', controlCommand, gnc_data) rate = rospy.Rate(10) final_message = sensorInfo_actuatorStatus() while not rospy.is_shutdown(): final_message.yaw_current = 17 final_message.depth_current = 21 final_message.temperature = 72 final_message.thruster_values[0] = 1600 final_message.thruster_values[1] = 1300 final_message.thruster_values[2] = 1700 final_message.thruster_values[3] = 1200 final_message.thruster_values[4] = 1500 final_message.thruster_values[5] = 1800 final_message.stabilized = True sensorActuator_pub.publish(final_message) rate.sleep() else: exit()
要将yaw_current、depth_current和temperature变成可以用rqt_configure动态调参的参数,可以使用dynamic_reconfigure库。
首先,需要在ROS包的cfg目录下创建一个名为“configs.cfg”的动态参数重配置文件,并在其中定义需要动态重配置的参数。例如:
```
# Define dynamic parameters
int yaw_current
int depth_current
int temperature
```
接下来,在代码中导入dynamic_reconfigure库,并在回调函数中将接收到的动态参数值赋值给final_message中对应的变量。例如:
```
import rospy
from guidance_navigation_control.msg import controlCommand
from guidance_navigation_control.msg import sensorInfo_actuatorStatus
from dynamic_reconfigure.server import Server
from my_package.cfg import configsConfig
final_message = sensorInfo_actuatorStatus()
def gnc_data(gnc_data):
print (gnc_data)
def dynamic_reconfig_callback(config, level):
final_message.yaw_current = config.yaw_current
final_message.depth_current = config.depth_current
final_message.temperature = config.temperature
return config
if __name__ == '__main__':
rospy.init_node('SENSORS_ACTUATORS')
sensorActuator_pub = rospy.Publisher('sensorInfo_actuatorStatus', sensorInfo_actuatorStatus, queue_size=10)
rospy.Subscriber('controlCommand', controlCommand, gnc_data)
srv = Server(configsConfig, dynamic_reconfig_callback)
rate = rospy.Rate(10)
while not rospy.is_shutdown():
final_message.thruster_values[0] = 1600
final_message.thruster_values[1] = 1300
final_message.thruster_values[2] = 1700
final_message.thruster_values[3] = 1200
final_message.thruster_values[4] = 1500
final_message.thruster_values[5] = 1800
final_message.stabilized = True
sensorActuator_pub.publish(final_message)
rate.sleep()
else:
exit()
```
最后,在launch文件中添加dynamic_reconfigure节点,让其运行configs.cfg文件。例如:
```
<node pkg="dynamic_reconfigure" type="dynamic_reconfigure" name="dynamic_reconfigure" args="configs_node_name configs.cfg" />
```
这样,就可以通过rqt_configure动态调参来修改yaw_current、depth_current和temperature的值了。
程序报错:Traceback (most recent call last): File "/tmp/jqcore/jqboson/jqboson/core/entry.py", line 373, in _run engine.start() File "/tmp/jqcore/jqboson/jqboson/core/engine.py", line 231, in start self._dispatcher.start() File "/tmp/jqcore/jqboson/jqboson/core/dispatcher.py", line 273, in start self._run_loop() File "/tmp/jqcore/jqboson/jqboson/core/dispatcher.py", line 240, in _run_loop self._loop.run() File "/tmp/jqcore/jqboson/jqboson/core/loop/loop.py", line 105, in run self._handle_queue() File "/tmp/jqcore/jqboson/jqboson/core/loop/loop.py", line 151, in _handle_queue message.callback(**message.callback_data) File "/tmp/jqcore/jqboson/jqboson/core/mds/market_data_subscriber.py", line 228, in broadcast consumer.send(market_data) File "/tmp/jqcore/jqboson/jqboson/core/mds/market_data_consumer_manager.py", line 59, in consumer_gen msg_callback() File "/tmp/jqcore/jqboson/jqboson/core/mds/market_data_consumer_manager.py", line 52, in msg_callback callback(market_data) File "/tmp/jqcore/jqboson/jqboson/core/mds/market_data_consumer_manager.py", line 122, in wrapper result = callback(*args, **kwargs) File "/tmp/jqcore/jqboson/jqboson/core/strategy.py", line 407, in _wrapper self._context.current_dt File "/tmp/strategy/user_code.py", line 60, in handle_data mancd, hist = mancd(context) UnboundLocalError: local variable 'mancd' referenced before assignment
报错信息显示 `UnboundLocalError: local variable 'mancd' referenced before assignment`,这是因为 `mancd` 函数在计算过程中出现了错误,导致其没有返回结果,而在调用 `mancd` 函数时,`mancd` 没有被正确赋值,因此出现了该错误。
可以尝试检查 `mancd` 函数的计算过程,查看是否有语法错误或逻辑错误。另外,建议在 `mancd` 函数中加入一些调试信息,以便更好地定位问题。例如:
```python
def mancd(context):
# 获取股票价格数据
prices = history(g.period, '1d', 'close')[context.stock]
# 计算macd指标
macd, signal, hist = talib.MACD(prices)
# 计算mancd指标
mancd, signal, hist = talib.MACDEXT(prices, fastperiod=12, slowperiod=26, signalperiod=9)
print('mancd:', mancd)
# 返回计算结果
return mancd, hist
```
这样,在出现问题时,就可以通过查看日志,定位问题所在。
阅读全文