Exemplo n.º 1
0
class caliMagCtrl():
    def __init__(self, buf=array('H', [100]), ch=1):
        self.timer = Timer(6)
        self.dac = DAC(ch, bits=12)
        self.buf = buf
        self.dac_on = 0

    def reset_buf(self, buf):
        self.buf = buf

    def start(self, freq=10):
        self.dac.write_timed(self.buf, self.timer, mode=DAC.CIRCULAR)
        self.timer.init(freq=freq * len(self.buf))
        self.dac_on = 1

    def stop(self):
        self.timer.deinit()
        self.dac.write(0)
        self.dac_on = 0

    def toggle(self, freq=5):
        if self.dac_on:
            self.stop()
        else:
            self.start(freq)
Exemplo n.º 2
0
class caliMagCtrl1():
    def __init__(self, amps=[], freqs=[], bufs=[], ch=1):
        # amps: 幅值序列
        # bufs: 对应幅值的正弦序列

        self.timer = Timer(6)
        self.dac = DAC(ch, bits=12)
        self.amps = amps
        self.bufs = bufs

        self.freqs = freqs
        self.indx = range(len(amps))
        self.newtask = False

    def next(self):
        ind = self.indx.pop(0)
        self.indx.append(ind)

        self.amp = self.amps[ind]
        buf = self.bufs[ind]
        self.freq = self.freqs[ind]

        if self.amp == 0:
            self.timer.deinit()
            self.dac.write(0)
        else:
            self.dac.write_timed(buf, self.timer, mode=DAC.CIRCULAR)
            self.timer.init(freq=self.freq * len(buf))

        self.newtask = True
Exemplo n.º 3
0
def tone1(freq):
    t0 = micros()
    dac = DAC(1)
    while True:
        theta = 2*math.pi*float(elapsed_micros(t0))*freq/1e6
        fv = math.sin(theta)
        v = int(126.0 * fv) + 127
        #print("Theta %f, sin %f, scaled %d" % (theta, fv, v))
        #delay(100)
        dac.write(v)
Exemplo n.º 4
0
class OutputToDue(object):
    def __init__(self, op_pin):
        self.pin = op_pin
        self.dac_output = DAC(op_pin)

    # setting transportting:
    # range of number: 0 - 255 !!!
    def write_message(self, number):
        if number > 150:
            number = 150
        elif number < 3:
            number = 0
        self.dac_output.write(int(number))
Exemplo n.º 5
0
class caliMagCtrl2():
    def __init__(self, freqs=[], bufs=[], ch=1, **kwargs):
        # freqs:待测试的频率
        # bufs: 对应幅值的正弦序列等消息,每个元素包含(amp,buf,port)
        # 其中port是指,使用可变电阻时对应控制的端口
        # ch:测试信号输出端口选择
        # kwargs: r_port,电阻选择的端口列表

        self.timer = Timer(6)
        self.dac = DAC(ch, bits=12)
        self.freqs = freqs
        self.bufs = bufs
        self.r_adapt = False
        if len(kwargs) > 0:
            if 'r_port' in kwargs:  #使用电阻自适应调整
                self.r_adapt = True
                (pyb.Pin(pin, pyb.Pin.OUT_PP) for pin in kwargs['r_port'])

        self.freqs = freqs
        self.indx = range(len(amps))
        self.newtask = False

    def next(self):
        ind = self.indx.pop(0)
        self.indx.append(ind)

        self.amp = self.amps[ind]
        buf = self.bufs[ind]
        self.freq = self.freqs[ind]

        if self.amp == 0:
            self.timer.deinit()
            self.dac.write(0)
        else:
            self.dac.write_timed(buf, self.timer, mode=DAC.CIRCULAR)
            self.timer.init(freq=self.freq * len(buf))

        self.newtask = True
Exemplo n.º 6
0
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()

# Only blobs that with more pixels than "pixel_threshold" and more area than "area_threshold" are
# returned by "find_blobs" below. Change "pixels_threshold" and "area_threshold" if you change the
# camera resolution. "merge=True" must be set to merge overlapping color blobs for color codes.

