Skip to content

Commit

Permalink
sbg-ecom added
Browse files Browse the repository at this point in the history
  • Loading branch information
crisconru committed May 22, 2024
1 parent 91a9cde commit 5d12095
Show file tree
Hide file tree
Showing 43 changed files with 2,496 additions and 0 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

108 changes: 108 additions & 0 deletions packages/sbg-ecom/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
.vscode
.DS_Store
package-lock.json

# Logs
!logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port
21 changes: 21 additions & 0 deletions packages/sbg-ecom/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 CoreMarine

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions packages/sbg-ecom/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# sbgECom parser

This library is a parser for sbgECom protocol which is an SBG binary propietary protocol for its GNSS + IMU devices.
28 changes: 28 additions & 0 deletions packages/sbg-ecom/examples/example_binary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { readFileSync } from 'fs'
import { SBGParser, availableFirmwares} from '../src/index'

const SBG_FILE = 'test/sbg-raw.bin'

const content: Buffer = readFileSync(SBG_FILE)

// console.log(content.toString('ascii'))
const firmwares = Array.from(availableFirmwares())
console.log(`Available firmwares = ${firmwares.join(', ')}`)
const firmware = '2.3'

console.log('Leer el fichero')
const parser = new SBGParser()
parser.addData(content)
const response = parser.getFrames()
console.log('parsed')
// console.log(response)
console.log(`responses = ${response.length}`)
let unknown = 0
let known = 0
response.forEach(res => {
// if (res.name !== 'unknown') { console.dir(res) }
(res.name !== 'unknown') ? known++ : unknown++
})

console.log(`Unknown frames -> ${unknown}`)
console.log(`Known frames -> ${known}`)
67 changes: 67 additions & 0 deletions packages/sbg-ecom/examples/example_csv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { readFileSync } from 'fs'
import { SBGParser, availableFirmwares} from '../src/index'
import { SBGFrameResponse } from '../src/types'

const parser = new SBGParser()
parser.memory = true

const parseFile = (path: string): SBGFrameResponse[] => {
const content = readFileSync(path, 'ascii')
// console.log(content)
const map = new Map<string, any>()

const lines = content.split('\n')
lines.forEach((line, lineNumber) => {
if (line.length && lineNumber) {
const [_id, timestamp, data] = line.split(',')
if (data.includes('x')) {
const buffer = Buffer.from(data.split('x')[1], 'hex')
map.set(timestamp, buffer)
}
// console.log(`${lineNumber}. ${line}`)
}
})
// const parser = new SBGParser()
// parser.memory = true
map.forEach((value, key) => {
parser.addData(value)
// console.log(`Added data of ${key}`)
})
return parser.getFrames()
}

const infoFrames = (frames: SBGFrameResponse[]): void => {
// console.log('parsed')
// // console.log(frames)
// console.log(`frames = ${frames.length}`)
let unknown = 0
let known = 0
frames.forEach(frame => {
// if (res.name !== 'unknown') { console.dir(res) }
(frame.name !== 'unknown') ? known++ : unknown++
// if (frame.name.includes('POS')) { console.dir(frame) }
})
console.log(`Unknown frames -> ${unknown}`)
console.log(`Known frames -> ${known}`)
}

const sbgframes = new Map<string, number>()

const updateFramesCounter = (frames: SBGFrameResponse[]) => {
frames.forEach(frame => {
const { name, type, format } = frame
const key = `${type}_${format}_${name}}`
const prevCounter = sbgframes.get(key)
const counter = (prevCounter === undefined) ? 0 : prevCounter + 1
sbgframes.set(key, counter)
})
}

