using System; using System.IO.Ports; using System.Threading; using System.Text; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using GHIElectronics.NETMF.Hardware; using GHIElectronics.NETMF.FEZ; namespace CxemCAR { public class Program { public const char cmdL = 'L'; // UART public const char cmdR = 'R'; // UART public const char cmdH = 'H'; // UART . 1 ( Horn) public const char cmdF = 'F'; // UART EEPROM public const char cmdr = 'r'; // UART EEPROM () public const char cmdw = 'w'; // UART EEPROM () //public const int t_TOut = 2500; // - , static int sw_autoOFF; static int autoOFF = 2500; static byte[] storage = new byte[InternalFlashStorage.Size]; // FLASH static OutputPort MotorL_d = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di4, false); // 1 static OutputPort MotorR_d = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di7, false); // 2 static OutputPort Channel1 = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di8, false); // . 1 static PWM MotorL = new PWM((PWM.Pin)FEZ_Pin.PWM.Di5); // 1 () static PWM MotorR = new PWM((PWM.Pin)FEZ_Pin.PWM.Di6); // 2 () static SerialPort UART1 = new SerialPort("COM1", 9600); // UART1 ( COM1) static Timer timerTO; // public static void Main() { byte[] L_Data = new byte[4]; // L byte L_index = 0; // byte[] R_Data = new byte[4]; // R byte R_index = 0; // byte[] H_Data = new byte[1]; // . byte H_index = 0; // byte[] F_Data = new byte[8]; // EEPROM byte F_index = 0; char command = ' '; // : R, L, H, F int i_tmp_L = 0; int i_tmp_R = 0; int i_tmp_H = 0; byte[] incomingByte = new byte[1]; // UART UART1.Open(); UART1.Flush(); timerTO = new Timer(new TimerCallback(TimeOut), null, autoOFF, autoOFF); // timer_init(); // while (true) { int read_count = UART1.Read(incomingByte, 0, 1); // if (read_count > 0) // ? { if (incomingByte[0] == cmdL) // L { command = cmdL; // Array.Clear(L_Data, 0, L_Data.Length); // L_index = 0; // } else if (incomingByte[0] == cmdR) // R { command = cmdR; // Array.Clear(R_Data, 0, R_Data.Length); // R_index = 0; // } else if (incomingByte[0] == cmdH) // . 1 { command = cmdH; // Array.Clear(H_Data, 0, H_Data.Length); // H_index = 0; // } else if (incomingByte[0] == cmdF) // . 1 { command = cmdF; // Array.Clear(F_Data, 0, F_Data.Length); // F_index = 0; // } else if (incomingByte[0] == '\r') command = 'e'; // else if (incomingByte[0] == '\t') command = 't'; // if (command == cmdL && incomingByte[0] != cmdL) { if (ValidData(incomingByte[0])) { L_Data[L_index] = incomingByte[0]; // if (L_index < (L_Data.Length - 1)) L_index++; // } } else if (command == cmdR && incomingByte[0] != cmdR) { if (ValidData(incomingByte[0])) { R_Data[R_index] = incomingByte[0]; if (R_index < (R_Data.Length - 1)) R_index++; } } else if (command == cmdH && incomingByte[0] != cmdH) { if (ValidData(incomingByte[0])) { H_Data[H_index] = incomingByte[0]; if (H_index < (H_Data.Length - 1)) H_index++; } } else if (command == cmdF && incomingByte[0] != cmdF) { F_Data[F_index] = incomingByte[0]; if (F_index < (F_Data.Length - 1)) F_index++; } else if (command == 'e') // { timerTO.Dispose(); // string tmp_L = new string(System.Text.UTF8Encoding.UTF8.GetChars(L_Data)); // string tmp_R = new string(System.Text.UTF8Encoding.UTF8.GetChars(R_Data)); string tmp_H = new string(System.Text.UTF8Encoding.UTF8.GetChars(H_Data)); try { if (tmp_L != null) i_tmp_L = int.Parse(tmp_L); // int if (tmp_R != null) i_tmp_R = int.Parse(tmp_R); if (tmp_H != null) i_tmp_H = int.Parse(tmp_H); } catch { Debug.Print("Error: convert String to Integer"); } if (i_tmp_L > 100) i_tmp_L = 100; else if (i_tmp_L < -100) i_tmp_L = -100; if (i_tmp_R > 100) i_tmp_R = 100; else if (i_tmp_R < -100) i_tmp_R = -100; Control4WD(i_tmp_L, i_tmp_R, i_tmp_H); timerTO.Change(autoOFF, autoOFF); // } else if (command == 't') // { Flash_Op(F_Data[0], F_Data[1], F_Data[2], F_Data[3], F_Data[4]); } } } } static void Flash_Op(byte FCMD, byte z1, byte z2, byte z3, byte z4) { if (FCMD == cmdr && sw_autoOFF != 255) // EEPROM { byte[] buffer = Encoding.UTF8.GetBytes("FData:"); // UART UART1.Write(buffer, 0, buffer.Length); // UART byte[] buffer2 = new byte[4] { storage[0], storage[1], storage[2], storage[3] }; UART1.Write(buffer2, 0, buffer2.Length); byte[] buffer3 = Encoding.UTF8.GetBytes("\r\n"); UART1.Write(buffer3, 0, buffer3.Length); } else if (FCMD == cmdw) // EEPROM { byte[] varToSave = new byte[InternalFlashStorage.Size]; varToSave[0] = z1; varToSave[1] = z2; varToSave[2] = z3; varToSave[3] = z4; InternalFlashStorage.Write(varToSave); // FLASH timer_init(); // byte[] buffer2 = Encoding.UTF8.GetBytes("FWOK\r\n"); // UART UART1.Write(buffer2, 0, buffer2.Length); // , } } static void timer_init() { InternalFlashStorage.Read(storage); // FLASH sw_autoOFF = storage[0]; if(sw_autoOFF == '1'){ // byte[] var_Data= new byte[3]; var_Data[0] = storage[1]; var_Data[1] = storage[2]; var_Data[2] = storage[3]; string tmp_autoOFF = new string(System.Text.UTF8Encoding.UTF8.GetChars(var_Data)); autoOFF = int.Parse(tmp_autoOFF)*100; timerTO.Change(autoOFF, autoOFF); // } else if(sw_autoOFF == '0'){ timerTO.Dispose(); // } Debug.Print("Timer Init" + autoOFF.ToString()); } static void TimeOut(object o) { //Debug.Print(DateTime.Now.ToString()); Control4WD(0, 0, 0); // } public static void Control4WD(int mLeft, int mRight, int Horn) { bool directionL, directionR; // L298N int valueL, valueR; // M1, M2 (0-100) if (mLeft > 0) { valueL = mLeft; directionL = false; } else if (mLeft < 0) { valueL = 100 - System.Math.Abs(mLeft); directionL = true; } else { directionL = false; valueL = 0; } if (mRight > 0) { valueR = mRight; directionR = false; } else if (mRight < 0) { valueR = 100 - System.Math.Abs(mRight); directionR = true; } else { directionR = false; valueR = 0; } if (Horn == 1) { Channel1.Write(true); } else Channel1.Write(false); //Debug.Print("L:" + valueL.ToString() + ", R:" + valueR.ToString()); MotorL.Set(30000, (byte)(valueL)); MotorR.Set(30000, (byte)(valueR)); MotorL_d.Write(directionL); MotorR_d.Write(directionR); } public static bool ValidData(byte chIncom) // "0..9" "-" { if ((chIncom >= 0x30 && chIncom <= 0x39) || chIncom == 0x2D) return true; else return false; } } }
Source: https://habr.com/ru/post/168723/
All Articles