Advertisement
Guest User

Untitled

a guest
Jun 30th, 2015
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading;
  5. using System.Windows.Forms;
  6. using System.Globalization;
  7. using MeasurementComputing.DAQFlex;
  8.  
  9.  
  10. namespace ConsoleApplication1
  11. {
  12.     class Program
  13.     {
  14.     static DaqDevice device;
  15.     static System.IO.StreamWriter file;
  16.     const int period = 150;
  17.  
  18.         static void Main()
  19.         {
  20.         string[] deviceNames = DaqDeviceManager.GetDeviceNames(DeviceNameFormat.NameAndSerno);
  21.         int deviceNumber = 0;
  22.         string deviceName = deviceNames[deviceNumber];
  23.         Thread th = new Thread(BackFunc);
  24.         file = new System.IO.StreamWriter("voltage.txt");
  25.  
  26.         //find the device and clear the console
  27.         device = DaqDeviceManager.CreateDevice(deviceName);
  28.         Console.Clear();
  29.  
  30.         //analog input section (to log pressure)
  31.         Console.SetCursorPosition(0, 0);
  32.                 Console.Write("-----------------------------------------------------------------------------\n");
  33.         device.SendMessage("AI:CHMODE=SE");
  34.         Console.WriteLine("Device Info:");
  35.         Console.WriteLine(device.SendMessage("?AI: VALIDCHANS/CHMODE"));
  36.         device.SendMessage("AI:SCALE=ENABLE");
  37.         device.SendMessage("AI{0}:RANGE=BIP10V");
  38.  
  39.         //analog output section (use this to set temperature in the future)
  40.         device.SendMessage("AO:SCALE=ENABLE");
  41.         device.SendMessage("AO{0}:VALUE=1.21");
  42.  
  43.         //digital io section (to control solenoids)
  44.         device.SendMessage("DIO{0}:DIR=OUT");
  45.         device.SendMessage("DIO{1}:DIR=OUT");
  46.         Console.WriteLine("Ports Available: " + device.SendMessage("?DIO"));
  47.         Console.WriteLine("Bits on Port 0: " + device.SendMessage("?DIO{0}"));
  48.         Console.WriteLine("Bits on Port 1: " + device.SendMessage("?DIO{1}"));
  49.         Console.Write("-----------------------------------------------------------------------------\n");
  50.  
  51.         //start polling the device for data
  52.         th.Start();
  53.         Console.ReadLine();
  54.  
  55.         //clean up and ensure solenoids are off
  56.         th.Abort();
  57.         Console.Clear();
  58.         file.Close();
  59.         device.SendMessage("DIO{1/0}:VALUE=0");
  60.         device.SendMessage("DIO{1/1}:VALUE=0");
  61.         DaqDeviceManager.ReleaseDevice(device);
  62.         }
  63.  
  64.     static void BackFunc() {
  65.         int i = 0;
  66.         const double gain = 0.076;
  67.                 DaqResponse responsePress, responseTemp;
  68.                 double pressVoltF, pressVolt, pressF, press, tempVolt, temp;
  69.         double error = 0, lError = 0, cError = 0, slope = 0;
  70.         double iCorr = 0, pCorr = 0, dCorr = 0, correction = 0;
  71.         int intCorrection = 0;
  72.         TimeSpan currTime;
  73.         bool flag = false;
  74.         //set initial values
  75.         double kP = 6;
  76.         double kI = 0.000;
  77.         double kD = 0;
  78.         double setP = 2;
  79.  
  80.  
  81.         //get a value to start with for the low pass filter
  82.                 responsePress = device.SendMessage("?AI{0}:VALUE");
  83.                 pressVoltF = responsePress.ToValue();
  84.         Thread.Sleep(period);
  85.  
  86.         while(true) {
  87.             currTime = DateTime.Now.Subtract(new DateTime(1970,1,1,0,0,0));
  88.  
  89.             //get pressue transducer voltage & calibrate
  90.             responsePress = device.SendMessage("?AI{0}:VALUE");
  91.             pressVolt = responsePress.ToValue(); //convert to a decimal number
  92.             pressVoltF += (pressVolt-pressVoltF)*gain;  //use a low pass filter on pressure voltage
  93.             press = (12.5*pressVolt)-6.25; //calibrate according to P=12.5V-6.25
  94.             pressF = (12.5*pressVoltF)-6.25; //pressure based on filtered pressure voltage
  95.  
  96.             //get temperature voltage & calibrate
  97.             tempVolt = 0;
  98.             temp = 0;
  99.  
  100.             //PID
  101.             error = setP - pressF;
  102.  
  103.             //proporitonal section (distance between curve and 0)
  104.             pCorr = kP * error;
  105.  
  106.             //integral section (accumulation of error --- area under curve)
  107.             cError += error;
  108.             iCorr = kI * cError;
  109.  
  110.             //derivative section (rate of change of error --- slope of curve)
  111.             slope = error - lError;
  112.             dCorr = kD * slope;
  113.             lError = error;
  114.  
  115.             correction = 1000*(pCorr + iCorr + dCorr);
  116.             intCorrection = Convert.ToInt32(Math.Round(correction));
  117.  
  118.             //decision making for correction
  119.             if(intCorrection >= period) {
  120.                 intCorrection = period;
  121.                 Console.SetCursorPosition(0, 25);
  122.                 Console.Write("Limiting max correction to {0}ms\n", period);
  123.             } else if(intCorrection <= (-1*period)) {
  124.                 intCorrection = -1*period;
  125.                                 Console.SetCursorPosition(0, 25);
  126.                                 Console.Write("Limiting min correction to -{0}ms\n", period);
  127.             }
  128.  
  129.             //display temperature and voltage
  130.             Console.SetCursorPosition(0, 8);
  131.             Console.Write("-----------------------\n");
  132.             Console.Write("Sample #: {0}\n", i++);
  133.             Console.Write("Pressure (V): {0:0.00}\n", pressVolt);
  134.             Console.Write("Pressure Filtered (V): {0:0.00}\n", pressVoltF);
  135.             Console.Write("Pressure (psi): {0:0.00}\n", press);
  136.             Console.Write("Pressure Filtered (psi): {0:0.00}\n", pressF);
  137.             Console.Write("Temperature (V): {0:0.0}\n", tempVolt);
  138.             Console.Write("Temperature (\u00b0C): {0:0.0}\n", temp);
  139.             Console.Write("Time (s): {0}\n", currTime.TotalSeconds);
  140.             Console.Write("Solenoid Duration Period (ms): {0}\n", intCorrection);
  141.             Console.Write("Pressure Setpoint (psi): {0}\n", setP);
  142.                         Console.Write("-----------------------\n");
  143.             Console.Write("\n\nPress any key to exit.....");
  144.             //log the data
  145.             file.Write(currTime.TotalSeconds+"\t"+press+"\t"+pressF+"\n");
  146.  
  147.             //command the corrected value
  148.             //if(Math.Abs(error) > 0.5) { //sets deadband and chooses solenoid for correction
  149.                 if(Math.Abs(intCorrection) >= 50) { //ensures that no modulation happens below 50ms range
  150.                     if(correction >= 0) {
  151.                         //solenoid 1;
  152.                         Console.SetCursorPosition(0, 26);
  153.                         Console.Write("Letting pressure in (Solenoid 1 active)\n");
  154.                         device.SendMessage("DIO{1/1}:VALUE=1"); //turn on
  155.                         Thread.Sleep(Math.Abs(intCorrection)); //wait the correction amount
  156.                         device.SendMessage("DIO{1/1}:VALUE=0"); //turn off
  157.                         flag = true;
  158.                     } else if (correction <0) {
  159.                         //solenoid 2;
  160.                         Console.SetCursorPosition(0, 26);
  161.                         Console.Write("Letting pressure out (Solenoid 2 active)\n");
  162.                         device.SendMessage("DIO{1/0}:VALUE=1"); //turn on
  163.                         Thread.Sleep(Math.Abs(intCorrection)); //wait the correction amount
  164.                         device.SendMessage("DIO{1/0}:VALUE=0"); //turn off
  165.                         flag = true;
  166.                     }
  167.                 }
  168.             //}
  169.  
  170.             //ensure the base period remains the same
  171.             if(flag == true) {
  172.                 Thread.Sleep(period-Math.Abs(intCorrection));
  173.                 flag = false;
  174.             } else {
  175.                 Thread.Sleep(period);
  176.             }
  177.         }
  178.     }
  179.     }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement