Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
alios-things
AliOS-Things
提交
e9e62567
A
AliOS-Things
项目概览
alios-things
/
AliOS-Things
11 个月 前同步成功
通知
62
Star
8
Fork
2
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
AliOS-Things
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
e9e62567
编写于
4月 29, 2022
作者:
E
ethan.lcz
提交者:
skylarCai
5月 02, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add motion API
Signed-off-by:
N
ethan.lcz
<
ethan.lcz@alibaba-inc.com
>
上级
5437cbe6
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
264 addition
and
0 deletion
+264
-0
haas_lib_bundles/python/libraries/motion/detections/fall_detection.py
...dles/python/libraries/motion/detections/fall_detection.py
+111
-0
haas_lib_bundles/python/libraries/motion/detections/tap_detection.py
...ndles/python/libraries/motion/detections/tap_detection.py
+106
-0
haas_lib_bundles/python/libraries/motion/motion.py
haas_lib_bundles/python/libraries/motion/motion.py
+47
-0
未找到文件。
haas_lib_bundles/python/libraries/motion/detections/fall_detection.py
0 → 100644
浏览文件 @
e9e62567
ITER_NUM
=
50
ACCELERATION_LOW_THREADHOLD
=
4
# 加速度变化下限阈值,越大越灵敏
ACCELERATION_UP_THREADHOLD
=
12
# 加速度变化上限阈值,越小越灵敏
ANGULAR_VELOCITY_LOW_THREADHOLD
=
1
# 角速度变化下限阈值,越小越灵敏
ANGULAR_VELOCITY_UP_THREADHOLD
=
40
# 角速度变化下限阈值,越大越灵敏
class
fall_detection
:
def
__init__
(
self
,
getData
):
self
.
ax_offset
=
0.0
self
.
ay_offset
=
0.0
self
.
az_offset
=
0.0
self
.
gx_offset
=
0.0
self
.
gy_offset
=
0.0
self
.
gz_offset
=
0.0
self
.
trigger1count
=
0
self
.
trigger2count
=
0
self
.
trigger1
=
False
self
.
trigger2
=
False
self
.
getData
=
getData
self
.
calibrate
()
def
calibrate
(
self
):
ax_sum
=
0.0
ay_sum
=
0.0
az_sum
=
0.0
gx_sum
=
0.0
gy_sum
=
0.0
gz_sum
=
0.0
for
num
in
range
(
0
,
ITER_NUM
):
data
=
self
.
getData
()
# 读取传感器信息值
ax_sum
+=
data
[
0
][
0
]
ay_sum
+=
data
[
0
][
1
]
az_sum
+=
data
[
0
][
2
]
gx_sum
+=
data
[
1
][
0
]
gy_sum
+=
data
[
1
][
1
]
gz_sum
+=
data
[
1
][
2
]
self
.
ax_offset
=
ax_sum
/
ITER_NUM
self
.
ay_offset
=
ay_sum
/
ITER_NUM
self
.
az_offset
=
az_sum
/
ITER_NUM
self
.
gx_offset
=
gx_sum
/
ITER_NUM
self
.
gy_offset
=
gy_sum
/
ITER_NUM
self
.
gz_offset
=
gz_sum
/
ITER_NUM
print
(
'Now you can start fall detection!'
)
def
detect
(
self
):
global
ACCELERATION_LOW_THREADHOLD
global
ACCELERATION_UP_THREADHOLD
global
ANGULAR_VELOCITY_LOW_THREADHOLD
global
ANGULAR_VELOCITY_UP_THREADHOLD
fall
=
False
data
=
self
.
getData
()
# 读取传感器信息值
AcX
=
data
[
0
][
0
]
AcY
=
data
[
0
][
1
]
AcZ
=
data
[
0
][
2
]
GyX
=
data
[
1
][
0
]
GyY
=
data
[
1
][
1
]
GyZ
=
data
[
1
][
2
]
ax
=
(
AcX
-
self
.
ax_offset
)
ay
=
(
AcY
-
self
.
ay_offset
)
az
=
(
AcZ
-
self
.
az_offset
)
gx
=
(
GyX
-
self
.
gx_offset
)
gy
=
(
GyY
-
self
.
gy_offset
)
gz
=
(
GyZ
-
self
.
gz_offset
)
if
hasattr
(
self
,
'sensitivity'
):
if
'ACCELERATION_LOW_THREADHOLD'
in
self
.
sensitivity
:
ACCELERATION_LOW_THREADHOLD
=
float
(
self
.
sensitivity
[
'ACCELERATION_LOW_THREADHOLD'
])
if
'ACCELERATION_UP_THREADHOLD'
in
self
.
sensitivity
:
ACCELERATION_UP_THREADHOLD
=
float
(
self
.
sensitivity
[
'ACCELERATION_UP_THREADHOLD'
])
if
'ANGULAR_VELOCITY_LOW_THREADHOLD'
in
self
.
sensitivity
:
ANGULAR_VELOCITY_LOW_THREADHOLD
=
float
(
self
.
sensitivity
[
'ANGULAR_VELOCITY_LOW_THREADHOLD'
])
if
'ANGULAR_VELOCITY_UP_THREADHOLD'
in
self
.
sensitivity
:
ANGULAR_VELOCITY_UP_THREADHOLD
=
float
(
self
.
sensitivity
[
'ANGULAR_VELOCITY_UP_THREADHOLD'
])
# calculating accelerationChangelitute vector for 3 axis
Raw_accelerationChange
=
pow
(
pow
(
ax
,
2
)
+
pow
(
ay
,
2
)
+
pow
(
az
,
2
),
0.5
)
# Multiplied by 10 to values are between 0 to 1
accelerationChange
=
Raw_accelerationChange
*
10
# Trigger1 function
# if accelerationChange breaks lower threshold
if
(
accelerationChange
<=
ACCELERATION_LOW_THREADHOLD
and
self
.
trigger2
==
False
):
self
.
trigger1
=
True
# print("TRIGGER 1 ACTIVATED")
if
(
self
.
trigger1
==
True
):
self
.
trigger1count
=
self
.
trigger1count
+
1
# if accelerationChange breaks upper threshold
if
(
accelerationChange
>=
ACCELERATION_UP_THREADHOLD
):
self
.
trigger2
=
True
# print("TRIGGER 2 ACTIVATED")
self
.
trigger1
=
False
self
.
trigger1count
=
0
if
(
self
.
trigger2
==
True
):
self
.
trigger2count
=
self
.
trigger2count
+
1
angleChange
=
pow
(
pow
(
gx
,
2
)
+
pow
(
gy
,
2
)
+
pow
(
gz
,
2
),
0.5
)
# Trigger2 function
# //if orientation changes by between 1-40 degrees
if
(
angleChange
>=
ANGULAR_VELOCITY_LOW_THREADHOLD
and
angleChange
<=
ANGULAR_VELOCITY_UP_THREADHOLD
):
self
.
trigger2
=
False
self
.
trigger2count
=
0
fall
=
True
return
fall
if
(
fall
==
True
):
# //in event of a fall detection
fall
=
False
if
(
self
.
trigger2count
>=
50
):
# //allow time for orientation change
self
.
trigger2
=
False
self
.
trigger2count
=
0
# print("TRIGGER 2 DECACTIVATED")
if
(
self
.
trigger1count
>=
5
):
# //allow time for accelerationChange to break upper threshold
self
.
trigger1
=
False
self
.
trigger1count
=
0
# print("TRIGGER 1 DECACTIVATED")
return
fall
haas_lib_bundles/python/libraries/motion/detections/tap_detection.py
0 → 100644
浏览文件 @
e9e62567
import
utime
# 延时函数在utime
ITER_NUM
=
100
class
tap_detection
:
def
__init__
(
self
,
tap_detect_count
,
getData
):
self
.
ax_offset
=
0.0
self
.
ay_offset
=
0.0
self
.
az_offset
=
0.0
self
.
triggercount
=
0
self
.
untriggercount
=
0
self
.
tapcount
=
0
self
.
tap_detect_count
=
tap_detect_count
if
(
self
.
tap_detect_count
==
2
):
self
.
accelerator_up_threshold
=
30.0
elif
(
self
.
tap_detect_count
==
1
):
self
.
accelerator_up_threshold
=
30.0
self
.
latency
=
150
self
.
getData
=
getData
self
.
calibrate
()
def
calibrate
(
self
):
ax_sum
=
0.0
ay_sum
=
0.0
az_sum
=
0.0
for
num
in
range
(
0
,
ITER_NUM
):
data
=
self
.
getData
()
# 读取传感器信息值
ax_sum
+=
data
[
0
][
0
]
ay_sum
+=
data
[
0
][
1
]
az_sum
+=
data
[
0
][
2
]
self
.
ax_offset
=
ax_sum
/
ITER_NUM
self
.
ay_offset
=
ay_sum
/
ITER_NUM
self
.
az_offset
=
az_sum
/
ITER_NUM
self
.
accelerator_low_threshold
=
0.0
for
num
in
range
(
0
,
ITER_NUM
):
data
=
self
.
getData
()
# 读取传感器信息值
ax
=
data
[
0
][
0
]
ay
=
data
[
0
][
1
]
az
=
data
[
0
][
2
]
ax
=
(
ax
-
self
.
ax_offset
)
ay
=
(
ay
-
self
.
ay_offset
)
az
=
(
az
-
self
.
az_offset
)
Raw_accelerationChange
=
pow
(
pow
(
ax
,
2
)
+
pow
(
ay
,
2
)
+
pow
(
az
,
2
),
0.5
)
# Multiplied by 10 to values are between 0 to 1
self
.
accelerator_low_threshold
+=
Raw_accelerationChange
*
10
self
.
accelerator_low_threshold
=
(
self
.
accelerator_low_threshold
/
ITER_NUM
)
*
2
print
(
'Now you can start tap detection!'
)
def
detect
(
self
):
fall
=
False
data
=
self
.
getData
()
# 读取传感器信息值
AcX
=
data
[
0
][
0
]
AcY
=
data
[
0
][
1
]
AcZ
=
data
[
0
][
2
]
ax
=
(
AcX
-
self
.
ax_offset
)
ay
=
(
AcY
-
self
.
ay_offset
)
az
=
(
AcZ
-
self
.
az_offset
)
# calculating accelerationChangelitute vector for 3 axis
Raw_accelerationChange
=
pow
(
pow
(
ax
,
2
)
+
pow
(
ay
,
2
)
+
pow
(
az
,
2
),
0.5
)
# Multiplied by 10 to values are between 0 to 1
accelerationChange
=
Raw_accelerationChange
*
10
if
(
fall
==
True
):
fall
=
False
if
hasattr
(
self
,
'sensitivity'
):
if
'ACCELERATION_UP_THREADHOLD'
in
self
.
sensitivity
:
self
.
accelerator_up_threshold
=
float
(
self
.
sensitivity
[
'ACCELERATION_UP_THREADHOLD'
])
# Trigger function
# if accelerationChange breaks lower threshold
if
(
self
.
tap_detect_count
==
2
):
if
(
accelerationChange
>=
self
.
accelerator_low_threshold
and
accelerationChange
<=
self
.
accelerator_up_threshold
):
self
.
triggercount
=
self
.
triggercount
+
1
if
(
self
.
triggercount
%
3
==
0
):
# catch one tap
self
.
tapcount
=
self
.
tapcount
+
1
utime
.
sleep_ms
(
100
)
if
(
self
.
tapcount
==
self
.
tap_detect_count
):
# catched tap_detect_count
self
.
triggercount
=
0
self
.
tapcount
=
0
fall
=
True
else
:
self
.
untriggercount
=
self
.
untriggercount
+
1
if
(
self
.
untriggercount
==
150
):
self
.
triggercount
=
0
self
.
tapcount
=
0
self
.
untriggercount
=
0
elif
(
self
.
tap_detect_count
==
1
):
if
(
accelerationChange
>=
self
.
accelerator_low_threshold
and
accelerationChange
<=
self
.
accelerator_up_threshold
):
self
.
triggercount
=
self
.
triggercount
+
1
if
(
self
.
triggercount
%
5
==
0
):
# catch one tap
self
.
tapcount
=
self
.
tapcount
+
1
utime
.
sleep_ms
(
100
)
if
(
self
.
tapcount
==
self
.
tap_detect_count
):
# catched tap_detect_count
self
.
triggercount
=
0
self
.
tapcount
=
0
fall
=
True
else
:
self
.
untriggercount
=
self
.
untriggercount
+
1
if
(
self
.
untriggercount
==
150
):
self
.
triggercount
=
0
self
.
tapcount
=
0
self
.
untriggercount
=
0
return
fall
haas_lib_bundles/python/libraries/motion/motion.py
0 → 100644
浏览文件 @
e9e62567
import
utime
# 延时函数在utime
from
.detections.fall_detection
import
fall_detection
from
.detections.tap_detection
import
tap_detection
import
_thread
# 线程库
class
Motion
:
def
__init__
(
self
,
action
,
getData
,
onActionDetected
):
self
.
action
=
action
if
(
action
==
"fall"
):
self
.
detectAction
=
fall_detection
(
getData
)
elif
(
action
==
"single_tap"
):
self
.
detectAction
=
tap_detection
(
1
,
getData
)
elif
(
action
==
"double_tap"
):
self
.
detectAction
=
tap_detection
(
2
,
getData
)
self
.
onActionDetected
=
onActionDetected
self
.
enableDetection
=
False
self
.
detectionThreadCreated
=
False
def
detect_action
(
self
):
while
(
self
.
enableDetection
==
True
):
isDetected
=
self
.
detectAction
.
detect
()
# 检测Action是否产生
if
(
isDetected
==
True
):
# Action被检测到
print
(
self
.
action
,
"detected!"
)
if
(
hasattr
(
self
,
'onActionDetected'
)):
self
.
onActionDetected
()
utime
.
sleep_us
(
10
)
self
.
detectionThreadCreated
=
False
# 使能action检测,若用户不指定灵敏度,则使用默认灵敏度
def
enable
(
self
,
sensitivity
=
''
):
if
(
self
.
detectionThreadCreated
==
False
):
if
(
sensitivity
!=
''
):
self
.
detectAction
.
sensitivity
=
sensitivity
try
:
# 创建行为检测线程
self
.
enableDetection
=
True
self
.
detectionThreadCreated
=
True
_thread
.
start_new_thread
(
self
.
detect_action
,
())
except
Exception
as
e
:
print
(
e
)
print
(
"Error: unable to start detect_action thread"
)
self
.
enableDetection
=
False
self
.
detectionThreadCreated
=
False
# 关闭action检测
def
disable
(
self
):
self
.
enableDetection
=
False
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录