[Fix]由于数据不准,微调了下策略

多报出错不如少报但正确
This commit is contained in:
chenfufeng
2021-11-29 18:52:05 +08:00
parent c262c9fcfb
commit ba47f45a9a

View File

@@ -1,15 +1,18 @@
package com.mogo.eagle.core.function.v2x.redlightwarning
import android.location.Location
import com.mogo.eagle.core.data.map.MogoLatLng
import com.mogo.eagle.core.data.trafficlight.*
import com.mogo.eagle.core.data.trafficlight.TrafficLightStatusHelper.getCurrentRoadTrafficLight
import com.mogo.eagle.core.function.api.trafficlight.IMoGoTrafficLightListener
import com.mogo.eagle.core.function.api.vip.IMoGoVipSetListener
import com.mogo.eagle.core.function.call.hmi.CallerHmiManager
import com.mogo.eagle.core.function.call.map.CallerMapLocationListenerManager
import com.mogo.eagle.core.function.call.trafficlight.CallTrafficLightListenerManager
import com.mogo.eagle.core.function.call.vip.CallVipSetListenerManager
import com.mogo.eagle.core.utilcode.util.SPUtils
import com.mogo.eagle.core.function.v2x.vip.VipCarManager
import com.mogo.eagle.core.utilcode.util.ThreadUtils
import com.mogo.map.navi.IMogoCarLocationChangedListener2
import com.mogo.module.common.MogoApisHandler
import com.mogo.module.common.enums.EventTypeEnum
import com.mogo.utils.logger.Logger
import kotlin.math.abs
@@ -17,14 +20,19 @@ import kotlin.math.ceil
import kotlin.math.floor
class RedLightWarningManager : IMoGoTrafficLightListener, IMoGoVipSetListener {
class RedLightWarningManager : IMoGoTrafficLightListener, IMoGoVipSetListener,
IMogoCarLocationChangedListener2 {
private var vip: Boolean = false
// 是否第一次进入道路100m处
private var isFirst = true
// 是否已进入到路口(停止线处)
private var isEnter = false
private var mLocation: Location? = null
companion object {
const val TAG = "RedLightWarningManager"
@@ -35,10 +43,11 @@ class RedLightWarningManager : IMoGoTrafficLightListener, IMoGoVipSetListener {
}
override fun onTrafficLightStatus(trafficLightResult: TrafficLightResult) {
// 到路口100m时回调
Logger.d(TAG, "处理路口交通数据:是否是第一次处理:${isFirst}是否进入路口:${isEnter}")
if (isFirst && !isEnter) {
getCurrentRoadTrafficLight(trafficLightResult)?.let {
handleRedLightWarning(it, trafficLightResult.flashYellow)
handleRedLightWarning(it, trafficLightResult)
}
isFirst = false
}
@@ -59,36 +68,57 @@ class RedLightWarningManager : IMoGoTrafficLightListener, IMoGoVipSetListener {
fun listenTrafficLight() {
CallTrafficLightListenerManager.registerEnterCrossRoadListener(TAG, this)
CallTrafficLightListenerManager.registerTrafficLightListener(TAG, this)
MogoApisHandler.getInstance().apis.registerCenterApi.registerCarLocationChangedListener(
VipCarManager.TAG,
this
)
CallVipSetListenerManager.registerVipSetListener(TAG, this)
}
private fun handleRedLightWarning(trafficLightStatus: TrafficLightStatus, yellowLightTime: Int = 0) {
private fun handleRedLightWarning(
trafficLightStatus: TrafficLightStatus,
trafficLightResult: TrafficLightResult
) {
// 如果是Vip则不处理
if (vip) {
Logger.w(TAG, "Vip用户不处理闯红灯、绿灯通行预警逻辑")
return
}
// 路口100m闯红灯预警
CallerMapLocationListenerManager.getCurrentLocation()?.let {
mLocation?.let {
// 单位m/s
val speed = it.speed
// 车停止或者速度非常慢,可能返回负数或者很小的值,需要过滤
Logger.d(TAG, "speed is:$speed")
if (speed <= 2.5f) return// 小于等于9km/h不处理
val distance = 100
// 由于到路口100m时回调不准手动计算直线距离
val distance = MogoApisHandler.getInstance().apis.mapServiceApi
.mapUIController.calculateLineDistance(
MogoLatLng(it.latitude, it.longitude),
MogoLatLng(trafficLightResult.lat, trafficLightResult.lon)
)
Logger.d(
TAG,
"路口经度为:${trafficLightResult.lon},纬度为:${trafficLightResult.lat};车的经度为:${it.longitude},纬度为:${it.latitude};两点距离为:${distance}"
)
val remainTime = trafficLightStatus.remain
val arriveTime = distance / speed
Logger.d(TAG, "speed is:$speed,remainTime is:$remainTime,arriveTime is:$arriveTime,yellowTime is:$yellowLightTime")
Logger.d(
TAG,
"speed is:$speed,remainTime is:$remainTime,arriveTime is:$arriveTime,yellowTime is:${trafficLightResult.flashYellow}"
)
when {
trafficLightStatus.isRed() -> {
Logger.d(TAG, "=====当前为红灯=====")
// 到达路口时红灯还没走完
if (arriveTime <= remainTime) {
// 到达路口时红灯还没走完(由于多个数据有偏差红灯预警延长1s绿灯提示延长2s多报出错不如少报且准)
if (arriveTime <= remainTime + 1) {
redLightWarning()
} else if (arriveTime > remainTime + yellowLightTime) {// 到达时红、黄灯都走完
} else if (arriveTime > remainTime + trafficLightResult.flashYellow + 2) {// 到达时红、黄灯都走完
// 单位Km/h当前为红灯推荐速度越慢越容易绿灯通过且要满足[10,50]
val originRemainSpeed = floor(100 / (remainTime + yellowLightTime) * 3.6).toInt()
when {
val originRemainSpeed =
floor(distance / (remainTime + trafficLightResult.flashYellow) * 3.6).toInt()
when {
originRemainSpeed > 50 -> greenLightWarning("10到50")
originRemainSpeed in 10..50 -> greenLightWarning("10到$originRemainSpeed")
}
@@ -96,13 +126,13 @@ class RedLightWarningManager : IMoGoTrafficLightListener, IMoGoVipSetListener {
}
trafficLightStatus.isYellow() -> {
Logger.d(TAG, "=====当前为黄灯=====")
// 到达路口时黄灯还没走完
if (arriveTime <= remainTime) {
// 到达路口时黄灯还没走完(由于多个数据有偏差红灯预警延长1s绿灯提示延长2s多报出错不如少报且准)
if (arriveTime <= remainTime + 1) {
redLightWarning()
} else {
} else if (arriveTime > remainTime + 2) {
// 单位Km/h当前为黄灯推荐速度越慢越容易绿灯通过且要满足[10,50]
val originRemainSpeed = floor(100 / remainTime * 3.6).toInt()
when {
val originRemainSpeed = floor(distance / remainTime * 3.6).toInt()
when {
originRemainSpeed > 50 -> greenLightWarning("10到50")
originRemainSpeed in 10..50 -> greenLightWarning("10到$originRemainSpeed")
}
@@ -110,17 +140,16 @@ class RedLightWarningManager : IMoGoTrafficLightListener, IMoGoVipSetListener {
}
trafficLightStatus.isGreen() -> {
Logger.d(TAG, "=====当前为绿灯=====")
// 到达路口时绿灯已经走完
if (arriveTime >= remainTime) {
// 到达路口时绿灯已经走完(由于多个数据有偏差,多报出错不如少报且准,绿灯时间减少一点)
if (arriveTime >= remainTime - 1) {
redLightWarning()
} else if (arriveTime < remainTime) {
} else if (arriveTime < remainTime - 2) {
// 单位Km/h当前为绿灯推荐速度越快越容易绿灯通过且要满足[10,50]
val originRemainSpeed = ceil(100 / remainTime * 3.6).toInt()
when {
val originRemainSpeed = ceil(distance / remainTime * 3.6).toInt()
when {
originRemainSpeed < 10 -> greenLightWarning("10到50")
originRemainSpeed in 10..50 -> greenLightWarning("${originRemainSpeed}到50")
}
greenLightWarning()
}
}
}
@@ -174,5 +203,19 @@ class RedLightWarningManager : IMoGoTrafficLightListener, IMoGoVipSetListener {
fun onDestroy() {
CallTrafficLightListenerManager.unRegisterTrafficLightListener(TAG)
CallVipSetListenerManager.unRegisterVipSetListener(TAG)
CallTrafficLightListenerManager.unRegisterEnterCrossRoadListener(TAG)
MogoApisHandler.getInstance().apis.registerCenterApi.unregisterCarLocationChangedListener(
TAG, this
)
}
override fun onCarLocationChanged(latLng: MogoLatLng?) {
}
override fun onCarLocationChanged2(latLng: Location?) {
latLng?.let {
mLocation = it
}
}
}