Skip to content

Commit

Permalink
Ad support for pixel masks
Browse files Browse the repository at this point in the history
  • Loading branch information
aMarcireau committed Sep 25, 2023
1 parent 5d6c8eb commit 331c997
Show file tree
Hide file tree
Showing 48 changed files with 274 additions and 127 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,11 @@ class UsbConfiguration:

## More examples

See _python/tests_ for different usage examples. `python/tests/display.py` implements a live event viewer with exponential decays caculated by the GPU. It requires vispy and glfw (`pip install vispy glfw`).
See _python/examples_ for different usage examples.

Some tests generate plots and require Plotly (`pip install plotly pandas kaleido`).
_python/examples/any_display.py_ implements a live event viewer with exponential decays caculated by the GPU. It requires vispy and glfw (`pip install vispy glfw`).

_python/examples/evk4_plot_hot_pixels_ generates plots and require Plotly (`pip install plotly pandas kaleido`).

## Contribute

Expand Down
2 changes: 1 addition & 1 deletion drivers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = [
"Alexandre Marcireau",
]
description = "Neuromorphic devices drivers"
version = "0.6.0"
version = "0.7.0"
edition = "2021"
license-file = "../LICENSE"
homepage = "https://github.com/neuromorphicsystems/neuromorphic-rs/"
Expand Down
121 changes: 57 additions & 64 deletions drivers/src/devices/prophesee_evk4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub struct Configuration {
pub biases: Biases,
pub x_mask: [u64; 20],
pub y_mask: [u64; 12],
pub pixel_mask: [u64; 21],
pub mask_intersection_only: bool,
pub enable_external_trigger: bool,
pub clock: Clock,
Expand Down Expand Up @@ -67,6 +68,9 @@ pub enum Error {

#[error("bytes mismatch while reading register {0}")]
RegisterReadMismatch(u32),

#[error("unsupported mask code ({code}) for pixel mask {offset}")]
PixelMask { code: u32, offset: u32 },
}

impl From<rusb::Error> for Error {
Expand Down Expand Up @@ -110,6 +114,7 @@ impl device::Usb for Device {
},
x_mask: [0; 20],
y_mask: [0; 12],
pixel_mask: [0; 21],
mask_intersection_only: false,
enable_external_trigger: true,
clock: Clock::Internal,
Expand Down Expand Up @@ -973,6 +978,51 @@ fn update_configuration(
}
.write(handle)?;
}
if match previous_configuration {
Some(previous_configuration) => {
previous_configuration.pixel_mask != configuration.pixel_mask
}
None => true,
} {
for offset in 0u32..64u32 {
let code = if offset < 63 {
(((configuration.pixel_mask[(offset / 3) as usize]) >> ((offset % 3) * 21))
& 0x1fffff) as u32
} else {
let mut code: u32 = 0;
for bit in 0..21 {
code |= ((configuration.pixel_mask[bit] >> 63) << bit) as u32;
}
code
};
if code == 0 {
DigitalMask {
x: 0,
reserved_11_16: 0,
y: 0,
reserved_26_31: 0,
enable: 0,
}
.offset(offset)
.write(handle)?;
} else {
let x = (code - 1) % 1280;
let y = 720 - 1 - (code - 1) / 1280;
if x >= 1280 || y >= 720 {
return Err(Error::PixelMask { code, offset });
}
DigitalMask {
x,
reserved_11_16: 0,
y,
reserved_26_31: 0,
enable: 1,
}
.offset(offset)
.write(handle)?;
}
}
}
Ok(())
}