while(True):
    clock.tick()
    img = sensor.snapshot()
    for blob in img.find_blobs([thresholds[1]], pixels_threshold=100, area_threshold=100, merge=True):
            img.draw_rectangle(blob.rect())
            img.draw_cross(blob.cx(), blob.cy())
            img.draw_string(blob.x() + 2, blob.y() + 2, "verde")
            dac = DAC('P6')#Select pin whit DAC or ADC
            dac.write(255)  # output between 0 and 255
    for blob in img.find_blobs([thresholds[0]], pixels_threshold=100, area_threshold=100, merge=True):
            img.draw_rectangle(blob.rect())
            img.draw_cross(blob.cx(), blob.cy())
            img.draw_string(blob.x() + 2, blob.y() + 2, "rojo")
            dac = DAC('P6')#Select pin whit DAC or ADC
            dac.write(100)  # output between 0 and 255
    for blob in img.find_blobs([thresholds[2]], pixels_threshold=100, area_threshold=100, merge=True):
            img.draw_rectangle(blob.rect())
            img.draw_cross(blob.cx(), blob.cy())
            img.draw_string(blob.x() + 2, blob.y() + 2, "amarillo")
            dac = DAC('P6')#Select pin whit DAC or ADC
            dac.write(50)  # output between 0 and 255
    print(clock.fps())
Exemplo n.º 7
0
# main.py -- put your code here!
import pyb
from pyb import DAC
#----------DAC--------------#
dac = DAC(1, bits=12)  #X15 pin
dac.write(4095)

dac2 = DAC(2, bits=12)  #X16 pin
dac2.write(2048)
Exemplo n.º 8
0
from pyb import DAC
from array import array
import math

dac = DAC(1, bits=12, buffering=True)   # use 12 bit resolution
dac.write(4095)         # output maximum value, 3.3V


# To output a continuous sine-wave at 12-bit resolution:
# create a buffer containing a sine-wave, using half-word samples
buf = array("H", 2048 + int(2047 * math.sin(2 * math.pi * i / 128)) for i in range(128))

# output the sine-wave at 200Hz
dac.write_timed(buf, 200 * len(buf), mode=DAC.CIRCULAR)
import pyb
from pyb import DAC


def volume(val):
    pyb.I2C(1, pyb.I2C.MASTER).mem_write(val, 46, 0)


volume(127)
dac = DAC(1)
t = 0
while True:
    dac.write(int(t * ((t >> 9 | t >> 13) & 25 & t >> 6)) % 256)
    #dac.write(int(t*((15&t>>11)%12)&55-(t>>5|t>>12)|t*(t>>10)*32) % 256)
    #dac.write(int((t*9&t>>4|t*5&t>>7|t*3&t//1024)-1))
    t += 1
Exemplo n.º 10
0
import math
from array import array
from pyb import DAC

dac = DAC(0, bits=8)
dac.write(0)
dac.write(64)
dac.write(128)
dac.write(172)
dac.write(255)
dac.deinit()

# create a buffer containing a sine-wave, using half-word samples
buf = array(
    'H', 2048 + int(2047 * math.sin(2 * math.pi * i / 128))
    for i in range(128))

#reinit dac to 8bit mode
dac = DAC(0, bits=12)

# output the sine-wave at 400Hz
dac.write_timed(buf, 400 * len(buf), mode=DAC.CIRCULAR)
Exemplo n.º 11
0
yaw=""
rightflag=2
flagspeed=2
leftflag=2
stopflag=2
startflag=2
dacR=0
dacL=0

