Skip to content

Conversation

@Donny9
Copy link
Contributor

@Donny9 Donny9 commented Jul 21, 2022

Summary

Driver/sensor enhance feature:

  • Support pub-sub mechanism to mange all sensor(physical sensor, abstract sensor, rpmsg sensor).
  • Support user to register abstract sensor by /dev/usensor node in userspace.
  • Support remote core to advertise sensor event to other core, and support remote core to subscribe sensor event from other core, so support multi-core user to access sensor event by rpmsg.
  • Support down-sample.
  • Allow multi users to access the same sensor device simultaneously.
  • Get real state of sensor device by cmd SNIOC_GET_STATE for users.
  • Get update state since last read by poll without timeout for users.
  • Sensor device will be activated when first user open and will close when last user closed.
  • When multi users to access device, driver always set the minimum sampling interval and latency to the sensor device and allow downsampled for users above the minimum sampling interval.
  • The circbuffer will overwrite old data when buffer is full, so if users don't read data soon, data will be lost, and the oldest data in circbuffer are returned to the users.
  • Always read the last data in the circbuffer as initial value for new users when the sensor device has not yet generated new data.
  • when user uses poll, if subscription interval is satisfied, the POLLIN events is returned for each users.
  • When new user generate or the state of sensor device changed, the POLLPRI will notify to all users.
  • Support multi advertisers to subscribe their own data as loop test.

Refer to:
https://docs.px4.io/v1.12/en/middleware/uorb.html

Impact

  • Provides sensor cross-core access capability.
  • Provides a subscription and publish-based mechanism to access sensors.

Testing

Vela team CI and unit test.

@Donny9
Copy link
Contributor Author

Donny9 commented Jul 21, 2022

@jihandong Please push all uorb relate commit to apps, thanks!

@Donny9 Donny9 force-pushed the sensor branch 2 times, most recently from 914f894 to eeeec1c Compare July 21, 2022 08:24
@Donny9
Copy link
Contributor Author

Donny9 commented Jul 21, 2022

This PR apache/nuttx-apps#1228 need merge to fix ci issue:

Error: sensortest.c:136:27: error: dereferencing pointer to incomplete type 'struct sensor_event_ots'
  136 |          "\n", name, event->timestamp, event->x, event->y);
      |                           ^~
sensortest.c: In function 'print_valf':
Error: sensortest.c:143:21: error: dereferencing pointer to incomplete type 'struct sensor_event_prox'
  143 |          name, event->timestamp, event->proximity);
      |                     ^~
sensortest.c: In function 'print_valf2':
Error: sensortest.c:150:21: error: dereferencing pointer to incomplete type 'struct sensor_event_baro'
  150 |          name, event->timestamp, event->pressure, event->temperature);
      |                     ^~
sensortest.c: In function 'print_valf3':
Error: sensortest.c:157:21: error: dereferencing pointer to incomplete type 'struct sensor_event_rgb'
  157 |          name, event->timestamp, event->r, event->g, event->b);
      |                     ^~
sensortest.c: In function 'print_ppgd':
Error: sensortest.c:165:21: error: dereferencing pointer to incomplete type 'struct sensor_event_ppgd'
  165 |          name, event->timestamp, event->ppg[0], event->ppg[1],
      |                     ^~
sensortest.c: In function 'print_ppgq':
Error: sensortest.c:176:21: error: dereferencing pointer to incomplete type 'struct sensor_event_ppgq'
  176 |          name, event->timestamp, event->ppg[0], event->ppg[1], event->ppg[2],
      |                     ^~
sensortest.c: In function 'print_gps':
Error: sensortest.c:189:21: error: dereferencing pointer to incomplete type 'struct sensor_event_gps'
  189 |          name, event->timestamp, event->time_utc, event->latitude,
      |                     ^~
sensortest.c: In function 'print_gps_satellite':

we can test sensor driver by uorb_listener, example:

