Skip to content

Commit

Permalink
Simple CANape point cloud demo with CDR encoded serialized data
Browse files Browse the repository at this point in the history
  • Loading branch information
RainerZ committed Aug 6, 2024
1 parent 76f2210 commit 9b5175e
Show file tree
Hide file tree
Showing 11 changed files with 1,366 additions and 688 deletions.
Binary file added examples/point_cloud_demo/CANape.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
551 changes: 289 additions & 262 deletions examples/point_cloud_demo/CANape/CANape.ini

Large diffs are not rendered by default.

858 changes: 656 additions & 202 deletions examples/point_cloud_demo/CANape/point_cloud.cna

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/point_cloud_demo/CANape/point_cloud.cnaxml
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@
<property name="ManualClosedPanelsBarVisibility">false</property>
<property name="RedrawContentWhenResizing">false</property>
<property name="ShowFloatWindowsInTaskbar">false</property>
<property name="RestoreLayoutOptions.FloatPanelsRestoreOffset" OwnerType="DevExpress.Xpf.Docking.RestoreLayoutOptions" DependencyPropertyType="System.Windows.Point">20.5,155.5</property>
<property name="RestoreLayoutOptions.FloatPanelsRestoreOffset" OwnerType="DevExpress.Xpf.Docking.RestoreLayoutOptions" DependencyPropertyType="System.Windows.Point">15,145.5</property>
</property>
</XtraSerializer>
</Root>
437 changes: 258 additions & 179 deletions examples/point_cloud_demo/CANape/point_cloud.gvc

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions examples/point_cloud_demo/CANape/xcp_lite.HEX
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
:020000020000FC
:020000040000FA
:02000004800179
:20000000000000000000004000000000000024400000000000000000000000000000F83F05
:10002000000000000000244000000000000000006C
:00000001FF

2 changes: 1 addition & 1 deletion examples/point_cloud_demo/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Point Cloud Demo

Use CANape to visualize a point cloud
Use CANape to visualize a point cloud

![CANape](CANape.png)

Expand Down
27 changes: 25 additions & 2 deletions examples/point_cloud_demo/point_cloud.a2l
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,17 @@
/begin MEMORY_SEGMENT
epk "" DATA FLASH INTERN 0x80000000 3 -1 -1 -1 -1 -1
/end MEMORY_SEGMENT


/begin MEMORY_SEGMENT
Params "" DATA FLASH INTERN 0x80010000 48 -1 -1 -1 -1 -1
/begin IF_DATA XCP
/begin SEGMENT /* index: */ 1 /* pages: */ 2 /* ext: */ 0 0 0
/begin CHECKSUM XCP_ADD_44 MAX_BLOCK_SIZE 0xFFFF EXTERNAL_FUNCTION "" /end CHECKSUM
/begin PAGE 0x0 ECU_ACCESS_DONT_CARE XCP_READ_ACCESS_DONT_CARE XCP_WRITE_ACCESS_DONT_CARE /end PAGE
/begin PAGE 0x1 ECU_ACCESS_DONT_CARE XCP_READ_ACCESS_DONT_CARE XCP_WRITE_ACCESS_NOT_ALLOWED /end PAGE
/end SEGMENT
/end IF_DATA
/end MEMORY_SEGMENT
/end MOD_PAR

/begin IF_DATA XCP
Expand Down Expand Up @@ -113,7 +123,20 @@

/end IF_DATA


