Skip to content

Commit

Permalink
integration time fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanpdx committed Dec 15, 2023
1 parent 9cfe7a4 commit f666c21
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 27 deletions.
49 changes: 25 additions & 24 deletions oresat_cfc/drivers/pirt1280.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import random
from enum import IntEnum
from time import sleep
from typing import Union

import numpy as np
from olaf import Adc, Gpio
Expand Down Expand Up @@ -65,7 +66,7 @@ class Pirt1280:
REG_WR = 0x40
"""OR with address to write"""

READ_BACK_WAIT = 0.1
READ_BACK_WAIT = 0.2
"""number of seconds to wait after writing a register before reading it back"""

# refclk
Expand Down Expand Up @@ -94,7 +95,7 @@ def __init__(
self._mock = mock
self._adc = Adc(adc_pin, mock)
self._gpio = Gpio(gpio_num, mock=mock)
self._integration_time = -1.0 # reduce IO calls
self._integration_time = -1 # reduce IO calls

if mock:
self._mock_regs = [0] * (list(Pirt1280Register)[-1].value + 1)
Expand All @@ -105,12 +106,12 @@ def __init__(

self._enabled = False

def __del__(self):
self.disable()

def enable(self):
"""Enable the PIRT1280 (power it on)."""

if self._enabled:
return

# set the enable GPIO high
if not self._mock:
self._gpio.high()
Expand All @@ -127,7 +128,7 @@ def enable(self):
read_value = self._read_8b_reg(Pirt1280Register.CONF1.value)
self._write_8b_reg(Pirt1280Register.CONF1.value, read_value & 0x3F)

self.integration_time = 1.0
self.integration_time = 1

def disable(self):
"""Disable the PIRT1280 (power it off)."""
Expand All @@ -136,7 +137,7 @@ def disable(self):
self._gpio.low()

self._enabled = False
self._integration_time = -1.0
self._integration_time = -1

@property
def is_enabled(self) -> bool:
Expand Down Expand Up @@ -172,11 +173,15 @@ def _write_8b_reg(self, reg: int, value: int):
f"0x{value:X} vs 0x{value_read:X}"
)

def _write_16b_reg(self, reg: int, value: int):
def _write_16b_reg(self, reg: int, value: Union[int, list]):
"""Write a 16-bit int to a pair of registers."""

# convert the value to little-endian bytes
b = value.to_bytes(2, "little")
if isinstance(value, list):
b = value
value = int.from_bytes(bytes(value), "little")
else:
b = list(value.to_bytes(2, "little"))
reg0 = reg
reg1 = reg + 1

Expand All @@ -199,7 +204,7 @@ def _write_16b_reg(self, reg: int, value: int):
if value != value_read:
raise Pirt1280Error(
f"readback to regs {Pirt1280Register(reg).name} did not match "
f"0x{value:X} vs 0x{value_read:X}"
f"0x{value:04X} vs 0x{value_read:04X}"
)

def capture(self) -> bytes:
Expand Down Expand Up @@ -231,15 +236,15 @@ def capture(self) -> bytes:
return bytes(imgbuf)

@property
def integration_time(self) -> float:
def integration_time(self) -> int:
"""float: The integration time in milliseconds."""

if self.is_enabled:
return self._integration_time
return 0.0
return 0

@integration_time.setter
def integration_time(self, value: float):
def integration_time(self, value: int):
if value == self._integration_time:
return # nothing todo

Expand All @@ -265,17 +270,13 @@ def integration_time(self, value: float):
# irb == integration_time reflcks bytes
irb = intr_refclks.to_bytes(4, "little")

# write the frame time register
self._write_8b_reg(Pirt1280Register.FT0.value, frb[0])
self._write_8b_reg(Pirt1280Register.FT1.value, frb[1])
self._write_8b_reg(Pirt1280Register.FT2.value, frb[2])
self._write_8b_reg(Pirt1280Register.FT3.value, frb[3])

# write the integration_time time register
self._write_8b_reg(Pirt1280Register.IT0.value, irb[0])
self._write_8b_reg(Pirt1280Register.IT1.value, irb[1])
self._write_8b_reg(Pirt1280Register.IT2.value, irb[2])
self._write_8b_reg(Pirt1280Register.IT3.value, irb[3])
# write the frame time registers
self._write_16b_reg(Pirt1280Register.FT0.value, [frb[0], frb[1]])
self._write_16b_reg(Pirt1280Register.FT2.value, [frb[2], frb[3]])

# write the integration_time time registers
self._write_16b_reg(Pirt1280Register.IT0.value, [irb[0], irb[1]])
self._write_16b_reg(Pirt1280Register.IT2.value, [irb[2], irb[3]])

self._integration_time = value

Expand Down
4 changes: 3 additions & 1 deletion oresat_cfc/services/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,9 @@ def _on_read_cam_temp(self) -> int:
def _on_read_cam_integration(self) -> int:
"""SDO read callback for camera integration time."""

return int(self._pirt1280.integration_time)
print(self._pirt1280.integration_time)

return self._pirt1280.integration_time

def _on_write_cam_integration(self, value: int):
"""SDO write callback for camera integration time."""
Expand Down
4 changes: 2 additions & 2 deletions oresat_cfc/templates/cfc.html
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ <h4>TEC (Thermoelectric Cooler) Controller</h4>
<br>
<br>
<label for="newIntTime">Integration Time (ms):</label>
<input id="newIntTime" type="number" step="0.001">
<input id="newIntTime" type="number">
<br>
<br>
<b>TEC</b>
Expand Down Expand Up @@ -192,7 +192,7 @@ <h4>TEC (Thermoelectric Cooler) Controller</h4>
writeValue("camera", "number_to_capture", parseInt(document.getElementById("newCapAmount").value));
writeValue("camera", "save_captures", document.getElementById("newCapSave").checked);
if (parseFloat(document.getElementById("newIntTime").value) > 0) {
writeValue("camera", "integration_time", parseFloat(document.getElementById("newIntTime").value));
writeValue("camera", "integration_time", parseInt(document.getElementById("newIntTime").value));
}
writeValue("tec", "status", document.getElementById("newTecStatus").checked);
writeValue("tec", "pid_setpoint", document.getElementById("newSetpoint").value);
Expand Down

0 comments on commit f666c21

Please sign in to comment.