ap> uorb_listener -r 25 -n 10 sensor_baro
[07/21 10:29:25.711242] [1471] [ap] 
Mointor objects num:1
[07/21 10:29:25.711914] [1471] [ap] object_name:sensor_baro, object_instance:0
[07/21 10:29:25.776794] [1471] [ap] sensor_baro:        timestamp: 108973761230 (22156 us ago) temperature: 27.20 pressure: 994.26
[07/21 10:29:25.817199] [1471] [ap] sensor_baro:        timestamp: 108973801635 (22095 us ago) temperature: 27.20 pressure: 994.25
[07/21 10:29:25.857421] [1471] [ap] sensor_baro:        timestamp: 108973842041 (21911 us ago) temperature: 27.21 pressure: 994.26
[07/21 10:29:25.898376] [1471] [ap] sensor_baro:        timestamp: 108973882507 (22400 us ago) temperature: 27.21 pressure: 994.24
[07/21 10:29:25.938354] [1471] [ap] sensor_baro:        timestamp: 108973922912 (22034 us ago) temperature: 27.22 pressure: 994.27
[07/21 10:29:25.978515] [1471] [ap] sensor_baro:        timestamp: 108973963317 (21729 us ago) temperature: 27.22 pressure: 994.26
[07/21 10:29:26.019409] [1471] [ap] sensor_baro:        timestamp: 108974003784 (22155 us ago) temperature: 27.22 pressure: 994.26
[07/21 10:29:26.059814] [1471] [ap] sensor_baro:        timestamp: 108974044250 (22095 us ago) temperature: 27.22 pressure: 994.26
[07/21 10:29:26.101806] [1471] [ap] sensor_baro:        timestamp: 108974084716 (23682 us ago) temperature: 27.22 pressure: 994.26
[07/21 10:29:26.140869] [1471] [ap] sensor_baro:        timestamp: 108974125305 (22094 us ago) temperature: 27.22 pressure: 994.27
[07/21 10:29:26.142333] [1471] [ap] Object name:sensor_baro0, recieved:10
[07/21 10:29:26.143066] [1471] [ap] Total number of received Message:10/10

@Donny9
Copy link
Contributor Author

Donny9 commented Jul 22, 2022

uorb PR:apache/nuttx-apps#1230

@xiaoxiang781216
Copy link
Contributor

@Donny9 the PR is too large, could you split the unrelated patch to the new PR?

Donny9 added 13 commits July 30, 2022 16:20
Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
If enable SNIOC_READLAST, sensor_read will return 0
when there is no new data until last read.

Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
…lock

Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
using recursive mutex to fix this issue.

Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
Donny9 and others added 6 commits July 30, 2022 23:36
Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
CHAMPION-6418
rptun sensor:
[ap] [ 5] [<0x2c352c68>] arm_switchcontext+0xc/0x10
[ap] [ 5] [<0x2c322eae>] sem_wait+0x5a/0xbc
[ap] [ 5] [<0x2c3376cc>] sensor_rpmsg_find_dev+0x8/0x78 (wait g_lock)
[ap] [ 5] [<0x2c338d96>] sensor_rpmsg_suback_handler+0xa/0x4c
[ap] [ 5] [<0x2c7630f6>] rpmsg_virtio_rx_callback+0xba/0x1b4
[ap] [ 5] [<0x2c762788>] rproc_virtio_notified+0x44/0x5c
[ap] [ 5] [<0x2c762154>] remoteproc_get_notification+0x1c/0x2c
[ap] [ 5] [<0x2c335576>] rptun_thread+0x6e/0x164
[ap] [ 5] [<0x2c324aba>] nxtask_start+0x3a/0x60