/begin CHARACTERISTIC Params.ampl_x "" VALUE 0x80010008 F64 0 NO_COMPU_METHOD 0.001 100 PHYS_UNIT "m" /end CHARACTERISTIC
/begin CHARACTERISTIC Params.ampl_y "" VALUE 0x80010020 F64 0 NO_COMPU_METHOD 0.001 100 PHYS_UNIT "m" /end CHARACTERISTIC
/begin CHARACTERISTIC Params.period_x "" VALUE 0x80010000 F64 0 NO_COMPU_METHOD 0.001 10 PHYS_UNIT "s" /end CHARACTERISTIC
/begin CHARACTERISTIC Params.period_y "" VALUE 0x80010018 F64 0 NO_COMPU_METHOD 0.001 10 PHYS_UNIT "s" /end CHARACTERISTIC
/begin CHARACTERISTIC Params.phi_x "" VALUE 0x80010010 F64 0 NO_COMPU_METHOD 0 1 PHYS_UNIT "PI" /end CHARACTERISTIC
/begin CHARACTERISTIC Params.phi_y "" VALUE 0x80010028 F64 0 NO_COMPU_METHOD 0 2 PHYS_UNIT "PI" /end CHARACTERISTIC
/begin GROUP Params "" /begin REF_CHARACTERISTIC
Params.ampl_x
Params.ampl_y
Params.period_x
Params.period_y
Params.phi_x
Params.phi_y
/end REF_CHARACTERISTIC /end GROUP

/begin MEASUREMENT mainloop_counter1 "" A_UINT64 NO_COMPU_METHOD 0 0 0 1000000000000 PHYS_UNIT "" ECU_ADDRESS 0xD0 ECU_ADDRESS_EXTENSION 2 /begin IF_DATA XCP /begin DAQ_EVENT FIXED_EVENT_LIST EVENT 0 /end DAQ_EVENT /end IF_DATA /end MEASUREMENT
/begin CHARACTERISTIC point_cloud "point cloud demo" ASCII 0x0 U8 0 NO_COMPU_METHOD 0 255 READ_ONLY NUMBER 200 ECU_ADDRESS_EXTENSION 2
Expand Down
124 changes: 102 additions & 22 deletions examples/point_cloud_demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,22 @@ use std::{
time::{Duration, Instant},
};

use serde::Serialize;
use serde::{Deserialize, Serialize};

use xcp::*;
//use xcp_type_description_derive::XcpTypeDescription;
//-----------------------------------------------------------------------------
// Defaults

const BIND_ADDR: [u8; 4] = [192, 168, 0, 83]; // [172, 19, 11, 24]; // [192, 168, 0, 83]; // [127, 0, 0, 1];

const POINT_COUNT: usize = 3;
const POINT_COUNT: usize = 16;
const AMPL: f64 = 10.0;
const PERIOD: f64 = 3.0;
const PERIOD: f64 = 10.0;

//-----------------------------------------------------------------------------
// XCP

use xcp::*;
use xcp_type_description_derive::XcpTypeDescription;

//-----------------------------------------------------------------------------
// Application start time
Expand All @@ -27,6 +33,51 @@ lazy_static::lazy_static! {
static ref START_TIME: Instant = Instant::now();
}

//-----------------------------------------------------------------------------
// Parameters

#[derive(Debug, Clone, Copy, Serialize, Deserialize, XcpTypeDescription)]
struct Params {
#[unit = "s"]
#[min = "0.001"]
#[max = "10"]
period_x: f64,

#[unit = "m"]
#[min = "0.001"]
#[max = "100"]
ampl_x: f64,

#[unit = "PI"]
#[min = "0.0"]
#[max = "1.0"]
phi_x: f64,

#[unit = "s"]
#[min = "0.001"]
#[max = "10"]
period_y: f64,

#[unit = "m"]
#[min = "0.001"]
#[max = "100"]
ampl_y: f64,

#[unit = "PI"]
#[min = "0.0"]
#[max = "2.0"]
phi_y: f64,
}

const PARAMS: Params = Params {
period_x: PERIOD,
ampl_x: AMPL,
phi_x: 0.0,
period_y: PERIOD,
ampl_y: AMPL,
phi_y: 0.0,
};

//---------------------------------------------------------------------------------------

