Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added clocks and powersupply #4

Merged
merged 14 commits into from Jul 6, 2021
92 changes: 92 additions & 0 deletions TCD1304.py
@@ -0,0 +1,92 @@
'''
Requests for the Code:
the code should have an power Output on the Voltagesource VDD=5V /VAD=5V
the code should build a Masterclock with a frequency inbetween 0.8 to 4 MHz, typical 2 MHz. The Voltagerange of the Clock
shall be 0-4V
The ICG is a squarewave with a high level of typical 4V over the time of 3694 Elements of the Clock.
The SH is a squarewave with a high level of typical 4V and indicates the integrationtime.
The integrationtime is started with the falling flank of the signal and and lasts till the next falling flank.
The analog input of the sensor shall be read by the the osilloscope.
the analog voltage signal shall be saved over the time and be integrated to get a digital signal.
the algotithims still needs to be definded.

import argparse
import platform
import os.path
import shutil
import sys
import time
from itertools import zip_longest
from typing import List, Tuple



'''

import numpy as np
import time
import math
from pslab.instrument.waveform_generator import PWMGenerator
from pslab.instrument.oscilloscope import Oscilloscope
from pslab.serial_handler import SerialHandler
from pslab.instrument.logic_analyzer import LogicAnalyzer
from pslab.instrument.power_supply import PowerSupply

frequency_master_clock = 2e6 # no need to specify the variable type (float). frequency_master_clock = float(2e6) turns into frequency_master_clock = 2e6. Same for integration_time variable. Im Datentyp float werden reele Zahlen in Exponentialdarstellung geschrieben (also Gleitkommazahlen). float(2e6) turns 2e6 which is already a float into a float. In other words, it does nothing; However f.e. 280,7 would have also been linked to float instead of int
integration_time = 1.85e-3 # time in seconds
oscillator_frequency = 128e6 #frequency of the oscilliator in Mhz
integration_elements = 3694 #according to timing requests
microseconds_in_second = 1e6
prescaler = int(math.log(oscillator_frequency / frequency_master_clock) / math.log(2)) # When setting a frequency by mapping the reference clock directly to a PWM output, only frequencies which are even factors of 128 MHz (the frequency of the PSLab's main oscillator) are available. The frequency is therefore not set by specifying the frequency itself, but by setting a prescaler.


class TCD1304():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No parentheses needed.

def __init__(
self, #Initializing; self is always first parameter of methods located inside a class and is not limited to the __init__ method but to most methods. A method is a function that belongs to a class
min_masterclock_frequency = 0.8e6, # no need to specify the variable type (float). View comment in L37
max_masterclock_frequency = 4e6, # no need to specify the variable type (float). View comment in L37
frequency = frequency_master_clock, #default frequency of masterclock 2 MHz; # no need to specify the variable type (float). View comment in L37
min_voltagerange_clock: float = 0, #it says that min_voltagerange_clock should (!) be a float. Otherwise it would not turn into a float automatically. Output on float will be 0.0 V and on int 0 V.
max_voltagerange_clock: float = 4,
pwmgen = ... # not sure if pwmgen needs to be assigned to a value ??
):

self.min_masterclock_frequency = min_masterclock_frequency #initializing starts from here (? Not sure though)
self.max_masterclock_frequency = max_masterclock_frequency
self.frequency = frequency
self.min_voltagerange_clock = min_voltagerange_clock
self.max_voltagerange_clock = max_voltagerange_clock
self.pwmgen = PWMGenerator() #once assigned in __init__ self.pwmgen can be used in other methods
self.scope = Oscilloscope ()


def power_source (): # puts a voltage of 4V on PV1
ps = PowerSupply()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use the self.ps object instead of creating a new one here.

ps.pv1 = 4
bessman marked this conversation as resolved.
Show resolved Hide resolved
ps.pv1
freddiscr marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As pointed out before, this line is unnecessary.


def master_clock (self): # puts PWM with frequency of 2MHz on SQ1; masterclock is the fastest clock
self.pwmgen.map_reference_clock(["SQ1"], prescaler)

def sh_clock (self): # on SQ2 sets pulse for the integrationtime. Running the Sensor in shutter mode try with a frequency of 1/4 of master_clock. tint (min) = 10µs
self.pwmgen.generate(["SQ2"], prescaler/4 )
bessman marked this conversation as resolved.
Show resolved Hide resolved


def icg_clock (self): # on SQ3 starts and stops the reading of the sensor; this third clock is the slowest clock
bessman marked this conversation as resolved.
Show resolved Hide resolved
self.pwmgen.set_state()
for pulse in range(integration_elements):
self.pwmgen.set_state(sq3=True)
time.sleep(integration_time / 2)
self.pwmgen.set_state(sq3=False)
time.sleep(integration_time / 2)

'''
normal state of icg is a high level. Start of the reading is on the rising
flank of the voltage. To start the reading voltage shall drop to low and rise
to high again.
'''
def analog_signal_read (self):
self.scope = Oscilloscope()
x, y = scope.capture (1, integration_elements, (integration_time / integration_elements)*microseconds_in_second) # Parameters set are (Ch1, amount of samples(3694), time between samples (~0,5µs))
bessman marked this conversation as resolved.
Show resolved Hide resolved
analog_measurement = np.ndarray ()
return analog_measurement # this should put the Measurements and timestamps in an array