Updating comments
This commit is contained in:
parent
9b3bb62811
commit
6c87db3778
|
@ -13,10 +13,10 @@ import logging
|
||||||
# Representation of a single UDP packet transmitted by Stabilizer.
|
# Representation of a single UDP packet transmitted by Stabilizer.
|
||||||
Packet = collections.namedtuple('Packet', ['index', 'adc', 'dac'])
|
Packet = collections.namedtuple('Packet', ['index', 'adc', 'dac'])
|
||||||
|
|
||||||
Format = collections.namedtuple('Format', ['batch_size', 'batches_per_frame'])
|
Format = collections.namedtuple('Format', ['batch_size', 'length'])
|
||||||
|
|
||||||
FORMAT = {
|
FORMAT = {
|
||||||
0: Format(8, 255)
|
0: Format(8, 964)
|
||||||
}
|
}
|
||||||
|
|
||||||
class Timer:
|
class Timer:
|
||||||
|
@ -94,30 +94,31 @@ class PacketParser:
|
||||||
if len(self.buf) < 4:
|
if len(self.buf) < 4:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
start_id, format_id = struct.unpack_from('!HH', self.buf)
|
start_id, format_id = struct.unpack_from('<HH', self.buf)
|
||||||
|
|
||||||
frame_format = FORMAT[format_id]
|
frame_format = FORMAT[format_id]
|
||||||
packet_size = 4 + frame_format.batch_size * frame_format.batches_per_frame * 8
|
|
||||||
|
|
||||||
if len(self.buf) < packet_size:
|
if len(self.buf) < frame_format.length:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
self.buf = self.buf[4:]
|
self.buf = self.buf[4:]
|
||||||
|
|
||||||
|
batches_per_frame = int((frame_format.length - 4) / (frame_format.batch_size * 8))
|
||||||
|
|
||||||
packets = []
|
packets = []
|
||||||
for offset in range(num_blocks):
|
for offset in range(batches_per_frame):
|
||||||
adcs_dacs = struct.unpack_from(f'!{4 * data_size}H', self.buf)
|
adcs_dacs = struct.unpack_from(f'<{4 * frame_format.batch_size}H', self.buf)
|
||||||
adc = [
|
adc = [
|
||||||
adcs_dacs[0:data_size],
|
adcs_dacs[0:frame_format.batch_size],
|
||||||
adcs_dacs[data_size:2*data_size],
|
adcs_dacs[frame_format.batch_size:2*frame_format.batch_size],
|
||||||
]
|
]
|
||||||
|
|
||||||
dac = [
|
dac = [
|
||||||
adcs_dacs[2*data_size: 3*data_size],
|
adcs_dacs[2*frame_format.batch_size: 3*frame_format.batch_size],
|
||||||
adcs_dacs[3*data_size:],
|
adcs_dacs[3*frame_format.batch_size:],
|
||||||
]
|
]
|
||||||
|
|
||||||
self.buf = self.buf[8*data_size:]
|
self.buf = self.buf[8*frame_format.batch_size:]
|
||||||
packets.append(Packet(start_id + offset, adc, dac))
|
packets.append(Packet(start_id + offset, adc, dac))
|
||||||
|
|
||||||
return packets
|
return packets
|
||||||
|
@ -155,7 +156,7 @@ def main():
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
# Receive any data over UDP and parse it.
|
# Receive any data over UDP and parse it.
|
||||||
data = connection.recv(4096)
|
data = connection.recv(4096 * 4)
|
||||||
if data and not timer.is_started():
|
if data and not timer.is_started():
|
||||||
timer.start()
|
timer.start()
|
||||||
|
|
||||||
|
@ -166,7 +167,7 @@ def main():
|
||||||
|
|
||||||
# Handle any dropped packets.
|
# Handle any dropped packets.
|
||||||
if not check_index(last_index, packet.index):
|
if not check_index(last_index, packet.index):
|
||||||
print(hex(last_index), hex(packet.index))
|
print('Drop from ', hex(last_index), hex(packet.index))
|
||||||
if packet.index < (last_index + 1):
|
if packet.index < (last_index + 1):
|
||||||
dropped = packet.index + 65536 - (last_index + 1)
|
dropped = packet.index + 65536 - (last_index + 1)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -308,17 +308,23 @@ const APP: () = {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stream the data.
|
// Stream the data.
|
||||||
generator.add::<_, { SAMPLE_BUFFER_SIZE * 8 }>(0, |buf| {
|
generator.add::<_, { SAMPLE_BUFFER_SIZE * 8 }>(0, |buf| unsafe {
|
||||||
let mut offset = 0;
|
let dst = buf.as_ptr() as usize as *mut u16;
|
||||||
for device in [adc_samples.iter(), dac_samples.iter()] {
|
|
||||||
for channel in device {
|
let adc0 = &adc_samples[0][0] as *const u16;
|
||||||
for sample in channel.iter() {
|
core::ptr::copy_nonoverlapping(adc0, dst, SAMPLE_BUFFER_SIZE);
|
||||||
buf[offset..offset + 2]
|
|
||||||
.copy_from_slice(&sample.to_ne_bytes());
|
let dst = dst.add(SAMPLE_BUFFER_SIZE);
|
||||||
offset += 2;
|
let adc1 = &adc_samples[1][0] as *const u16;
|
||||||
}
|
core::ptr::copy_nonoverlapping(adc1, dst, SAMPLE_BUFFER_SIZE);
|
||||||
}
|
|
||||||
}
|
let dst = dst.add(SAMPLE_BUFFER_SIZE);
|
||||||
|
let dac0 = &dac_samples[0][0] as *const u16;
|
||||||
|
core::ptr::copy_nonoverlapping(dac0, dst, SAMPLE_BUFFER_SIZE);
|
||||||
|
|
||||||
|
let dst = dst.add(SAMPLE_BUFFER_SIZE);
|
||||||
|
let dac1 = &dac_samples[1][0] as *const u16;
|
||||||
|
core::ptr::copy_nonoverlapping(dac1, dst, SAMPLE_BUFFER_SIZE);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Update telemetry measurements.
|
// Update telemetry measurements.
|
||||||
|
|
|
@ -36,11 +36,6 @@ pub struct NetStorage {
|
||||||
[Option<(smoltcp::wire::IpAddress, smoltcp::iface::Neighbor)>; 8],
|
[Option<(smoltcp::wire::IpAddress, smoltcp::iface::Neighbor)>; 8],
|
||||||
pub routes_cache:
|
pub routes_cache:
|
||||||
[Option<(smoltcp::wire::IpCidr, smoltcp::iface::Route)>; 8],
|
[Option<(smoltcp::wire::IpCidr, smoltcp::iface::Route)>; 8],
|
||||||
|
|
||||||
pub dhcp_rx_metadata: [smoltcp::socket::RawPacketMetadata; 1],
|
|
||||||
pub dhcp_tx_metadata: [smoltcp::socket::RawPacketMetadata; 1],
|
|
||||||
pub dhcp_tx_storage: [u8; 600],
|
|
||||||
pub dhcp_rx_storage: [u8; 600],
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct UdpSocketStorage {
|
pub struct UdpSocketStorage {
|
||||||
|
@ -94,10 +89,6 @@ impl Default for NetStorage {
|
||||||
sockets: [None, None, None, None, None, None],
|
sockets: [None, None, None, None, None, None],
|
||||||
tcp_socket_storage: [TcpSocketStorage::new(); NUM_TCP_SOCKETS],
|
tcp_socket_storage: [TcpSocketStorage::new(); NUM_TCP_SOCKETS],
|
||||||
udp_socket_storage: [UdpSocketStorage::new(); NUM_UDP_SOCKETS],
|
udp_socket_storage: [UdpSocketStorage::new(); NUM_UDP_SOCKETS],
|
||||||
dhcp_tx_storage: [0; 600],
|
|
||||||
dhcp_rx_storage: [0; 600],
|
|
||||||
dhcp_rx_metadata: [smoltcp::socket::RawPacketMetadata::EMPTY; 1],
|
|
||||||
dhcp_tx_metadata: [smoltcp::socket::RawPacketMetadata::EMPTY; 1],
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,9 +19,11 @@ use heapless::pool::{Box, Init, Pool, Uninit};
|
||||||
|
|
||||||
use super::NetworkReference;
|
use super::NetworkReference;
|
||||||
|
|
||||||
const FRAME_COUNT: usize = 4;
|
const FRAME_COUNT: usize = 6;
|
||||||
|
const FRAME_SIZE: usize = 1024;
|
||||||
|
|
||||||
static mut FRAME_DATA: [u8; 5200] = [0; 5200];
|
static mut FRAME_DATA: [u8; FRAME_SIZE * FRAME_COUNT] =
|
||||||
|
[0; FRAME_SIZE * FRAME_COUNT];
|
||||||
|
|
||||||
/// Represents the destination for the UDP stream to send data to.
|
/// Represents the destination for the UDP stream to send data to.
|
||||||
///
|
///
|
||||||
|
@ -70,7 +72,7 @@ pub fn setup_streaming(
|
||||||
let (producer, consumer) = queue.split();
|
let (producer, consumer) = queue.split();
|
||||||
|
|
||||||
let frame_pool =
|
let frame_pool =
|
||||||
cortex_m::singleton!(: Pool<[u8; 1024]>= Pool::new()).unwrap();
|
cortex_m::singleton!(: Pool<[u8; FRAME_SIZE]>= Pool::new()).unwrap();
|
||||||
|
|
||||||
// Note(unsafe): We guarantee that FRAME_DATA is only accessed once in this function.
|
// Note(unsafe): We guarantee that FRAME_DATA is only accessed once in this function.
|
||||||
let memory = unsafe { &mut FRAME_DATA };
|
let memory = unsafe { &mut FRAME_DATA };
|
||||||
|
@ -86,13 +88,13 @@ pub fn setup_streaming(
|
||||||
struct StreamFrame {
|
struct StreamFrame {
|
||||||
format: u16,
|
format: u16,
|
||||||
sequence_number: u16,
|
sequence_number: u16,
|
||||||
buffer: Box<[u8; 1024], Init>,
|
buffer: Box<[u8; FRAME_SIZE], Init>,
|
||||||
offset: usize,
|
offset: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StreamFrame {
|
impl StreamFrame {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
buffer: Box<[u8; 1024], Uninit>,
|
buffer: Box<[u8; FRAME_SIZE], Uninit>,
|
||||||
format: u16,
|
format: u16,
|
||||||
sequence_number: u16,
|
sequence_number: u16,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
|
@ -132,7 +134,7 @@ impl StreamFrame {
|
||||||
/// The data generator for a stream.
|
/// The data generator for a stream.
|
||||||
pub struct FrameGenerator {
|
pub struct FrameGenerator {
|
||||||
queue: Producer<'static, StreamFrame, FRAME_COUNT>,
|
queue: Producer<'static, StreamFrame, FRAME_COUNT>,
|
||||||
pool: &'static Pool<[u8; 1024]>,
|
pool: &'static Pool<[u8; FRAME_SIZE]>,
|
||||||
current_frame: Option<StreamFrame>,
|
current_frame: Option<StreamFrame>,
|
||||||
sequence_number: u16,
|
sequence_number: u16,
|
||||||
}
|
}
|
||||||
|
@ -140,7 +142,7 @@ pub struct FrameGenerator {
|
||||||
impl FrameGenerator {
|
impl FrameGenerator {
|
||||||
fn new(
|
fn new(
|
||||||
queue: Producer<'static, StreamFrame, FRAME_COUNT>,
|
queue: Producer<'static, StreamFrame, FRAME_COUNT>,
|
||||||
pool: &'static Pool<[u8; 1024]>,
|
pool: &'static Pool<[u8; FRAME_SIZE]>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
queue,
|
queue,
|
||||||
|
@ -172,11 +174,15 @@ impl FrameGenerator {
|
||||||
self.current_frame.as_mut().unwrap().add_batch::<_, T>(f);
|
self.current_frame.as_mut().unwrap().add_batch::<_, T>(f);
|
||||||
|
|
||||||
if self.current_frame.as_ref().unwrap().is_full::<T>() {
|
if self.current_frame.as_ref().unwrap().is_full::<T>() {
|
||||||
// If we fail to enqueue the frame, free the underlying buffer.
|
if self
|
||||||
match self.queue.enqueue(self.current_frame.take().unwrap()) {
|
.queue
|
||||||
Err(frame) => self.pool.free(frame.buffer),
|
.enqueue(self.current_frame.take().unwrap())
|
||||||
_ => {}
|
.is_err()
|
||||||
};
|
{
|
||||||
|
// Given that the queue is the same size as the number of frames available, this
|
||||||
|
// should never occur.
|
||||||
|
panic!("Frame enqueue failure")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -189,7 +195,7 @@ pub struct DataStream {
|
||||||
stack: NetworkReference,
|
stack: NetworkReference,
|
||||||
socket: Option<<NetworkReference as UdpClientStack>::UdpSocket>,
|
socket: Option<<NetworkReference as UdpClientStack>::UdpSocket>,
|
||||||
queue: Consumer<'static, StreamFrame, FRAME_COUNT>,
|
queue: Consumer<'static, StreamFrame, FRAME_COUNT>,
|
||||||
frame_pool: &'static Pool<[u8; 1024]>,
|
frame_pool: &'static Pool<[u8; FRAME_SIZE]>,
|
||||||
remote: SocketAddr,
|
remote: SocketAddr,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -203,7 +209,7 @@ impl DataStream {
|
||||||
fn new(
|
fn new(
|
||||||
stack: NetworkReference,
|
stack: NetworkReference,
|
||||||
consumer: Consumer<'static, StreamFrame, FRAME_COUNT>,
|
consumer: Consumer<'static, StreamFrame, FRAME_COUNT>,
|
||||||
frame_pool: &'static Pool<[u8; 1024]>,
|
frame_pool: &'static Pool<[u8; FRAME_SIZE]>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
stack,
|
stack,
|
||||||
|
|
Loading…
Reference in New Issue