fn main() {
Expand All @@ -42,37 +93,66 @@ fn main() {
.start_server(XcpTransportLayer::Udp, BIND_ADDR, 5555, 8000 - 20 - 8)
.unwrap();

let mut event_point_cloud = daq_create_event!("point_cloud", 200);
let params = Xcp::create_calseg("Params", &PARAMS, true);

let mut event_point_cloud = daq_create_event!("point_cloud", POINT_COUNT * 12 + 8);

let mut mainloop_counter1: u64 = 0;
daq_register!(mainloop_counter1, event_point_cloud);

#[derive(Serialize)]
struct Point {
x: f32,
y: f32,
z: f32,
}

let mut point_cloud = Vec::with_capacity(4);
for _ in 0..POINT_COUNT {
point_cloud.push(Point {
x: 0.0,
y: 0.0,
z: 0.0,
});
}

let mut phi = 0.0;
let mut h = 0.0;
loop {
thread::sleep(Duration::from_millis(50));
thread::sleep(Duration::from_millis(10));
let t = START_TIME.elapsed().as_micros() as f64 * 0.000001; // s

mainloop_counter1 += 1;
if mainloop_counter1 > 256 {
mainloop_counter1 = 0;
}

// Serialize a struct into the event capture buffer
#[derive(Serialize)]
struct Point {
x: f32,
y: f32,
z: f32,
phi += 2.0 * PI / POINT_COUNT as f64 * 0.001;
if phi > 2.0 * PI / POINT_COUNT as f64 {
phi = 0.0;
}
h += 0.01;
if h > 30.0 {
h = 0.0;
}
let mut point_cloud = Vec::with_capacity(4);
for i in 0..POINT_COUNT {
// Calculate demo measurement variable depending on calibration parameters (sine signal with ampl and period)
let time = START_TIME.elapsed().as_micros() as f64 * 0.000001; // s

let x: f32 = (AMPL * (PI * time / PERIOD).sin()) as f32;
let y: f32 = (AMPL * (PI * time / PERIOD).cos()) as f32;
let z: f32 = i as f32;
point_cloud.push(Point { x, y, z });
for (i, p) in point_cloud.iter_mut().enumerate() {
let a_x: f64 = params.ampl_x;
let a_y: f64 = params.ampl_y;
let omega_x = 2.0 * PI / params.period_x;
let omega_y = 2.0 * PI / params.period_y;
let phi_x = 1.0 * PI / POINT_COUNT as f64 * i as f64 + phi;
let phi_y = 1.0 * PI / POINT_COUNT as f64 * i as f64 + phi;

p.x = (a_x * (omega_x * t + phi_x).cos()) as f32;
p.y = (a_y * (omega_y * t + phi_y).sin()) as f32;
p.z = h * (i as f32 * 0.03);
}

// Serialize into the event capture buffer
daq_serialize!(point_cloud, event_point_cloud, "point cloud demo");
event_point_cloud.trigger();

params.sync();
xcp.write_a2l();
}
}
27 changes: 25 additions & 2 deletions examples/point_cloud_demo/xcp_lite_autodetect.a2l
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,17 @@
/begin MEMORY_SEGMENT
epk "" DATA FLASH INTERN 0x80000000 3 -1 -1 -1 -1 -1
/end MEMORY_SEGMENT


/begin MEMORY_SEGMENT
Params "" DATA FLASH INTERN 0x80010000 48 -1 -1 -1 -1 -1
/begin IF_DATA XCP
/begin SEGMENT /* index: */ 1 /* pages: */ 2 /* ext: */ 0 0 0
/begin CHECKSUM XCP_ADD_44 MAX_BLOCK_SIZE 0xFFFF EXTERNAL_FUNCTION "" /end CHECKSUM
/begin PAGE 0x0 ECU_ACCESS_DONT_CARE XCP_READ_ACCESS_DONT_CARE XCP_WRITE_ACCESS_DONT_CARE /end PAGE
/begin PAGE 0x1 ECU_ACCESS_DONT_CARE XCP_READ_ACCESS_DONT_CARE XCP_WRITE_ACCESS_NOT_ALLOWED /end PAGE
/end SEGMENT
/end IF_DATA
/end MEMORY_SEGMENT
/end MOD_PAR

/begin IF_DATA XCP
Expand Down Expand Up @@ -113,7 +123,20 @@

/end IF_DATA


/begin CHARACTERISTIC Params.ampl_x "" VALUE 0x80010008 F64 0 NO_COMPU_METHOD 0.001 100 PHYS_UNIT "m" /end CHARACTERISTIC
/begin CHARACTERISTIC Params.ampl_y "" VALUE 0x80010020 F64 0 NO_COMPU_METHOD 0.001 100 PHYS_UNIT "m" /end CHARACTERISTIC
/begin CHARACTERISTIC Params.period_x "" VALUE 0x80010000 F64 0 NO_COMPU_METHOD 0.001 10 PHYS_UNIT "s" /end CHARACTERISTIC
/begin CHARACTERISTIC Params.period_y "" VALUE 0x80010018 F64 0 NO_COMPU_METHOD 0.001 10 PHYS_UNIT "s" /end CHARACTERISTIC
/begin CHARACTERISTIC Params.phi_x "" VALUE 0x80010010 F64 0 NO_COMPU_METHOD 0 1 PHYS_UNIT "PI" /end CHARACTERISTIC
/begin CHARACTERISTIC Params.phi_y "" VALUE 0x80010028 F64 0 NO_COMPU_METHOD 0 2 PHYS_UNIT "PI" /end CHARACTERISTIC
/begin GROUP Params "" /begin REF_CHARACTERISTIC
Params.ampl_x
Params.ampl_y
Params.period_x
Params.period_y
Params.phi_x
Params.phi_y
/end REF_CHARACTERISTIC /end GROUP

/begin MEASUREMENT mainloop_counter1 "" A_UINT64 NO_COMPU_METHOD 0 0 0 1000000000000 PHYS_UNIT "" ECU_ADDRESS 0xD0 ECU_ADDRESS_EXTENSION 2 /begin IF_DATA XCP /begin DAQ_EVENT FIXED_EVENT_LIST EVENT 0 /end DAQ_EVENT /end IF_DATA /end MEASUREMENT
/begin CHARACTERISTIC point_cloud "point cloud demo" ASCII 0x0 U8 0 NO_COMPU_METHOD 0 255 READ_ONLY NUMBER 200 ECU_ADDRESS_EXTENSION 2
Expand Down
23 changes: 6 additions & 17 deletions src/daq/daq_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Xcp {
x_dim,
y_dim,
event,
0, // byte_offset
0, // byte_offset
0,
1.0, // factor
0.0, // offset
Expand Down Expand Up @@ -180,10 +180,7 @@ impl<const N: usize> DaqEvent<N> {
let b = &self.buffer as *const _ as usize; // base address
debug!(
"add_stack: {} {:?} ptr={:p} base={:p}",
name,
datatype,
ptr,
&self.buffer as *const _
name, datatype, ptr, &self.buffer as *const _
);
let o: i64 = p as i64 - b as i64; // variable - base address
assert!(
Expand All @@ -210,7 +207,6 @@ impl<const N: usize> DaqEvent<N> {
));
}


/// Associate a variable on stack to this DaqEvent and register it
#[allow(clippy::too_many_arguments)]
pub fn add_heap(
Expand All @@ -225,14 +221,8 @@ impl<const N: usize> DaqEvent<N> {
unit: &'static str,
comment: &'static str,
) {
debug!(
"add_heap: {} {:?} ptr={:p} ",
name,
datatype,
ptr,

);

debug!("add_heap: {} {:?} ptr={:p} ", name, datatype, ptr,);

Xcp::get()
.get_registry()
.lock()
Expand All @@ -251,7 +241,6 @@ impl<const N: usize> DaqEvent<N> {
unit,
));
}

}

//-----------------------------------------------------------------------------
Expand All @@ -267,13 +256,13 @@ impl<const N: usize> DaqEvent<N> {
#[macro_export]
macro_rules! daq_create_event {
// Without capture buffer
( $name:expr, $capacity: literal ) => {{
( $name:expr, $capacity: expr ) => {{
// Scope for lazy static XCP_EVENT__, create the XCP event only once
lazy_static::lazy_static! {
static ref XCP_EVENT__: XcpEvent = Xcp::get().create_event($name, false);
}
// Create the DAQ event every time the thread is running through this code
DaqEvent::<$capacity>::new_from(&XCP_EVENT__)
DaqEvent::<{ $capacity }>::new_from(&XCP_EVENT__)
}};
// With capture buffer capacity
( $name:expr ) => {{
Expand Down

0 comments on commit 9b5175e

Please sign in to comment.