Skip to content
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 4 additions & 18 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
[package]
name = "ublox"
version = "0.1.0"
authors = ["Lane Kolbly <[email protected]>"]
edition = "2018"
license = "MIT"
description = "A crate to communicate with u-blox GPS devices using the UBX protocol"
[workspace]
members = ["ublox", "ublox_derive", "ubx_protocol"]

[dependencies]
serde = "1.0"
serde_derive = "1.0"
serialport = "3.3.0"
bincode = "1.2.1"
chrono = "0.4"
crc = "1.8.1"
syn = "1.0.14"
ublox_derive = { path = "ublox_derive/" }
num-traits = "0.2"
num-derive = "0.2"
[patch.'crates-io']
ublox_derive = { path = "ublox_derive" }
15 changes: 15 additions & 0 deletions ublox/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "ublox"
version = "0.1.0"
authors = ["Lane Kolbly <[email protected]>"]
edition = "2018"
license = "MIT"
description = "A crate to communicate with u-blox GPS devices using the UBX protocol"

[dependencies]
serde = "1.0"
serde_derive = "1.0"
serialport = "3.3.0"
bincode = "1.2.1"
chrono = "0.4"
crc = "1.8.1"
File renamed without changes.
29 changes: 18 additions & 11 deletions src/lib.rs → ublox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
//! `ublox` is a library to talk to u-blox GPS devices using the UBX protocol.
//! At time of writing this library is developed for a device which behaves like
//! a NEO-6M device.
use crate::error::{Error, Result};
use chrono::prelude::*;
use crc::{crc16, Hasher16};
use std::io;
use std::time::{Duration, Instant};
use crate::error::{Error, Result};

pub use crate::ubx_packets::*;
pub use crate::segmenter::Segmenter;
pub use crate::ubx_packets::*;

mod error;
mod ubx_packets;
mod segmenter;
mod ubx_packets;

