def voltage_ramp_down(goalVoltage): #get initial values voltageReading = mcp3428.take_single_reading(0) bitVoltage = voltageReading currentReading = mcp3428.take_single_reading(1) bitCurrent = currentReading prevTime = time.time() while 1: livetime = time.time() if livetime - prevTime < 1: continue currentReading = mcp3428.take_single_reading(1) current = currentReading * currentConversion voltageReading = mcp3428.take_single_reading(0) voltage = voltageReading * voltageConversion prevTime = livetime print('-----------------------------') print('voltage: ' + str(voltage)) print('-----------------------------') print('max current: ' + str(current)) print('-----------------------------') if voltage > goalVoltage and voltage >= 60: bitVoltage -= 50 print('bit: ' + str(bitVoltage)) print('-----------------------------') dac97.set_voltage(bitVoltage) if voltage < 60: dac97.set_voltage(0)
def voltage_ramp(goalVoltage): bitVoltage = 0 bitCurrent = 4095 # set the voltage to zero. Since the DAC recieves a voltage in bits, # and is twelve bits (12 bits), the range # we can set these to are between 0 and 4095 for voltages that range from 0 V to 10 V. print('------------') print('Voltage set to 0 V...') print('------------') print('Max Current Set to 3.3 mA') print('------------') dac97.set_voltage(bitVoltage) dac96.set_voltage(bitCurrent) # start timer: we need timers to regulate the rate at which the high voltage is incremented prevTime = time.time() #begin while loop of increments and decrements while 1: # read the voltage at the beginning of each iteration. # these readings are from 0 to 10 V, so convert wisel #get the livetime and what until 1 second passes livetime = time.time() if livetime - prevTime < 1: continue currentReading = mcp3428.take_single_reading(1) current = currentReading * currentConversion voltageReading = mcp3428.take_single_reading(0) voltage = voltageReading * voltageConversion #resets the prevTime variable for the next increment prevTime = livetime print('-----------------------------') print('voltage (0 to 3000 V): ' + str(voltage)) print('-----------------------------') print('max current (0 to 10 V): ' + str(current)) print('-----------------------------') if voltage < goalVoltage: bitVoltage += 50 print('bit: ' + str(bitVoltage)) print('-----------------------------') dac97.set_voltage(bitVoltage) else: hold_value(goalVoltage, bitVoltage)
def hold_value(goalVoltage, bitVoltage): print('Bringing to ' + str(goalVoltage) + 'Volts.....') while 1: voltageReading = mcp3428.take_single_reading(0) voltage = voltageReading * voltageConversion if voltage < (goalVoltage - 1): bitVoltage += 1 dac97.set_voltage(bitVoltage) elif voltage > (goalVoltage + 1): bitVoltage -= 1 dac97.set_voltage(bitVoltage) print('voltage: ' + str(mcp3428.take_single_reading(0) * voltageConversion)) else: continue
print('\r\n') print ('Press ctrl-C to exit...') while loopBool == 0: bitVoltage = int(input('Enter Voltage Bit: ')) bitCurrent = int(input('Enter Max Current bit: ')) dac97.set_voltage(bitVoltage) dac96.set_voltage(bitCurrent) time.sleep(.1) print ('Setting Voltage to : ' + str(bitVoltage)) print ('\r\n') print ('------------') print('\r\n') print ('Voltage: ' + str(mcp3428.take_single_reading(0)*conversionVoltage)) print('\r\n') print('------------') print('\r\n') print('Setting Max Current to : ' + str(bitCurrent)) print('\r\n') print('------------') print('\r\n') print('Current: ' + str(mcp3428.take_single_reading(1)*conversionCurrent)) while loopBool == 1: voltages = np.zeros(4096) currents = np.zeros(4096) saveVoltage = 0 saveCurrent = 0 for i in range(4096):
import mcp3428 import Adafruit_BBIO.GPIO as GPIO import os GPIO.setup("P8_12", GPIO.IN) #GPIO.add_event_detect("P8_12", GPIO.RISING) # Get I2C bus, this is I2C Bus 2 bus = smbus.SMBus(2) #kwargs is a Python set that contains the address of your device as well as additional device and calibration values. #kwargs does not have to be populated as every value is optional and will be replaced with a default value if not is specified. #below is an example of a kwarg declaration that is populated with all of the default values for each user configurable property #refer to the datasheet for this chip to calculate what values you should be using for your project. kwargs = {'address': 0x6E, 'mode': 0x10, 'sample_rate': 0x08, 'gain': 0x01} #create the MCP3428 object from the MCP3428 library #the object requires that you pass it the bus object so that it can communicate and share the bus with other chips if necessary mcp3428 = mcp3428.MCP3428(bus, kwargs) while True : if GPIO.input("P8_12") == True: #if GPIO.event_detected("P8_12") == True: time.sleep(0.1) if GPIO.input("P8_12") == True: print "Crusher ON" time.sleep(5) flow = mcp3428.take_single_reading(2) print flow else: print "Crusher OFF"
import time import smbus import mcp3428 import csv import Adafruit_MCP4725 import matplotlib.pyplot as plt import numpy as np bus = smbus.SMBus(1) kwargs = {'address': 0x68, 'mode': 0x10, 'sample_rate': 0x08, 'gain': 0x00} mcp3428 = mcp3428.MCP3428(bus, kwargs) # Create a DAC instance...whatever that means dac = Adafruit_MCP4725.MCP4725(address=0x60, busnum=1) print('Press Ctrl-C to quit...') x = np.array([]) y = np.array([]) for i in range(0, 4095): dac.set_voltage(i) time.sleep(.01) reading = mcp3428.take_single_reading(0) np.append(x, i) np.append(y, reading) m, b = np.polyfit(x, y, 1) plt.plot(x, y, '.b') plt.title('Conversion Factor') plt.show()
def voltageRampCheck(): currentReading = mcp3428.take_single_reading(1) current = currentReading * currentConversion voltageReading = mcp3428.take_single_reading(0) voltage = voltageReading * voltageConversion return voltage, current
def hold_value(goalVoltage, bitVoltage): print('Bringing to ' + str(goalVoltage) + 'Volts.....') counts = 0 while 1: voltageReading = mcp3428.take_single_reading(0) voltage = voltageReading * voltageConversion if counts >= 20: break counts = counts + 1 if voltage < (goalVoltage - 1): bitVoltage += 1 dac97.set_voltage(bitVoltage) elif voltage > (goalVoltage + 1): bitVoltage -= 1 dac97.set_voltage(bitVoltage) print('voltage: ' + str(mcp3428.take_single_reading(0) * voltageConversion)) #reset the parameters of the ramp below, then run the function voltage_ramp.exe if __name__ == '__main__': print('Voltage Ramp') print('\r\n') goalVoltage = int(input('Enter high voltage amount: ')) voltRead = mcp3428.take_single_reading(0) if voltRead < goalVoltage: voltage_ramp_up(goalVoltage) else: voltage_ramp_down(goalVoltage)
import time import smbus import mcp3428 # Get I2C bus, this is I2C Bus 1 bus = smbus.SMBus(1) #kwargs is a Python set that contains the address of your device as well as additional device and calibration values. #kwargs does not have to be populated as every value is optional and will be replaced with a default value if not is specified. #below is an example of a kwarg declaration that is populated with all of the default values for each user configurable property #refer to the datasheet for this chip to calculate what values you should be using for your project. kwargs = {'address': 0x68, 'mode': 0x10, 'sample_rate': 0x08, 'gain': 0x00} #create the MCP3428 object from the MCP3428 library #the object requires that you pass it the bus object so that it can communicate and share the bus with other chips if necessary mcp3428 = mcp3428.MCP3428(bus, kwargs) while True: # Get the readings on all channel in an array all_readings = mcp3428.take_readings() print 'All Readings:' print all_readings print '---' for reading in all_readings: print reading # Get the reading on just on channel, channel 0 is the first channel on the device. print 'Channel 0 reading: ' print mcp3428.take_single_reading(0) print '\r\n' time.sleep(.25)
voltage += 1 bit = voltage / conversion_factor time.sleep(0.01) dac.set_voltage(bit) print('increasing voltage to' + voltage) print('\r\n') print('------------') print('\r\n') print('Voltage: ' + mcp3428.take_single_recording(0)) print('\r\n') print('------------') print('\r\n') previous_reading = mcp3428.take_single_recording(0) start = time.time() #start time of loop time.sleep(0.1) current_reading = mcp3428.take_single_reading(0) end = time.time() #end time rate = (current_reading - previous_reading) / (end - start) if rate > 1: #if the rate exceeds 1 volt per second, as in it jumps past the increase in voltage, it will be done. voltage -= 1 bit = voltage / conversion_factor dac.set_voltage(bit) print('Decreasing Voltage by 1') print('\r\n') print('------------') print('\r\n') print('Voltage: ' + mcp3428.take_single_reading(0)) print('\r\n') print('------------') print('\r\n')