# ===================================================== setup =========================================#
uart = UART(4, 9600)							#UART for ESP communication
uart.init(9600, bits=8, parity=None, stop=1,read_buf_len=64)
#uart1 = UART(3, 9600)							#UART for ultrasonic module
#uart1.init(9600, bits=8, parity=None, stop=1,read_buf_len=64)
dacA=DAC(1,bits=12)							#DAC Initialization
dacA.write(init_dacA)
dacB=DAC(2,bits=12)
dacB.write(init_dacB)
motor1_switch = Pin('Y6', Pin.OUT_PP)					#Relay for DAC; connected in a normally open connection
motor2_switch = Pin('Y7', Pin.OUT_PP) 
motor1_switch.high()
motor2_switch.high()

#last measured time (for calculating frequency) (for left wheel)
l_m_time_L1 = 0
l_m_time_L2 = utime.ticks_us() 

#last measured time (for calculating frequency) (for right wheel)
l_m_time_R1 = 0
l_m_time_R2 = utime.ticks_us()
Exemplo n.º 12
0
                right()
                stop()
                start()
                state = 'left'
        x0 = x
        y0 = y
    stop()
    print("map has been traced")


#############################################################################################################
#														SETUP												#
#############################################################################################################
uart = UART(4, 9600)  #UART for ESP communication
uart.init(9600, bits=8, parity=None, stop=1, read_buf_len=64)
dacA.write(init_dacA)
dacB.write(init_dacB)
motor_relay_L.high()
motor_relay_R.high()
motor1_switch.high()
motor2_switch.high()
motor_L_brake.high()
motor_R_brake.high()

setSpeedR(0)
setSpeedL(0)
Switch().callback(lambda: forward(2000))
tim.callback(speedCorrection)
ExtInt('Y1', ExtInt.IRQ_RISING_FALLING, Pin.PULL_NONE,
       cfreq_R)  #hall sensors as inperrupts
ExtInt('Y2', ExtInt.IRQ_RISING_FALLING, Pin.PULL_NONE, cfreq_L)
Exemplo n.º 13
0
 # PYB_adda_test.pyb 
 
 # PyBoard Test of DAC & ADC
 # (c) Claus Kuehnel 2019-02-17 [email protected]

 # PyBoard v1.1 has two DAC channels, connected to X5 and X6
 # There are 16 ADC channels. The ADC channel connected to X19 is used here.
 # Connect X5 & X19 for this test.

import time
from pyb import Pin,DAC, ADC
dac = DAC(Pin('X5'), bits=12)
adc = ADC(Pin('X19'))

print('\nTesting ADDA subsystem of PyBoard\n')

print('DAC\t ADC\t Diff')
print('-----------------------')

for i in range(256):
  j = i * 16
  dac.write(j)
  time.sleep_ms(100)
  adcvalue = adc.read()
  time.sleep_ms(50)
  diff = adcvalue - j
  print('{0:4d}\t{1:4d}\t{2:4d}'.format(j, adcvalue, diff))
   


Exemplo n.º 14
0
#import numpy as np
#import commpy.channelcoding.convcode as cc
#from commpy.utilities import dec2bitarray
import binascii
from pyb import DAC
import utime

#***********************************************
# DAC parameters configuration
#***********************************************
dac = DAC(1, bits=8)

#***********************************************
#Conversion from text message into single decimal byte#
#***********************************************
msg = 'Hello World'
#Ascci converted to hex and then to dec
for s in range(len(msg)):
    msg_dec = int(binascii.hexlify(msg[s]), 16)
    dac.write(msg_dec)
    #pyb.delay(50)

print(msg)
print(msg_dec)
Exemplo n.º 15
0
oled.draw_text(0, 0, 'Measure pitch and pitch_dot')
oled.draw_text(0,20, 'CCW: pitch (deg)')
oled.draw_text(0,30, 'CW: pitch_dot (deg/s)')
oled.display()

# IMU connected to X9 and X10
imu = MPU6050(1, False)

# Pitch angle calculation using complementary filter
def pitch_estimate(pitch, dt, alpha):
	theta = imu.pitch()
	pitch_dot = imu.get_gy()
	pitch = alpha*(pitch + pitch_dot*dt) + (1-alpha)*theta
	return (pitch, pitch_dot)