#[derive(Debug)]
pub enum ResetType {
Expand All @@ -32,7 +32,6 @@ pub struct Device {
port: Box<dyn serialport::SerialPort>,
segmenter: Segmenter,
//buf: Vec<u8>,

alp_data: Vec<u8>,
alp_file_id: u16,

Expand Down Expand Up @@ -61,7 +60,7 @@ impl Device {
///
/// This function will panic if it cannot open the serial port.
pub fn new(device: &str) -> Result<Device> {
let s = serialport::SerialPortSettings{
let s = serialport::SerialPortSettings {
baud_rate: 9600,
data_bits: serialport::DataBits::Eight,
flow_control: serialport::FlowControl::None,
Expand All @@ -70,7 +69,7 @@ impl Device {
timeout: Duration::from_millis(1),
};
let port = serialport::open_with_settings(device, &s).unwrap();
let mut dev = Device{
let mut dev = Device {
port: port,
segmenter: Segmenter::new(),
alp_data: Vec::new(),
Expand Down Expand Up @@ -174,7 +173,7 @@ impl Device {
pub fn get_position(&mut self) -> Option<Position> {
match (&self.navstatus, &self.navpos) {
(Some(status), Some(pos)) => {
if status.itow != pos.get_itow() {
if status.itow != pos.itow {
None
} else if status.flags & 0x1 == 0 {
None
Expand All @@ -190,7 +189,7 @@ impl Device {
pub fn get_velocity(&mut self) -> Option<Velocity> {
match (&self.navstatus, &self.navvel) {
(Some(status), Some(vel)) => {
if status.itow != vel.get_itow() {
if status.itow != vel.itow {
None
} else if status.flags & 0x1 == 0 {
None
Expand Down Expand Up @@ -264,7 +263,7 @@ impl Device {
pub fn load_aid_data(
&mut self,
position: Option<Position>,
tm: Option<DateTime<Utc>>
tm: Option<DateTime<Utc>>,
) -> Result<()> {
let mut aid = AidIni::new();
match position {
Expand Down Expand Up @@ -313,7 +312,10 @@ impl Device {
return Ok(Some(Packet::AckAck(packet)));
}
Some(Packet::MonVer(packet)) => {
println!("Got versions: SW={} HW={}", packet.sw_version, packet.hw_version);
println!(
"Got versions: SW={} HW={}",
packet.sw_version, packet.hw_version
);
return Ok(None);
}
Some(Packet::NavPosVelTime(packet)) => {
Expand Down Expand Up @@ -382,7 +384,12 @@ impl Device {
}

fn send(&mut self, packet: UbxPacket) -> Result<()> {
CfgMsg{classid: 5, msgid: 4, rates: [0, 0, 0, 0, 0, 0]}.to_bytes();
CfgMsg {
classid: 5,
msgid: 4,
rates: [0, 0, 0, 0, 0, 0],
}
.to_bytes();
let serialized = packet.serialize();
self.port.write_all(&serialized)?;
Ok(())
Expand Down
12 changes: 8 additions & 4 deletions src/main.rs → ublox/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
use ublox::{Device, Position};
use std::time::Duration;
use chrono::prelude::*;
use std::time::Duration;
use ublox::{Device, Position};

fn main() {
let mut dev = Device::new("/dev/ttyUSB0").unwrap();

let pos = Position{lon: -97.5, lat: 30.2, alt: 200.0};
let pos = Position {
lon: -97.5,
lat: 30.2,
alt: 200.0,
};
println!("Setting AID data...");
match dev.load_aid_data(Some(pos), Some(Utc::now())) {
Err(e) => {
println!("Got error setting AID data: {:?}", e);
},
}
_ => {}
}

Expand Down
File renamed without changes.
144 changes: 27 additions & 117 deletions src/ubx_packets.rs → ublox/src/ubx_packets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,8 @@ use crate::error::Result;
use bincode;
use chrono::prelude::*;
use serde_derive::{Deserialize, Serialize};
use std::vec::Vec;
use std::str;
//use syn::{parse_macro_input, parse_quote, DeriveInput, Data, TokenStream};
use ublox_derive::ubx_packet;

// These are needed for ubx_packet
use std::convert::TryInto;
use num_derive::{FromPrimitive, ToPrimitive};
use num_traits::{FromPrimitive, ToPrimitive};
use std::vec::Vec;

#[derive(Debug)]
pub struct Position {
Expand Down Expand Up @@ -73,35 +66,6 @@ impl UbxPacket {
}
}

/*#[proc_macro_attribute]
fn ubx_packet(attr: TokenStream, input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
match input.data {
Data::Struct(ref data) => {
match data.fields {
Fields::Named(ref fields) => {
println!("{:?}", fields);
}
Fields::Unnamed(ref fields) => {
//
}
}
}
Data::Enum(_) | Data::Union(_) => unimplemented!()
}
}*/

/*#[ubx_packet]
struct MyPacket {
tow: u32,
lon: i32,
lat: i32,
height: i32,
height_msl: i32,
h_acc: u32,
v_acc: u32,
}*/

pub trait UbxMeta {
fn get_classid() -> u8;
fn get_msgid() -> u8;
Expand Down Expand Up @@ -148,18 +112,7 @@ macro_rules! ubx_meta {
};
}

#[ubx_packet]
pub struct NavPosLLH {
itow: u32,
lon: i32,
lat: i32,
height: i32,
height_msl: i32,
horizontal_accuracy: u32,
vertical_accuracy: u32,
}

/*#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct NavPosLLH {
pub itow: u32,
pub lon: i32,
Expand All @@ -168,45 +121,21 @@ pub struct NavPosLLH {
pub height_msl: i32,
pub horizontal_accuracy: u32,
pub vertical_accuracy: u32,
}*/
}

//ubx_meta!(NavPosLLH, 0x01, 0x02);
ubx_meta!(NavPosLLH, 0x01, 0x02);

impl From<&NavPosLLH> for Position {
fn from(packet: &NavPosLLH) -> Self {
Position {
lon: packet.get_lon() as f32 / 10_000_000.0,
lat: packet.get_lat() as f32 / 10_000_000.0,
alt: packet.get_height_msl() as f32 / 1000.0,
}
}
}

trait FooTrait {
fn foo() -> u32;
}

impl<T: FooTrait> From<&T> for Velocity {
fn from(packet: &T) -> Self {
Velocity {
speed: 0.0,
heading: 0.0,
lon: packet.lon as f32 / 10_000_000.0,
lat: packet.lat as f32 / 10_000_000.0,
alt: packet.height_msl as f32 / 1000.0,
}
}
}

#[ubx_packet]
pub struct NavVelNED {
pub itow: u32,
pub vel_north: i32, // cm/s
pub vel_east: i32,
pub vel_down: i32,
pub speed: u32,
pub ground_speed: u32,
pub heading: i32, // 1e-5 degrees
}

/*#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct NavVelNED {
pub itow: u32,
pub vel_north: i32, // cm/s
Expand All @@ -217,33 +146,17 @@ pub struct NavVelNED {
pub heading: i32, // 1e-5 degrees
}

ubx_meta!(NavVelNED, 0x01, 0x12);*/
ubx_meta!(NavVelNED, 0x01, 0x12);

impl From<&NavVelNED> for Velocity {
fn from(packet: &NavVelNED) -> Self {
Velocity {
speed: packet.get_ground_speed() as f32 / 1_000.0,
heading: packet.get_heading() as f32 / 100_000.0,
speed: packet.ground_speed as f32 / 1_000.0,
heading: packet.heading as f32 / 100_000.0,
}
}
}

/*pub struct NavPosVelTime {
itow: u32,
year: u16,
month: u8,
day: u8,
hour: u8,
min: u8,
sec: u8,

#[ubx_bitfield(8)]
#[ubx_range(0:0)]
valid: bool,

// etc.
}*/

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct NavPosVelTime {
pub itow: u32,
Expand Down Expand Up @@ -301,14 +214,13 @@ impl From<&NavPosVelTime> for Velocity {

impl From<&NavPosVelTime> for DateTime<Utc> {
fn from(sol: &NavPosVelTime) -> Self {
let ns = if sol.nanosecond < 0 { 0 } else { sol.nanosecond } as u32;
let ns = if sol.nanosecond < 0 {
0
} else {
sol.nanosecond
} as u32;
Utc.ymd(sol.year as i32, sol.month.into(), sol.day.into())
.and_hms_nano(
sol.hour.into(),
sol.min.into(),
sol.sec.into(),
ns,
)
.and_hms_nano(sol.hour.into(), sol.min.into(), sol.sec.into(), ns)
}
}

Expand Down Expand Up @@ -482,8 +394,12 @@ pub struct MonVer {
}

impl UbxMeta for MonVer {
fn get_classid() -> u8 { 0x0a }
fn get_msgid() -> u8 { 0x04 }
fn get_classid() -> u8 {
0x0a
}
fn get_msgid() -> u8 {
0x04
}

fn to_bytes(&self) -> Vec<u8> {
unimplemented!("Sending MonVer packets is unimplemented");
Expand Down Expand Up @@ -516,16 +432,10 @@ macro_rules! parse_packet_branch {
impl Packet {
pub fn deserialize(classid: u8, msgid: u8, payload: &[u8]) -> Result<Packet> {
match (classid, msgid) {
//(0x01, 0x02) => parse_packet_branch!(Packet::NavPosLLH, payload),
(0x01, 0x02) => {
Ok(Packet::NavPosLLH(NavPosLLH::new(payload.try_into().unwrap())))
},
(0x01, 0x02) => parse_packet_branch!(Packet::NavPosLLH, payload),
(0x01, 0x03) => parse_packet_branch!(Packet::NavStatus, payload),
(0x01, 0x07) => parse_packet_branch!(Packet::NavPosVelTime, payload),
//(0x01, 0x12) => parse_packet_branch!(Packet::NavVelNED, payload),
(0x01, 0x12) => {
Ok(Packet::NavVelNED(NavVelNED::new(payload.try_into().unwrap())))
}
(0x01, 0x12) => parse_packet_branch!(Packet::NavVelNED, payload),
(0x05, 0x01) => parse_packet_branch!(Packet::AckAck, payload),
(0x06, 0x00) => {
// Depending on the port ID, we parse different packets
Expand All @@ -542,11 +452,11 @@ impl Packet {
(0x0A, 0x04) => {
let sw_version = str::from_utf8(&payload[0..30]).unwrap();
let hw_version = str::from_utf8(&payload[31..40]).unwrap();
return Ok(Packet::MonVer(MonVer{
return Ok(Packet::MonVer(MonVer {
sw_version: sw_version.to_string(),
hw_version: hw_version.to_string(),
}));
},
}
(0x0B, 0x01) => parse_packet_branch!(Packet::AidIni, payload),
(0x0B, 0x32) => parse_packet_branch!(Packet::AlpSrv, payload),
(c, m) => {
Expand Down
Loading