@@ -79,14 +79,12 @@ fn main() {
7979 println ! ( "🚀 Mock node is running!" ) ;
8080 println ! ( " Waiting for SDO requests on COB-ID 0x{:03X}..." , 0x600 + node_id as u16 ) ;
8181 println ! ( " Broadcasting TPDO1 on COB-ID 0x{:03X} every 100ms" , 0x180 + node_id as u16 ) ;
82+ println ! ( " TPDO1 contains: CabinTemperature (0x2000:01), OutsideTemperature (0x2000:02)" ) ;
8283 println ! ( " Press Ctrl+C to stop\n " ) ;
8384
8485 // TPDO broadcasting state
8586 let mut last_tpdo_time = Instant :: now ( ) ;
8687 let tpdo_interval = Duration :: from_millis ( 100 ) ;
87- let mut temperature: u16 = 2350 ; // 23.50°C
88- let mut pressure: u16 = 1013 ; // 1013 hPa
89- let mut status: u8 = 1 ; // OK
9088
9189 // Main loop: listen for CAN frames and respond to SDO requests
9290 loop {
@@ -112,35 +110,36 @@ fn main() {
112110
113111 // Broadcast TPDO periodically
114112 if last_tpdo_time. elapsed ( ) >= tpdo_interval {
115- // Update test values (simulate changing sensor data)
116- temperature = ( temperature + 1 ) % 3000 ; // Varies from 0 to 30.00°C
117- pressure = 1000 + ( pressure - 1000 + 1 ) % 50 ; // Varies from 1000 to 1050 hPa
118- status = if status == 1 { 2 } else { 1 } ; // Toggle between 1 and 2
119-
120- // Create TPDO frame
121- // TPDO1 COB-ID = 0x180 + node_id
122- // Mapping: Temperature (16-bit), Pressure (16-bit), Status (8-bit)
123- let tpdo_cob_id = 0x180 + node_id as u16 ;
124-
125- if let Some ( std_id) = StandardId :: new ( tpdo_cob_id) {
126- let mut data = [ 0u8 ; 8 ] ;
127-
128- // Pack data in little-endian format
129- data[ 0 ] = ( temperature & 0xFF ) as u8 ;
130- data[ 1 ] = ( ( temperature >> 8 ) & 0xFF ) as u8 ;
131- data[ 2 ] = ( pressure & 0xFF ) as u8 ;
132- data[ 3 ] = ( ( pressure >> 8 ) & 0xFF ) as u8 ;
133- data[ 4 ] = status;
134- // Remaining bytes are padding (0)
135-
136- if let Some ( frame) = CanFrame :: new ( std_id, & data[ ..5 ] ) {
137- if let Err ( e) = socket. write_frame ( & frame) {
138- eprintln ! ( "⚠ Failed to send TPDO: {}" , e) ;
139- } else {
140- print ! ( "📤 TPDO1: Temp={:.2}°C, Press={}hPa, Status={}\r " ,
141- temperature as f32 / 100.0 , pressure, status) ;
142- use std:: io:: Write ;
143- std:: io:: stdout ( ) . flush ( ) . ok ( ) ;
113+ // Read current values from Object Dictionary
114+ // TPDO1 mapping: 0x2000:01 (CabinTemperature, Real32), 0x2000:02 (OutsideTemperature, Real32)
115+ let cabin_temp = sdo_server. object_dict ( ) . get ( 0x2000 , 0x01 ) ;
116+ let outside_temp = sdo_server. object_dict ( ) . get ( 0x2000 , 0x02 ) ;
117+
118+ if let ( Some ( ( cabin_data, _) ) , Some ( ( outside_data, _) ) ) = ( cabin_temp, outside_temp) {
119+ // Create TPDO frame
120+ // TPDO1 COB-ID = 0x180 + node_id
121+ let tpdo_cob_id = 0x180 + node_id as u16 ;
122+
123+ if let Some ( std_id) = StandardId :: new ( tpdo_cob_id) {
124+ let mut data = [ 0u8 ; 8 ] ;
125+
126+ // Pack data according to TPDO mapping
127+ // Bytes 0-3: CabinTemperature (Real32, little-endian)
128+ data[ 0 ..4 ] . copy_from_slice ( & cabin_data[ ..4 ] ) ;
129+ // Bytes 4-7: OutsideTemperature (Real32, little-endian)
130+ data[ 4 ..8 ] . copy_from_slice ( & outside_data[ ..4 ] ) ;
131+
132+ if let Some ( frame) = CanFrame :: new ( std_id, & data) {
133+ if let Err ( e) = socket. write_frame ( & frame) {
134+ eprintln ! ( "⚠ Failed to send TPDO: {}" , e) ;
135+ } else {
136+ // Decode for display
137+ let cabin_f32 = f32:: from_le_bytes ( [ cabin_data[ 0 ] , cabin_data[ 1 ] , cabin_data[ 2 ] , cabin_data[ 3 ] ] ) ;
138+ let outside_f32 = f32:: from_le_bytes ( [ outside_data[ 0 ] , outside_data[ 1 ] , outside_data[ 2 ] , outside_data[ 3 ] ] ) ;
139+ print ! ( "📤 TPDO1: CabinTemp={:.2}°C, OutsideTemp={:.2}°C\r " , cabin_f32, outside_f32) ;
140+ use std:: io:: Write ;
141+ std:: io:: stdout ( ) . flush ( ) . ok ( ) ;
142+ }
144143 }
145144 }
146145 }
0 commit comments