Expand Down Expand Up @@ -1397,70 +1447,13 @@ register! { AreaY3Addr, 0x904C, { value: 0..32 } }
register! { AreaY4Addr, 0x9050, { value: 0..32 } }
register! { CounterCtrl, 0x9054, { value: 0..32 } }
register! { CounterTimerThreshold, 0x9058, { value: 0..32 } }
register! { DigitalMaskPixel00, 0x9100, { value: 0..32 } }
register! { DigitalMaskPixel01, 0x9104, { value: 0..32 } }
register! { DigitalMaskPixel02, 0x9108, { value: 0..32 } }
register! { DigitalMaskPixel03, 0x910C, { value: 0..32 } }
register! { DigitalMaskPixel04, 0x9110, { value: 0..32 } }
register! { DigitalMaskPixel05, 0x9114, { value: 0..32 } }
register! { DigitalMaskPixel06, 0x9118, { value: 0..32 } }
register! { DigitalMaskPixel07, 0x911C, { value: 0..32 } }
register! { DigitalMaskPixel08, 0x9120, { value: 0..32 } }
register! { DigitalMaskPixel09, 0x9124, { value: 0..32 } }
register! { DigitalMaskPixel10, 0x9128, { value: 0..32 } }
register! { DigitalMaskPixel11, 0x912C, { value: 0..32 } }
register! { DigitalMaskPixel12, 0x9130, { value: 0..32 } }
register! { DigitalMaskPixel13, 0x9134, { value: 0..32 } }
register! { DigitalMaskPixel14, 0x9138, { value: 0..32 } }
register! { DigitalMaskPixel15, 0x913C, { value: 0..32 } }
register! { DigitalMaskPixel16, 0x9140, { value: 0..32 } }
register! { DigitalMaskPixel17, 0x9144, { value: 0..32 } }
register! { DigitalMaskPixel18, 0x9148, { value: 0..32 } }
register! { DigitalMaskPixel19, 0x914C, { value: 0..32 } }
register! { DigitalMaskPixel20, 0x9150, { value: 0..32 } }
register! { DigitalMaskPixel21, 0x9154, { value: 0..32 } }
register! { DigitalMaskPixel22, 0x9158, { value: 0..32 } }
register! { DigitalMaskPixel23, 0x915C, { value: 0..32 } }
register! { DigitalMaskPixel24, 0x9160, { value: 0..32 } }
register! { DigitalMaskPixel25, 0x9164, { value: 0..32 } }
register! { DigitalMaskPixel26, 0x9168, { value: 0..32 } }
register! { DigitalMaskPixel27, 0x916C, { value: 0..32 } }
register! { DigitalMaskPixel28, 0x9170, { value: 0..32 } }
register! { DigitalMaskPixel29, 0x9174, { value: 0..32 } }
register! { DigitalMaskPixel30, 0x9178, { value: 0..32 } }
register! { DigitalMaskPixel31, 0x917C, { value: 0..32 } }
register! { DigitalMaskPixel32, 0x9180, { value: 0..32 } }
register! { DigitalMaskPixel33, 0x9184, { value: 0..32 } }
register! { DigitalMaskPixel34, 0x9188, { value: 0..32 } }
register! { DigitalMaskPixel35, 0x918C, { value: 0..32 } }
register! { DigitalMaskPixel36, 0x9190, { value: 0..32 } }
register! { DigitalMaskPixel37, 0x9194, { value: 0..32 } }
register! { DigitalMaskPixel38, 0x9198, { value: 0..32 } }
register! { DigitalMaskPixel39, 0x919C, { value: 0..32 } }
register! { DigitalMaskPixel40, 0x91A0, { value: 0..32 } }
register! { DigitalMaskPixel41, 0x91A4, { value: 0..32 } }
register! { DigitalMaskPixel42, 0x91A8, { value: 0..32 } }
register! { DigitalMaskPixel43, 0x91AC, { value: 0..32 } }
register! { DigitalMaskPixel44, 0x91B0, { value: 0..32 } }
register! { DigitalMaskPixel45, 0x91B4, { value: 0..32 } }
register! { DigitalMaskPixel46, 0x91B8, { value: 0..32 } }
register! { DigitalMaskPixel47, 0x91BC, { value: 0..32 } }
register! { DigitalMaskPixel48, 0x91C0, { value: 0..32 } }
register! { DigitalMaskPixel49, 0x91C4, { value: 0..32 } }
register! { DigitalMaskPixel50, 0x91C8, { value: 0..32 } }
register! { DigitalMaskPixel51, 0x91CC, { value: 0..32 } }
register! { DigitalMaskPixel52, 0x91D0, { value: 0..32 } }
register! { DigitalMaskPixel53, 0x91D4, { value: 0..32 } }
register! { DigitalMaskPixel54, 0x91D8, { value: 0..32 } }
register! { DigitalMaskPixel55, 0x91DC, { value: 0..32 } }
register! { DigitalMaskPixel56, 0x91E0, { value: 0..32 } }
register! { DigitalMaskPixel57, 0x91E4, { value: 0..32 } }
register! { DigitalMaskPixel58, 0x91E8, { value: 0..32 } }
register! { DigitalMaskPixel59, 0x91EC, { value: 0..32 } }
register! { DigitalMaskPixel60, 0x91F0, { value: 0..32 } }
register! { DigitalMaskPixel61, 0x91F4, { value: 0..32 } }
register! { DigitalMaskPixel62, 0x91F8, { value: 0..32 } }
register! { DigitalMaskPixel63, 0x91FC, { value: 0..32 } }
register! { DigitalMask, 0x9100, {
x: 0..11,
reserved_11_16: 11..16,
y: 16..26,
reserved_26_31: 26..31,
enable: 31..32,
} }
register! { AreaCnt00, 0x9200, { value: 0..32 } }
register! { AreaCnt01, 0x9204, { value: 0..32 } }
register! { AreaCnt02, 0x9208, { value: 0..32 } }
Expand Down
3 changes: 2 additions & 1 deletion python/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ tests/*.raw
*.so
*.pyd
__pycache__
tests/hot_pixels
tests/hot_pixels/
tests/*.png
2 changes: 1 addition & 1 deletion python/tests/display.py → python/examples/any_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def __init__(
1,
)
vispy.gloo.set_clear_color("#292929")
self.timer = vispy.app.Timer(FRAME_DURATION, connect=self.update, start=True)
self.timer = vispy.app.Timer(FRAME_DURATION, connect=self.update, start=True) # type: ignore
self.show()

def on_resize(self, event):
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def __init__(
1,
)
vispy.gloo.set_clear_color("#292929")
self.timer = vispy.app.Timer(FRAME_DURATION, connect=self.update, start=True)
self.timer = vispy.app.Timer(FRAME_DURATION, connect=self.update, start=True) # type: ignore
self.show()

def on_resize(self, event):
Expand Down
File renamed without changes.
Binary file added python/examples/evk4_mask.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 48 additions & 0 deletions python/examples/evk4_mask.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import pathlib

import neuromorphic_drivers as nd
import numpy as np
import plotly.express

dirname = pathlib.Path(__file__).resolve().parent

pixel_mask = nd.PropheseeEvk4PixelMask()
pixel_mask.set(63, 1026, 664)
nd.write_mask_as_png(
pixel_mask.pixels(),
dirname / "evk4_mask.png",
)

print(pixel_mask.pixel_mask())

counts = np.zeros(
(nd.prophesee_evk4.Properties().width, nd.prophesee_evk4.Properties().height),
dtype=np.uint64,
)

with nd.open(
configuration=nd.prophesee_evk4.Configuration(
biases=nd.prophesee_evk4.Biases(
diff_off=50,
),
pixel_mask=pixel_mask.pixel_mask(),
)
) as device:
for status, packet in device:
if "dvs_events" in packet:
np.add.at(counts, (packet["dvs_events"]["x"], packet["dvs_events"]["y"]), 1)
if packet["dvs_events"]["t"][-1] > 2000000:
break

log_counts = np.flipud(np.log((counts + 1).astype(np.float64)).transpose())
figure = plotly.express.imshow(log_counts, color_continuous_scale="thermal")
figure.update_layout(
width=nd.prophesee_evk4.Properties().width,
height=nd.prophesee_evk4.Properties().height,
margin=dict(l=0, r=0, b=0, t=0),
xaxis={"visible": False, "showticklabels": False},
yaxis={"visible": False, "showticklabels": False},
showlegend=False,
)
figure.update_coloraxes(showscale=False)
figure.write_image(dirname / f"evk4_mask_accumulated.png")
Binary file added python/examples/evk4_mask_accumulated.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@


for diff_off, counts in diff_off_to_counts.items():
log_counts = np.log((counts + 1).astype(np.float64)).transpose()
log_counts = np.flipud(np.log((counts + 1).astype(np.float64)).transpose())
figure = plotly.express.imshow(log_counts, color_continuous_scale="thermal")
figure.update_layout(
width=1280,
Expand Down
File renamed without changes.
Binary file added python/examples/hot_pixels/diff_off_46_count.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python/examples/hot_pixels/diff_off_46_map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python/examples/hot_pixels/diff_off_47_count.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python/examples/hot_pixels/diff_off_47_map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python/examples/hot_pixels/diff_off_48_count.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python/examples/hot_pixels/diff_off_48_map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python/examples/hot_pixels/diff_off_49_count.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python/examples/hot_pixels/diff_off_49_map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python/examples/hot_pixels/diff_off_50_count.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python/examples/hot_pixels/diff_off_50_map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python/examples/hot_pixels/diff_off_51_count.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python/examples/hot_pixels/diff_off_51_map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python/examples/hot_pixels/diff_off_52_count.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python/examples/hot_pixels/diff_off_52_map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python/examples/hot_pixels/diff_off_53_count.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python/examples/hot_pixels/diff_off_53_map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python/examples/hot_pixels/diff_off_54_count.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python/examples/hot_pixels/diff_off_54_map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python/examples/hot_pixels/diff_off_55_count.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python/examples/hot_pixels/diff_off_55_map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python/examples/hot_pixels/diff_off_56_count.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python/examples/hot_pixels/diff_off_56_map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python/examples/hot_pixels/diff_off_57_count.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python/examples/hot_pixels/diff_off_57_map.png
Binary file added python/examples/hot_pixels/diff_off_58_count.png
Binary file added python/examples/hot_pixels/diff_off_58_map.png
Binary file added python/examples/hot_pixels/diff_off_59_count.png
Binary file added python/examples/hot_pixels/diff_off_59_map.png
Binary file added python/examples/hot_pixels/diff_off_60_map.png
Binary file added python/examples/pixel_mask.png
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,29 @@ class Configuration:
serde.type.uint64,
serde.type.uint64,
] = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
pixel_mask: tuple[
serde.type.uint64,
serde.type.uint64,
serde.type.uint64,
serde.type.uint64,
serde.type.uint64,
serde.type.uint64,
serde.type.uint64,
serde.type.uint64,
serde.type.uint64,
serde.type.uint64,
serde.type.uint64,
serde.type.uint64,
serde.type.uint64,
serde.type.uint64,
serde.type.uint64,
serde.type.uint64,
serde.type.uint64,
serde.type.uint64,
serde.type.uint64,
serde.type.uint64,
serde.type.uint64,
] = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
mask_intersection_only: bool = False
enable_external_trigger: bool = True
clock: Clock = Clock.INTERNAL
Expand Down
Loading

0 comments on commit 331c997

Please sign in to comment.