'''
Main program loop
'''
pitch = 0	# initialise pitch angle to 0 to start
alpha = 0.9	# alpha value in complementary filter
tic = pyb.millis()
while True:
	b_LED.toggle()
	dt = pyb.millis() - tic
	if dt > 20:		# sampling time is 20 msec or 50Hz
		[pitch, pitch_dot] = pitch_estimate(pitch, dt*0.001, alpha)
		tic = pyb.millis()
		if pot.read() > 2048:	# decide which to send to X5 (BNC)
			a_out.write(min(4095,int(pitch*40)+2048))
		else:
			a_out.write(min(4095,int(pitch_dot*10)+2048))
Exemplo n.º 16
0
from pyb import DAC
from binascii import hexlify, unhexlify
import utime
from struct import unpack

#create a text message
data = str('H')
for s in range(len(data)):
    h = hexlify(data[s])
    #hdec = int((h), 16)
    hdec = int.from_bytes(h, 'little', True)
print(h)
print(hdec)

#Parameters Fs=1MSPS
SPB = 10

#Parameters for DAC
dac = DAC(1, bits=12)  #12 bits resolution
dac.write(hdec)

#pyb.delay(100)
#print(hdec)
#print(type(hdec))
Exemplo n.º 17
0
# DAC Control Example
#
# This example shows how to use the DAC pin output onboard your OpenMV Cam.

import time
from pyb import DAC

dac = DAC("P6") # Must always be "P6".

while(True):
    # The DAC has 8-12 bits of resolution (default 8-bits).
    for i in range(256):
        dac.write(i)
        time.sleep(20)
    for i in range(256):
        dac.write(255-i)
        time.sleep(20)
Exemplo n.º 18
0
    else:
        return (sensor.read()/2552.3)**(1/-1.045)


# decrease volume as object gets closer
def v_change_dis():
    return 255-int((sensor.read()) >> 4)
    # you can get rid of "255-" to make it do the opposite
# volume control

tim = Timer(1)
tim.init(freq=20)
dac = DAC(1)
pot = ADC('B0')
# tim.callback(lambda t: dac.write(int(pot.read()>>4)))
tim.callback(lambda t: dac.write(v_change_dis()))
# GPIO test

