main.go代码:
package main
import(
"fmt"
"net"
"time"
"os"
"strings"
"strconv"
"github.com/gin-gonic/gin"
"net/http"
"math"
)
const(
NETWORK = "tcp"
RADDR = "192.168.1.11:2112" //激光扫描仪地址
START_SCAN = "sEN LMDscandata 1"
STOP_SCAN = "sEN LMDscandata 0"
)
var(
netConn net.Conn
xPos [1000] float64
yPos [1000]float64
dataLen int64
Dim [1000] float64
)
//50hz ,0.5度
func main(){
fmt.Println("开始连接lms511")
conn,err := net.DialTimeout(NETWORK,RADDR,10*time.Second)
if err != nil{
fmt.Println("Client connect error !",err.Error())
os.Exit(1)
}
netConn = conn
fmt.Println(netConn.LocalAddr().String(),"连接成功")
Lms511Test()
router := initRouter() //初始化路由
router.Run(":8080")
}
func initRouter() *gin.Engine {
gin.SetMode(gin.ReleaseMode)
// gin.SetMode(gin.DebugMode)
router := gin.New()
//静态服务器
router.Static("/webapp", "./webapp")
//api
api := router.Group("api")
{
//获取数据
api.GET("getData", func (c *gin.Context){
c.JSON(http.StatusOK, gin.H{
"len": dataLen,
"xPos":xPos,
"yPos":yPos,
"dim":Dim,
})
})
}
return router
}
//lms511通讯测试
func Lms511Test() {
buf := make([]byte,5000)
go func(){
for{
n, err := netConn.Read(buf) //读取扫描仪发的数据
flag := checkError(err)
if flag == 0 {
fmt.Println("data error")
break
}
//解析扫描仪协议
if buf[0] == 0x02 {
data := strings.Split(string(buf[0:n])," ")
//系数
factors := 1.0
if len(data) > 100{
if data[21] == "40000000"{
factors = 2.0
}
//开始角度
tmp ,_ := strconv.ParseInt(data[23],16,64)
startAngle := float64(int(tmp)/10000)
//角度分辨率
tmp ,_= strconv.ParseInt(data[24],16,64)
angleStep := float64(tmp)/10000
//数据个数
dataNum,_ := strconv.ParseInt(data[25],16,64)
// fmt.Println(dataNum)
//计算坐标
// x := make([]float64,dataNum)
// y := make([]float64,dataNum)
dataLen = dataNum
for i := 0 ;i < int(dataNum);i++{
d ,_ := strconv.ParseInt(data[26+i],16,64)
xPos[i] = float64(d)*factors*math.Cos((startAngle + float64(i)*float64(angleStep))/180*math.Pi)/1000
yPos[i] = float64(d)*factors*math.Sin((startAngle + float64(i)*float64(angleStep))/180*math.Pi)/1000
Dim[i] = float64(d)*factors
}
}
}
}
}()
sendLaserCmd(netConn,START_SCAN) ; //发送开始扫描命令
fmt.Println("开始扫描")
// time.Sleep(time.Second*10)
// sendLaserCmd(netConn,STOP_SCAN) ; //停止连续采集
// fmt.Println("扫描结束")
}
//发送命令到激光扫描仪
func sendLaserCmd(conn net.Conn,cmd string){
buff := []byte(cmd)
var sendBuff []byte = make([]byte,len(buff)+2)
sendBuff[0] = 0x02
sendBuff[len(buff)+1] = 0x03
for i := 0 ;i < len(buff);i++{
sendBuff[i+1] = buff[i]
}
conn.Write(sendBuff)
}
//检查错误
func checkError(err error) int {
if err != nil {
if err.Error() == "EOF" {
//fmt.Println("用户退出了")
return 0
}
fmt.Println("an error!", err.Error())
return -1
}
return 1
}
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>LMS511激光扫描仪测试程序</title>
<script src = "./js/jquery.min.js"></script>
</head>
<body>
<canvas id="myCanvas" width="800" height="600"></canvas>
</body>
<script type="text/javascript">
function getPos(res){
var position = [];
var scale = 100;
for (var i = 1;i < res.len;i++){
var dim = res.dim[i]; //距离
var angle = (-5 + i*0.5)/180*Math.PI;
// x = 300+dim*2*Math.cos(angle)/1000*scale;
// y = 300-dim*2*Math.sin(angle)/1000*scale;
x = 300 + res.xPos[i]*scale;
y = 300 - res.yPos[i]*scale;
position.push({x:x,y:y});
}
return position;
}
setInterval(function(){
$.get("/api/getData",function(res){
position = getPos(res);
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.fillStyle="#113333";
ctx.fillRect(0,0,800,600);
ctx.beginPath();
// ctx.moveTo(position[0].x,position[0].y);
ctx.fillStyle = "red"
for (var i = 1;i < position.length;i++){
var pos = position[i];
// ctx.lineTo(pos.x,pos.y)
ctx.fillRect(pos.x-1,pos.y-1,2,2)
}
ctx.strokeStyle="green";
ctx.stroke();
})
},200)
</script>
</html>
效果图:

golang+webgl实践激光雷达(二)激光扫描仪测试程序
原文:https://www.cnblogs.com/laofenmao/p/12081894.html