// const SBG_CSV = 'test/sbg_50.csv'
// const SBG_CSV = 'test/sbg_100.csv'
// const SBG_CSV = 'test/sbg_1000.csv'
const SBG_CSV = 'test/sbg_2000.csv'
const parsedFrames = parseFile(SBG_CSV)
infoFrames(parsedFrames)
updateFramesCounter(parsedFrames)
console.log('----------------------------------------------')
71 changes: 71 additions & 0 deletions packages/sbg-ecom/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"name": "@coremarine/sbg-ecom",
"version": "0.0.1",
"description": "Library to parse sbgECom binary protocol for SBG devices",
"author": "CoreMarine",
"license": "MIT",
"homepage": "https://github.com/core-marine-dev/devices/tree/main/packages/sbg-ecom",
"repository": {
"type": "git",
"url": "git+https://github.com/core-marine-dev/devices.git"
},
"bugs": {
"url": "https://github.com/core-marine-dev/devices/issues"
},
"keywords": [
"sbg",
"ecom",
"sbgECom",
"imu",
"mru",
"gnss",
"gps",
"binary",
"protocol"
],
"type": "module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
}
}
},
"sideEffects": false,
"files": [
"dist"
],
"engines": {
"node": ">= 18"
},
"scripts": {
"build": "npm run format && tsup",
"lint": "ts-standard",
"format": "ts-standard --fix",
"test": "vitest",
"test:coverage": "vitest run --coverage"
},
"dependencies": {
"crc": "^4.3.2"
},
"ts-standard": {
"ignore": [
"tests",
"vitest.config.ts",
"tsup.config.ts",
"examples"
]
},
"eslintConfig": {
"parserOptions": {
"project": "./tsconfig.json"
}
}
}
79 changes: 79 additions & 0 deletions packages/sbg-ecom/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@

export const SBGFrameMessageClasses = {
CMD: 0x10,
LOG: 0x00,
HIGH_FREQ: 0x01,
NMEA_STANDARD: 0x02,
NMEA_PROPIETARY: 0x03,
THIRD_PARTY: 0x04
} as const

export const SBGFrameTypes = {
CMD: 'command',
LOG: 'log',
HIGH_FREQ: 'high-frequency',
NMEA_STANDARD: 'nmea-standard',
NMEA_PROPIETARY: 'nmea-propietary',
THIRD_PARTY: 'thid-party',
UNKNOWN: 'unknown'
} as const

export const SBGFrameFormats = {
STANDARD: 'standard',
LARGE: 'large'
} as const

export const SBGParsingStatuses = {
OK: 0,
MISSING_BYTES: 1,
ERROR_CRC: 2,
ERROR_EXT: 3
} as const

export const TWO_BYTES_MAX = 65_535
// ALL FRAMES
export const SYNC_FLAG = Buffer.from([0xff, 0x5a])
export const SYNC_INDEX = 0
export const SYNC_LENTGH = 2

export const ID_INDEX = 2
export const ID_LENGTH = 1

export const CLASS_INDEX = 3
export const CLASS_LENGTH = 1

export const LENGTH_INDEX = 4
export const LENGTH_LENGTH = 2

export const PAYLOAD_INDEX = 6

export const CRC_LENGTH = 2

export const EXT_FLAG = Buffer.from([0x33])
export const EXT_LENGTH = 1

export const HEADER_LENGTH = SYNC_LENTGH + ID_LENGTH + CLASS_LENGTH + LENGTH_LENGTH
export const FOOTER_LENGTH = CRC_LENGTH + EXT_LENGTH

export const MINIMAL_FRAME_LENGTH = HEADER_LENGTH + FOOTER_LENGTH
// LARGE FRAME
export const STANDARD_FRAME_MAXIMUM_CLASS_BYTELENGTH = 4096
export const LARGE_FRAME_MINIMUM_CLASS = Buffer.from([0x80]).readUInt8()
export const TRANSMISSION_ID_INDEX = 6

export const TRANSMISSION_ID_LENGTH = 1

export const PAGE_INDEX_INDEX = 7
export const PAGE_INDEX_LENGTH = 2

export const PAGES_INDEX = 9
export const PAGES_LENGTH = 2

export const DATA_INDEX = 11
// UNKOWN MESSAGE
export const UNKNOWN_SBG_FRAME_DATA = {
name: 'unknown',
type: SBGFrameTypes.UNKNOWN,
format: SBGFrameFormats.STANDARD,
data: null
}
Loading

0 comments on commit 5d12095

Please sign in to comment.