//code adapted from https://www.hackster.io/oztl/near-perfect-gyroscope-e5e10e /// <summary> /// Create new MPU 6050 object and initiate device /// </summary> /// <param name="busID">Serial bus ID on ESP32 device (1 or 2)</param> /// <returns></returns> public static Mpu6050 StartNew(int busID) { var tmp = new Mpu6050(busID); tmp.Start(); return(tmp); }
public static void Main() { #if platform_test && !platform_beetle Configuration.SetPinFunction(23, DeviceFunction.I2C1_DATA); Configuration.SetPinFunction(22, DeviceFunction.I2C1_CLOCK); _mpu = Mpu6050.StartNew(1); #elif !platform_test && platform_beetle _mpu = Mpu6050.StartNew(2); #endif _mpuArgList = new ArrayList() { "", "", "" }; _led = new PinController.LedBlinker(2); _touchController.Init(); _palmTouch = _touchController.OpenPin(27, new TouchPadConfig() { PinSelectMode = TouchPinSelectMode.GpioIndex }); _triggerTouch = _touchController.OpenPin(13, new TouchPadConfig() { PinSelectMode = TouchPinSelectMode.GpioIndex }); Debug.WriteLine("Init"); _led.On(); Thread.Sleep(100); _led.Off(); var AppID = "Esp32.HairTrigger"; var serverUdpPort = 48597; var nanoConfig = new ArdNetClientConfig(AppID, null, serverUdpPort); nanoConfig.TCP.HeartbeatConfig.HeartbeatInterval = TimeSpan.FromMilliseconds(2000); nanoConfig.TCP.HeartbeatConfig.ForceStrictHeartbeat = true; nanoConfig.TCP.HeartbeatConfig.RespondToHeartbeats = false; _ardManager = new ArdNetClientManager(SystemConfig.WiFiCredentials, nanoConfig); _ardManager.TcpEndpointConnected += ArdManager_TcpEndpointConnected; _ardManager.TcpEndpointDisconnected += NanoClient_ServerDisconnected; _ardManager.StartWorkerThread(); Debug.WriteLine("Loop"); MpuValue data; DateTime now; while (true) { _ = _serverMsgHandle.WaitOne(); now = DateTime.UtcNow; if (!_palmTouch.IsTouched()) { Thread.Sleep(10); continue; } if ((now - _mpuLastSend).TotalMilliseconds > 75) { data = _mpu.GetData(); //gyro data //x, y, z _mpuArgList[0] = data.GyroX.ToString(); _mpuArgList[1] = data.GyroY.ToString(); _mpuArgList[2] = data.GyroZ.ToString(); var request = TcpRequest.CreateOutbound("MPU", _mpuArgList); _ardManager.EnqueueTask(x => { _ = x.SendCommand(request); }); _mpuLastSend = now; } now = DateTime.UtcNow; if (!_triggerIsDown && (now - _triggerLastSend).TotalMilliseconds > 50) { if (_triggerTouch.IsTouched()) { _ardManager.EnqueueTask(x => x.SendCommand(_triggerTcpRequest_Down)); _triggerIsDown = true; _triggerLastSend = now; } } else if (_triggerIsDown && (now - _triggerLastSend).TotalMilliseconds > 10) { if (!_triggerTouch.IsTouched()) { _ardManager.EnqueueTask(x => x.SendCommand(_triggerTcpRequest_Up)); _triggerIsDown = false; _triggerLastSend = now; } } } }