123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- package main
- import (
- "awesomeProject/entity"
- "awesomeProject/pjisuv_msgs"
- "fmt"
- "github.com/bluenviron/goroslib/v2"
- "math"
- "os"
- "os/signal"
- "time"
- )
- var (
- Maxlenobj int32 = 0
- param entity.PjisuvParam
- ObjectSlice = make(map[uint32][][]float32)
- )
- func main() {
- ticker := time.NewTicker(3 * time.Second)
- defer ticker.Stop()
- go listener()
- for {
- select {
- case <-ticker.C:
- FinalCallback()
- }
- }
- }
- func isCuttingOut(ObjectList [][]float32) bool {
- for i, objY := range ObjectList[1] {
- if math.Abs(float64(objY)) <= 0.9 && math.Abs(param.AngularVelocityZOfCicvLocation) <= 0.6 {
- //fmt.Println(objY)
- for j := 0; j < len(ObjectList[1])-i-1; j++ {
- objX := ObjectList[0][1+i+j]
- if math.Abs(float64(ObjectList[1][1+i+j])) >= 1.9 && math.Abs(param.AngularVelocityZOfCicvLocation) <= 0.6 && objX >= 1 && objX <= 19 {
- //fmt.Println(objX)
- return true
- }
- }
- }
- }
- return false
- }
- func FinalCallback() {
- for _, objValue := range ObjectSlice {
- //fmt.Println(objValue)
- //fmt.Println("------------------------------------------------")
- Maxlenobj = max(Maxlenobj, int32(len(objValue[0])))
- if len(objValue[0]) <= 10 || !isCuttingOut(objValue) {
- continue
- }
- event_lable := "FrontVehicleCutOutNear"
- fmt.Println(event_lable)
- ObjectSlice = make(map[uint32][][]float32)
- }
- if Maxlenobj >= 100 {
- ObjectSlice = make(map[uint32][][]float32)
- Maxlenobj = 0
- }
- }
- func CallbackCicvLocation(data *pjisuv_msgs.PerceptionLocalization) {
- param.AngularVelocityZOfCicvLocation = data.AngularVelocityZ
- }
- func CallbackTpperception(data *pjisuv_msgs.PerceptionObjects) {
- for _, obj := range data.Objs {
- if obj.X <= -2 || math.Abs(float64(obj.Y)) >= 90 {
- continue
- }
- if _, ok := ObjectSlice[obj.Id]; !ok {
- ObjectSlice[obj.Id] = [][]float32{{}, {}}
- }
- ObjectSlice[obj.Id][0] = append(ObjectSlice[obj.Id][0], obj.X)
- ObjectSlice[obj.Id][1] = append(ObjectSlice[obj.Id][1], obj.Y)
- }
- }
- func listener() {
- // create a node and connect to the master
- n, err := goroslib.NewNode(goroslib.NodeConf{
- Name: "goroslib_sub",
- MasterAddress: "127.0.0.1:11311",
- })
- if err != nil {
- panic(err)
- }
- defer n.Close()
- // create a subscriber
- subCicvLocation, err := goroslib.NewSubscriber(goroslib.SubscriberConf{
- Node: n,
- Topic: "/cicv_location",
- Callback: CallbackCicvLocation,
- })
- if err != nil {
- panic(err)
- }
- defer subCicvLocation.Close()
- // create a subscriber
- subTpperception, err := goroslib.NewSubscriber(goroslib.SubscriberConf{
- Node: n,
- Topic: "tpperception",
- Callback: CallbackTpperception,
- })
- if err != nil {
- panic(err)
- }
- defer subTpperception.Close()
- // wait for CTRL-C
- c := make(chan os.Signal, 1)
- signal.Notify(c, os.Interrupt)
- <-c
- }
|