1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
using System.Linq; using UnityEngine; using System.Collections; using System.Collections.Generic; using SimpleJSON; using System; using System.IO.Ports; namespace SerialCtrl { public class SerialTrial : MVRScript { // Name of plugin public static string pluginName = "Serial Controller"; // Serial Variables private string[] portNames; private string portSelected = "None"; private string baudSelected = "9600"; protected JSONStorableFloat pulseFreq; private float pulsetime = 1; private string pulsestring = ""; protected UIDynamicButton startbutton; protected UIDynamicButton stopbutton; protected JSONStorableString jstring; protected UIDynamicTextField dtext; protected JSONStorableFloat ThrustX; // Measurement Variables private Atom refObject; // Male person private FreeControllerV3 refObjA; // Penis base private FreeControllerV3 refObjB; // Penis tip private Vector3 refA; // Penis base position private Vector3 refB; // Penis tip position private Vector3 refLine; // Penis reference line private Atom targObject; // Female person private FreeControllerV3 targObjC; // Target point private Vector3 targC; // Target point position private float xTarget; // x-coordinate of target relative to datum private float xMax = -1000f; // x-coordinate of target relative to datum private float xMin = 1000f; // x-coordinate of target relative to datum // Serial Stuff SerialPort serial; // Function to initiate plugin public override void Init() { try { // Check this is the right kind of atom if (containingAtom.type != "Person") { SuperController.LogError($"Plugin must be added to a Person Atom, not '{containingAtom.type}'"); return; } pluginLabelJSON.val = "Serial Controller"; // Select COM Port portNames = System.IO.Ports.SerialPort.GetPortNames(); List<string> comPorts = new List<string>(portNames); //comPorts.Add("None"); //comPorts.Add("Choice1"); //comPorts.Add("Choice2"); //comPorts.Add("Choice3"); JSONStorableStringChooser portChooser = new JSONStorableStringChooser("COM Port Chooser", comPorts, "None", "Select COM port", PortChooserCallback); UIDynamicPopup portChooserPopup = CreatePopup(portChooser, false); portChooserPopup.labelWidth = 300f; // Select Baud Rate List<string> baudRates = new List<string>(); baudRates.Add("9600"); baudRates.Add("19200"); baudRates.Add("38400"); baudRates.Add("74880"); baudRates.Add("115200"); baudRates.Add("230400"); baudRates.Add("250000"); JSONStorableStringChooser baudChooser = new JSONStorableStringChooser("Baud Rate Chooser", baudRates, "9600", "Select baud rate", BaudChooserCallback); UIDynamicPopup baudChooserPopup = CreatePopup(baudChooser, false); baudChooserPopup.labelWidth = 300f; // Serial pulse rate control pulseFreq = new JSONStorableFloat("Serial Pulse Freq (/sec)", 50f, 1f, 100f, true, true); RegisterFloat(pulseFreq); CreateSlider(pulseFreq, false); // Serial buttons startbutton = CreateButton("Start Serial", false); if (startbutton != null) { startbutton.button.onClick.AddListener(StartButtonCallback); } stopbutton = CreateButton("Stop Serial", false); if (stopbutton != null) { stopbutton.button.onClick.AddListener(StopButtonCallback); } // Thrust position ThrustX = new JSONStorableFloat("X Position", 50f, 0f, 100f, true, true); RegisterFloat(ThrustX); CreateSlider(ThrustX, false); jstring = new JSONStorableString("FooString", "Text"); dtext = CreateTextField(jstring, false); // Identify reference points refObject = containingAtom; refObjA = refObject.GetStorableByID("penisBaseControl") as FreeControllerV3; refObjB = refObject.GetStorableByID("penisTipControl") as FreeControllerV3; targObject = SuperController.singleton.GetAtomByUid("Person"); targObjC = targObject.GetStorableByID("hipControl") as FreeControllerV3; } catch (Exception e) { SuperController.LogError("Exception caught: " + e); } } // Update is called with each rendered frame by Unity void Update() { try { refA = refObjA.transform.position; refB = refObjB.transform.position; targC = targObjC.transform.position; refLine = refB-refA; xTarget = Vector3.Dot(refLine,targC-refA); xTarget = xTarget/(refLine.magnitude*refLine.magnitude); if (xTarget > xMax) xMax = xTarget; if (xTarget < xMin) xMin = xTarget; ThrustX.val = 100*(xTarget-xMin)/(xMax-xMin); jstring.val = xMin.ToString("0.00") + " " + xTarget.ToString("0.00") + " " + xMax.ToString("0.00")+ " " + ThrustX.val.ToString("0"); // If serial active if (serial != null && serial.IsOpen) { // Watch for next serial pulse pulsetime -= Time.deltaTime; if (pulsetime <= 0f) { pulsetime = 1/pulseFreq.val; if(serial != null && serial.IsOpen) { SendPulse(); } } } } catch (Exception e) { SuperController.LogError("Exception caught: " + e); } } protected void PortChooserCallback(string s) { portSelected = s; } protected void BaudChooserCallback(string s) { baudSelected = s; } // Serial button functions protected void StartButtonCallback() { StartSerial(); SuperController.LogMessage("Serial connection started: " + portSelected + ", baud " + baudSelected ); } protected void StopButtonCallback() { StopSerial(); SuperController.LogMessage("Serial connection stopped"); } // Function to start serial void StartSerial() { if (portSelected != "None") { serial = new SerialPort(portSelected, Convert.ToInt32(baudSelected)); serial.Open(); serial.ReadTimeout = 10; } } // Function to stop serial void StopSerial() { if(serial != null && serial.IsOpen) serial.Close(); } // Function to send serial void SendPulse() { if (ThrustX.val >= 99f) { pulsestring = "L99"; } else if (ThrustX.val >= 10f) { pulsestring = "L" + string.Format("{0:0}",ThrustX.val); } else if (ThrustX.val >= 1f) { pulsestring = "L0" + string.Format("{0:0}",ThrustX.val); } else { pulsestring = "L00"; } serial.WriteLine(pulsestring); } // Function to close plugin void OnDestroy() { try { StopSerial(); } catch (Exception e) { SuperController.LogError("Exception caught: " + e); } } } } |
この記事のコピーは以下のサイトで販売している場合があります。
また、未販売の記事のリクエストをお受けしています。
https://neo-sahara.booth.pm/
販売開始リクエスト受付