gpio = pyb.Pin('A13', pyb.Pin.OUT_PP)
gpio.high()
pyb.delay(10)
gpio.low()
pyb.delay(10)
gpio.high()
pyb.delay(10)
'''
class stat:
    def __init__(self):
        self.s = 0
    def num(self):
        self.s = 14
class printheadController():
    def __init__(self):
        self.p_SICL = Pin(config.SICL, Pin.OUT_PP)
        self.p_SIBL = Pin(config.SIBL, Pin.OUT_PP)
        self.p_CK = Pin(config.CK, Pin.OUT_PP)
        self.p_LAT = Pin(config.LAT, Pin.OUT_PP)
        self.p_CH = Pin(config.CH, Pin.OUT_PP)
        self.p_NCHG = Pin(config.NCHG, Pin.OUT_PP)

        self.p_LAT.value(0)
        self.p_NCHG.value(0)

        self.dac = DAC(1)
        self.dac.write(127)

        waveform = config.WAVEFORM
        factor = max(waveform)
        for i in range(len(waveform)):
            waveform[i] = int(255*(waveform[i]/factor))
        self.waveform = array('i', waveform)

    def _latch(self):
        self.p_NCHG.value(1)
        self.p_LAT.value(1)
        self.p_LAT.value(0)
        self.p_NCHG.value(1)


    def _fire_nozzles(self):
        self.p_NCHG.value(1)
        run_dac(len(self.waveform), addressof(self.waveform))

    def fire(self, B='0', C='0', S='M', Q='E'):
        droplet_size = self._get_size(S)
        droplet_quality = self._get_quality(Q)
        signal = SequenceFactory().get_sequence2(nozzles_black=self._bin_to_range(B),
                                                nozzles_cyan=self._bin_to_range(C),
                                                nozzles_yellow=self._bin_to_range(C),
                                                nozzles_magenta=self._bin_to_range(C),
                                                size=droplet_size,
                                                quality=droplet_quality)
        self._fire(signal)

    def fire_all(self, S='M', Q='E'):
        droplet_size = self._get_size(S)
        droplet_quality = self._get_quality(Q)
        signal = SequenceFactory().get_sequence2(  nozzles_black=range(1, 91),
                                                nozzles_cyan=range(1, 31),
                                                nozzles_yellow=range(1, 31),
                                                nozzles_magenta=range(1, 31),
                                                size=droplet_size,
                                                quality=droplet_quality)
        self._fire(signal)

    def _get_size(self, S):
        if S=='S': return 'small'
        if S=='M': return 'medium'
        if S=='L': return 'large'
        return 'medium'

    def _get_quality(self, Q):
        if Q=='E': return 'economy'
        if Q=='J': return 'jeff'
        if Q=='1': return 'VSD1'
        if Q=='2': return 'VSD2'
        if Q=='3': return 'VSD3'
        if Q=='A': return 'all'
        return 'all'

    def _bin_to_range(self, bin):
        counter = 1
        lst = []
        bin = list(bin)

        for i in bin:
            if i == '1': lst.append(counter)
            counter += 1
        return lst

    def _fire(self, signal):
        for i in range(100):
            self._wake_chip()
            self._latch()
            dma_functions.output_signal(signal)
            self._all_signals_low()
            self._fire_nozzles()
            time.sleep(0.001)

    def _all_signals_low(self):
        self.p_SIBL.value(0)
        self.p_SICL.value(0)
        self.p_CK.value(0)
        self.p_CH.value(0)

    def _wake_chip(self):
        self.p_NCHG.value(1)
        time.sleep(0.0001)
        self.p_NCHG.value(0)
        self.p_LAT.value(1)
        self.p_LAT.value(0)
        for i in range(51):
            self.p_NCHG.value(1)
            time.sleep_us(105)
            self.p_NCHG.value(0)
            time.sleep_us(105)
        self.p_NCHG.value(1)
Exemplo n.º 20
0
# DAC Control Example
#
# This example shows how to use the DAC pin output onboard your OpenMV Cam.

import time
from pyb import DAC

dac = DAC("P6") # Must always be "P6".

while(True):
    # The DAC has 8-12 bits of resolution (default 8-bits).
    for i in range(256):
        dac.write(i)
        time.sleep_ms(20)
    for i in range(256):
        dac.write(255-i)
        time.sleep_ms(20)
        red_led.on()
        utime.sleep_ms(200)
        lpf2.initialize()
    else:
        red_led.off()
        button_list = [p_inb2.value(), p_ing1.value()]
        str1 = ''.join(str(e) for e in button_list)
        send_data = int(str1, 2)
        print(send_data)
        try:
            lpf2.load_payload(send_data)
            print('a')
        except:
            print('sending data to spike error')
            pass
        if p_inw1.value() == 1:
            print('value=1')
            for i in range(100, 255):
                led1.write(i)
                led2.write(255 - i)
                utime.sleep_ms(10)
            for i in range(100, 255):
                led1.write(255 - i)
                led2.write(i)
                utime.sleep_ms(10)
        else:
            print('value=0')
            led1.write(0)
            led2.write(0)
        utime.sleep_ms(105)
Exemplo n.º 22
0
#import numpy as np
#import commpy.channelcoding.convcode as cc
#from commpy.utilities import dec2bitarray
import binascii
from pyb import DAC
import utime

#En esta prueba se crea un arreglo predefinido
#con decimales del 0 al 255 y se envia cada
#elemento del arreglo al DAC(1)
#***********************************************
# DAC parameters configuration
#***********************************************
dac = DAC(1, bits=8)
#***********************************************
#Conversion from text message into single decimal byte#
#***********************************************
msg = bytearray([
    255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255,
    0, 0, 255, 255, 0, 0
])
#msg = bytearray([0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255])

#Ascci converted to hex and then to dec
for s in range(len(msg)):
    #msg_dec = int(binascii.hexlify(msg[s]), 16)
    dac.write(msg[s])

print(msg)
print(len(msg))
import pyb
import random
from pyb import DAC


def volume(val):
    pyb.I2C(1, pyb.I2C.MASTER).mem_write(val, 46, 0)


volume(127)
dac = DAC(1)
while True:
    dac.write(random.randint(0, 256))
from pyb import DAC

dac = DAC(1)            # DAC 1 sur la broche A0 (Feather) ou X5 (Pyboard)
dac.write(128)          # Ecriture d'une valeur sur 8 bit

dac = DAC(1, bits=12)   # DAC 1 en 12 bit
dac.write(4095)         # Ecriture de la valeur maximale
Exemplo n.º 25
0
# PYB_adda_test.pyb

# PyBoard Test of DAC & ADC
# (c) Claus Kuehnel 2019-02-17 [email protected]

# PyBoard v1.1 has two DAC channels, connected to X5 and X6
# There are 16 ADC channels. The ADC channel connected to X19 is used here.
# Connect X5 & X19 for this test.

import time
from pyb import Pin, DAC, ADC
dac = DAC(Pin('X5'))
adc = ADC(Pin('X19'))

print('\nTesting ADDA subsystem of PyBoard\n')

print('DAC\t ADC\t Diff')
print('-----------------------')

for i in range(256):
    dac.write(i)
    time.sleep_ms(100)
    adcvalue = adc.read()
    time.sleep_ms(50)
    diff = 16 * i
    diff = adcvalue - diff
    print('{0:3d}\t{1:4d}\t{2:4d}'.format(i, adcvalue, diff))
Exemplo n.º 26
0
from pyb import DAC
dac = DAC(1)  # create DAC 1 on pin X5
dac.write(128)  # write a value to the DAC (makes X5 1.65V)
dac = DAC(1, bits=12)  # use 12 bit resolution
dac.write(4095)  # output maximum value, 3.3V
Exemplo n.º 27
0
from pyb import DAC
import utime,math

dac = DAC(1,buffering=True)            # create DAC 1 on pin X5
dac.write(128)          # write a value to the DAC (makes X5 1.65V)

# To output a continuous sine-wave:

# create a buffer containing a sine-wave
buf = bytearray(100)
for i in range(len(buf)):
    buf[i] = 128 + int(127 * math.sin(2 * math.pi * i / len(buf)))

# output the sine-wave at 400Hz
dac.write_timed(buf, 400 * len(buf), mode=DAC.CIRCULAR)
Exemplo n.º 28
0
ch = Timer.channel(1, Timer.PWM, pin=p)
ch.pulse_width_percent(50)
'''
1.9 ADC
'''
from pyb import Pin, ADC

adc = ADC(Pin('X19'))
adc.read()  # read value, 0 - 4095
'''
1.10 DAC
'''
from pyb import Pin, DAC

dac = DAC(Pin('X5'))
dac.write(120)  # output between 0 and 255
'''
1.11 UART
'''
from pyb import UART

uart1 = UART(1, 9600)
uart1.write('hello')
uart1.read(5)  # read up to 5 bytes
'''
1.12 SPI Bus
'''
from pyb import SPI

spi1 = SPI(1, SPI.MASTER, baudrate=200000, polarity=1, phase=0)
spi1.send('hello')
Exemplo n.º 29
0
#hardware platform: pyboard V1.1

from pyb import DAC, Pin
import math
import time

dac0 = DAC(Pin('X5'))
dac1 = DAC(Pin('X6'))
a = 0
while True:
    value = math.sin(a * math.pi / 180)  #get sine
    dac0.write(int(100 + value * 100))  #ouput wave
    dac1.write(a * 255 // 360)
    a += 1
    if (a == 361):
        a = 0
    time.sleep(0.0001)