apps:(note: ipc buffer is busy, it's not enough for dual core.)
[ap] [26] [<0x2c323a5e>] nxsig_timedwait+0x8a/0x184
[ap] [26] [<0x2c324344>] nxsig_nanosleep+0x34/0xa4
[ap] [26] [<0x2c324446>] nxsig_usleep+0x26/0x38
[ap] [26] [<0x2c76338e>] rpmsg_virtio_get_tx_payload_buffer+0x5e/0x100 (wait ipc buffer)
[ap] [26] [<0x2c33775a>] sensor_rpmsg_advsub_one+0x1e/0xdc (get g_lock)
[ap] [26] [<0x2c338312>] sensor_rpmsg_open+0xee/0x178
[ap] [26] [<0x2c3365bc>] sensor_open+0x50/0x234
[ap] [26] [<0x2c749cb8>] open+0xc8/0x194
[ap] [26] [<0x2c375324>] orb_subscribe_multi+0x1c/0x94

Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
note: replace dev lock by upper driver lock

rptun:
sem_wait
sched/semaphore/sem_wait.c:273
nxmutex_lock
/home/neo/projects/monkey/nuttx/include/nuttx/mutex.h:161
sensor_close (wait upper driver lock)
drivers/sensors/sensor.c:509
sensor_rpmsg_free_stub (get dev lock)
drivers/sensors/sensor_rpmsg.c:546
sensor_rpmsg_unsub_handler
drivers/sensors/sensor_rpmsg.c:1025
rptun_thread

apps:
sem_wait
sched/semaphore/sem_wait.c:273
sensor_rpmsg_close (wait dev lock)
drivers/sensors/sensor_rpmsg.c:613
sensor_close (get upper driver lock)
drivers/sensors/sensor.c:512

Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
Signed-off-by: jihandong <jihandong@xiaomi.com>
Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
jihandong and others added 3 commits July 30, 2022 23:36
1. about interval:
  If interval is not set, generation is increased by 1 along with
  publish and copy, multi-copy is continuous.

  If interval is set, pick proper samples from buffer based on
  mainline/user generation, multi-copy is one-by-one.

2. about bufferpos:
  user->bufferpos always point to next position to check.

       data user last read
  ----------v--------------------------
  |        |        |        |
  -------------------^-----------------
                 bufferpos

  If buffer is full, bufferpos point to buffer.head

Examples:
  If a buffer contains 4 samples, newest generatoin is 40.

  -------------------------------------
  |10      |20      |30      |40
  ------------------------------^------
                                |
   if user's next generation is 42, notify user to copy No.40 sample,
                because 42 is closer to 40 than 50.

  -------------------------------------
  |10      |20      |30      |40
  ----------------------------------^--
                                    |
   if user's next generation is 48, do not notify user,
         because 48 is closer to 50, which is next mainline sample.

Signed-off-by: jihandong <jihandong@xiaomi.com>
Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
task1:
nuttx/arch/arm/src/common/arm_blocktask.c:151
nxsem_wait
nuttx/arch/arm/src/../../../sched/semaphore/sem_wait.c:155 (discriminator 2)
sem_wait
nuttx/arch/arm/src/../../../sched/semaphore/sem_wait.c:273
nxmutex_lock.lto_priv.2
nuttx/include/nuttx/mutex.h:161
sensor_rpmsg_push_event_one
nuttx/arch/arm/src/../../../drivers/sensors/sensor_rpmsg.c:748
up_unblock_task
nuttx/arch/arm/src/common/arm_unblocktask.c:75 (discriminator 2)
sensor_rpmsg_push_event
nuttx/arch/arm/src/../../../drivers/sensors/sensor_rpmsg.c:858
write
orb_publish_multi
nuttx/arch/arm/src/../../../../apps/system/uorb/uORB/uORB.c:188

task2:
nxrmutex_lock.isra.0
sensor_read
nuttx/arch/arm/src/../../../drivers/sensors/sensor.c:753
sem_post
nuttx/arch/arm/src/../../../sched/semaphore/sem_post.c:224
nxmutex_unlock.lto_priv.1
nuttx/include/nuttx/mutex.h:259
sensor_rpmsg_push_event_one
nuttx/arch/arm/src/../../../drivers/sensors/sensor_rpmsg.c:787
nxmutex_unlock.lto_priv.1
nuttx/include/nuttx/mutex.h:259
nuttx/arch/arm/src/../../../drivers/sensors/sensor.c:969
sensor_rpmsg_alloc_stub
nuttx/arch/arm/src/../../../drivers/sensors/sensor_rpmsg.c:549
sensor_rpmsg_advack_handler

Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
@xiaoxiang781216 xiaoxiang781216 merged commit 78381f9 into apache:master Aug 1, 2022
@xiaoxiang781216 xiaoxiang781216 mentioned this pull request Jan 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants