Advertisement
Guest User

Untitled

a guest
Jun 30th, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.         t = 0
  87.         last_t = DateTime.Now blah blah blah
  88.  
  89.         while(true) {
  90.             delta_t = last_t - DateTime.Now.Subtract(new DateTime(1970,1,1,0,0,0));
  91.  
  92.             t += delta_t
  93.  
  94.             if t < 2
  95.                 setP = t
  96.             else
  97.                 setP = 2
  98.            
  99.             //get pressue transducer voltage & calibrate
  100.             responsePress = device.SendMessage("?AI{0}:VALUE");
  101.             pressVolt = responsePress.ToValue(); //convert to a decimal number
  102.             pressVoltF += (pressVolt-pressVoltF)*gain;  //use a low pass filter on pressure voltage
  103.             press = (12.5*pressVolt)-6.25; //calibrate according to P=12.5V-6.25
  104.             pressF = (12.5*pressVoltF)-6.25; //pressure based on filtered pressure voltage
  105.  
  106.             //get temperature voltage & calibrate
  107.             tempVolt = 0;
  108.             temp = 0;
  109.  
  110.             //PID
  111.             error = setP - pressF;
  112.  
  113.             //proporitonal section (distance between curve and 0)
  114.             pCorr = kP * error;
  115.  
  116.             //integral section (accumulation of error --- area under curve)
  117.             cError += error;
  118.             iCorr = kI * cError;
  119.  
  120.             //derivative section (rate of change of error --- slope of curve)
  121.             slope = error - lError;
  122.             dCorr = kD * slope;
  123.             lError = error;
  124.  
  125.             correction = 1000*(pCorr + iCorr + dCorr);
  126.             intCorrection = Convert.ToInt32(Math.Round(correction));
  127.  
  128.             //decision making for correction
  129.             if(intCorrection >= period) {
  130.                 intCorrection = period;
  131.                 Console.SetCursorPosition(0, 25);
  132.                 Console.Write("Limiting max correction to {0}ms\n", period);
  133.             } else if(intCorrection <= (-1*period)) {
  134.                 intCorrection = -1*period;
  135.                                 Console.SetCursorPosition(0, 25);
  136.                                 Console.Write("Limiting min correction to -{0}ms\n", period);
  137.             }
  138.  
  139.             //display temperature and voltage
  140.             Console.SetCursorPosition(0, 8);
  141.             Console.Write("-----------------------\n");
  142.             Console.Write("Sample #: {0}\n", i++);
  143.             Console.Write("Pressure (V): {0:0.00}\n", pressVolt);
  144.             Console.Write("Pressure Filtered (V): {0:0.00}\n", pressVoltF);
  145.             Console.Write("Pressure (psi): {0:0.00}\n", press);
  146.             Console.Write("Pressure Filtered (psi): {0:0.00}\n", pressF);
  147.             Console.Write("Temperature (V): {0:0.0}\n", tempVolt);
  148.             Console.Write("Temperature (\u00b0C): {0:0.0}\n", temp);
  149.             Console.Write("Time (s): {0}\n", currTime.TotalSeconds);
  150.             Console.Write("Solenoid Duration Period (ms): {0}\n", intCorrection);
  151.             Console.Write("Pressure Setpoint (psi): {0}\n", setP);
  152.                         Console.Write("-----------------------\n");
  153.             Console.Write("\n\nPress any key to exit.....");
  154.             //log the data
  155.             file.Write(currTime.TotalSeconds+"\t"+press+"\t"+pressF+"\n");
  156.  
  157.             //command the corrected value
  158.             //if(Math.Abs(error) > 0.5) { //sets deadband and chooses solenoid for correction
  159.                 if(Math.Abs(intCorrection) >= 50) { //ensures that no modulation happens below 50ms range
  160.                     if(correction >= 0) {
  161.                         //solenoid 1;
  162.                         Console.SetCursorPosition(0, 26);
  163.                         Console.Write("Letting pressure in (Solenoid 1 active)\n");
  164.                         device.SendMessage("DIO{1/1}:VALUE=1"); //turn on
  165.                         Thread.Sleep(Math.Abs(intCorrection)); //wait the correction amount
  166.                         device.SendMessage("DIO{1/1}:VALUE=0"); //turn off
  167.                         flag = true;
  168.                     } else if (correction <0) {
  169.                         //solenoid 2;
  170.                         Console.SetCursorPosition(0, 26);
  171.                         Console.Write("Letting pressure out (Solenoid 2 active)\n");
  172.                         device.SendMessage("DIO{1/0}:VALUE=1"); //turn on
  173.                         Thread.Sleep(Math.Abs(intCorrection)); //wait the correction amount
  174.                         device.SendMessage("DIO{1/0}:VALUE=0"); //turn off
  175.                         flag = true;
  176.                     }
  177.                 }
  178.             //}
  179.  
  180.             //ensure the base period remains the same
  181.             if(flag == true) {
  182.                 Thread.Sleep(period-Math.Abs(intCorrection));
  183.                 flag = false;
  184.             } else {
  185.                 Thread.Sleep(period);
  186.             }
  187.         }
  188.     }
  189.     }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement