public static nxt_result motor_off(NXTBrick.Motor motor) { nxt_result result = new nxt_result(); NXTBrick.MotorState motorState = new NXTBrick.MotorState(); // prepare motor's state to set motorState.Power = (sbyte)0; motorState.TurnRatio = (sbyte)0; motorState.Mode = NXTBrick.MotorMode.None; motorState.Regulation = NXTBrick.MotorRegulationMode.Speed; motorState.RunState = NXTBrick.MotorRunState.Idle; // tacho limit motorState.TachoLimit = 0; // set motor's state if (nxt.SetMotorState(motor, motorState) != true) { IrcBot.log += "Failed setting motor state\n"; result.result = false; result.value = "Failed"; return(result); } result.result = true; result.value = "succed"; return(result); }
public static nxt_result motor_degree(string motors, string degree) { string[] motors_array; nxt_result motor_result = new nxt_result(); motor_result.result = false; motor_result.value = "Motor not found."; motors_array = motors.ToLower().Split('v'); foreach (string motor in motors_array) { if (motor.ToUpper() == "A") { motor_result = motor_degree(NXTBrick.Motor.A, Convert.ToInt32(degree)); } if (motor.ToUpper() == "B") { motor_result = motor_degree(NXTBrick.Motor.B, Convert.ToInt32(degree)); } if (motor.ToUpper() == "C") { motor_result = motor_degree(NXTBrick.Motor.C, Convert.ToInt32(degree)); } if (motor.ToUpper() == "ALL") { motor_result = motor_degree(NXTBrick.Motor.All, Convert.ToInt32(degree)); } if (motor_result.result == false) { return(motor_result); } } return(motor_result); }
public static nxt_result motor_off(string motors) { string[] motors_array; nxt_result motor_result = new nxt_result(); motor_result.result = false; motor_result.value = "Motor not found."; motors_array = motors.ToLower().Split('v'); foreach (string motor in motors_array) { if (motor.ToUpper() == "A") { motor_result = motor_off(NXTBrick.Motor.A); } if (motor.ToUpper() == "B") { motor_result = motor_off(NXTBrick.Motor.B); } if (motor.ToUpper() == "C") { motor_result = motor_off(NXTBrick.Motor.C); } if (motor.ToUpper() == "ALL") { motor_result = motor_off(NXTBrick.Motor.All); } if (motor_result.result == false) { return(motor_result); } } return(motor_result); }
public static nxt_result motor_degree(NXTBrick.Motor motor, int degree) { nxt_result result = new nxt_result(); NXTBrick.MotorState motorState = new NXTBrick.MotorState(); if (motor == NXTBrick.Motor.A) { // prepare motor's state to set motorState.Power = (sbyte)speed_motors[0]; motorState.TurnRatio = (sbyte)speed_motors[0]; } if (motor == NXTBrick.Motor.B) { // prepare motor's state to set motorState.Power = (sbyte)speed_motors[1]; motorState.TurnRatio = (sbyte)speed_motors[1]; } if (motor == NXTBrick.Motor.C) { // prepare motor's state to set motorState.Power = (sbyte)speed_motors[2]; motorState.TurnRatio = (sbyte)speed_motors[2]; } // TODO If al motor's on handel correct speed if (motor == NXTBrick.Motor.All) { // prepare motor's state to set motorState.Power = (sbyte)speed_motors[0]; motorState.TurnRatio = (sbyte)speed_motors[0]; } motorState.Mode = NXTBrick.MotorMode.On; motorState.Regulation = NXTBrick.MotorRegulationMode.Speed; motorState.RunState = NXTBrick.MotorRunState.Running; // tacho limit motorState.TachoLimit = degree; // set motor's state if (nxt.SetMotorState(motor, motorState) != true) { IrcBot.log += "Failed setting motor state\n"; result.result = false; result.value = "Failed"; return(result); } result.result = true; result.value = "succed"; return(result); }
private void timer2_Tick(object sender, EventArgs e) { if (config.sensor_auto_on_off) { string temp, sql; TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)); double unixTime = ts.TotalSeconds; //send commands nxt_result[] cmd = new nxt_result[4]; temp = ""; if (sensor1type.SelectedIndex != -1) { cmd[0] = NXT_ROVER_CONTROL.command_translation("cmd sensor value 1"); temp += "Sensor 1: " + cmd[0].value + "\n"; } if (sensor2type.SelectedIndex != -1) { cmd[1] = NXT_ROVER_CONTROL.command_translation("cmd sensor value 2"); temp += "Sensor 2: " + cmd[1].value + "\n"; } if (sensor3type.SelectedIndex != -1) { cmd[2] = NXT_ROVER_CONTROL.command_translation("cmd sensor value 3"); temp += "Sensor 3: " + cmd[2].value + "\n"; } if (sensor4type.SelectedIndex != -1) { cmd[3] = NXT_ROVER_CONTROL.command_translation("cmd sensor value 4"); temp += "Sensor 4: " + cmd[3].value + "\n"; } //update database sql = "INSERT INTO `rover`.`sensors` (`ID`, `when`, `result`) VALUES (NULL, '" + unixTime.ToString().Replace(',', '.') + "', '" + temp + "');"; mysql.QueryCommand(sql); //show if (temp.Length < 5) { status.Text = "Please select type of sensor below."; } else { status.Text = temp; } } }
public static nxt_result run_program(string name) { nxt_result result = new nxt_result(); if (nxt.RunProgram(name + ".RXE")) { result.result = true; result.value = "succed"; } else { result.result = false; result.value = "Failed"; } return(result); }
public static nxt_result stop_program() { nxt_result result = new nxt_result(); if (nxt.StopProgram()) { result.result = true; result.value = "succed"; } else { result.result = false; result.value = "Failed"; } return(result); }
public static nxt_result set_speed(string motors, int speed) { string[] motors_array; nxt_result motor_result = new nxt_result(); motor_result.result = false; motor_result.value = "Motor not found."; motors_array = motors.ToLower().Split('v'); foreach (string motor in motors_array) { if (motor.ToUpper() == "A") { motor_result.result = true; motor_result.value = "done!"; speed_motors[0] = speed; } if (motor.ToUpper() == "B") { motor_result.result = true; motor_result.value = "done!"; speed_motors[1] = speed; } if (motor.ToUpper() == "C") { motor_result.result = true; motor_result.value = "done!"; speed_motors[2] = speed; } if (motor.ToUpper() == "ALL") { motor_result.result = true; motor_result.value = "done!"; speed_motors[0] = speed; speed_motors[1] = speed; speed_motors[2] = speed; } if (motor_result.result == false) { return(motor_result); } } return(motor_result); }
public static void check() { TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)); double unixTime = ts.TotalSeconds; mysql_results res = new mysql_results(); nxt_result result = new nxt_result(); //check mysql conntection if (mysql.connection.State == ConnectionState.Open || mysql.connection.State == ConnectionState.Connecting) { res = mysql.QueryCommand("SELECT `ID`,`message`,`when` FROM `log_current_session` WHERE `status`='' AND `when`< '" + unixTime.ToString().Replace(',', '.') + "' AND `when`> '" + (unixTime - 100).ToString().Replace(',', '.') + "' AND `type`='cmd' ORDER BY `when` ASC LIMIT 0,1"); if (res.result) { IrcBot.log += DateTime.Now + "checking(" + res.msg + ")..."; result = NXT_ROVER_CONTROL.command_translation(res.msg); if (result.result) { //true cmd.CommandText = "UPDATE `log_current_session` SET `status`='" + result.value + "', `when`='" + unixTime.ToString().Replace(',', '.') + "' WHERE `ID`=" + res.ID; cmd.CommandType = CommandType.Text; MySqlDataReader reader = cmd.ExecuteReader(); reader.Close(); IrcBot.log += "succed\n"; } else { //false cmd.CommandText = "UPDATE `log_current_session` SET `status`='" + result.value + "' WHERE `ID`=" + res.ID; cmd.CommandType = CommandType.Text; MySqlDataReader reader = cmd.ExecuteReader(); reader.Close(); IrcBot.log += "faild\n"; } } } else { //make connecion IrcBot.log += "Mysql connection faild"; mysql.connect(); } }
public static nxt_result run_program_mysql(int name) { nxt_result result = new nxt_result(); mysql_results result_mysql = new mysql_results(); result_mysql = mysql.pre_program(Convert.ToString(name)); if (result_mysql.result == true) { result.result = true; result.value = "succed"; } else { result.result = false; result.value = "Faild"; } return(result); }
public static nxt_result get_sensor(int sensor) { nxt_result result = new nxt_result(); NXTBrick.SensorValues sensorValues; result.result = false; result.type = "sensor"; result.value = "0"; // get input values if (nxt.GetSensorValue(sensors[sensor - 1], out sensorValues)) { result.result = sensorValues.IsValid; result.value = "Raw: " + sensorValues.Raw.ToString() + " normalized: " + sensorValues.Normalized.ToString(); } else { IrcBot.log += "Failed getting input values\n"; } return(result); }
private void timer2_Tick(object sender, EventArgs e) { if (config.sensor_auto_on_off) { string temp,sql; TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)); double unixTime = ts.TotalSeconds; //send commands nxt_result[] cmd = new nxt_result[4]; temp = ""; if (sensor1type.SelectedIndex != -1) { cmd[0] = NXT_ROVER_CONTROL.command_translation("cmd sensor value 1"); temp += "Sensor 1: " + cmd[0].value + "\n"; } if (sensor2type.SelectedIndex != -1) { cmd[1] = NXT_ROVER_CONTROL.command_translation("cmd sensor value 2"); temp += "Sensor 2: " + cmd[1].value + "\n"; } if (sensor3type.SelectedIndex != -1) { cmd[2] = NXT_ROVER_CONTROL.command_translation("cmd sensor value 3"); temp += "Sensor 3: " + cmd[2].value + "\n"; } if (sensor4type.SelectedIndex != -1) { cmd[3] = NXT_ROVER_CONTROL.command_translation("cmd sensor value 4"); temp += "Sensor 4: " + cmd[3].value + "\n"; } //update database sql = "INSERT INTO `rover`.`sensors` (`ID`, `when`, `result`) VALUES (NULL, '" + unixTime.ToString().Replace(',', '.') + "', '" + temp + "');"; mysql.QueryCommand(sql); //show if (temp.Length < 5) { status.Text = "Please select type of sensor below."; } else { status.Text = temp; } } }
public static nxt_result stop_program() { nxt_result result = new nxt_result(); if (nxt.StopProgram()) { result.result = true; result.value = "succed"; } else { result.result = false; result.value = "Failed"; } return result; }
public static nxt_result set_speed(string motors,int speed) { string[] motors_array; nxt_result motor_result = new nxt_result(); motor_result.result = false; motor_result.value = "Motor not found."; motors_array = motors.ToLower().Split('v'); foreach (string motor in motors_array) { if (motor.ToUpper() == "A") { motor_result.result = true; motor_result.value = "done!"; speed_motors[0] = speed; } if (motor.ToUpper() == "B") { motor_result.result = true; motor_result.value = "done!"; speed_motors[1] = speed; } if (motor.ToUpper() == "C") { motor_result.result = true; motor_result.value = "done!"; speed_motors[2] = speed; } if (motor.ToUpper() == "ALL") { motor_result.result = true; motor_result.value = "done!"; speed_motors[0] = speed; speed_motors[1] = speed; speed_motors[2] = speed; } if (motor_result.result == false) { return motor_result; } } return motor_result; }
public static nxt_result run_program_mysql(int name) { nxt_result result = new nxt_result(); mysql_results result_mysql = new mysql_results(); result_mysql = mysql.pre_program(Convert.ToString(name)); if (result_mysql.result == true) { result.result = true; result.value = "succed"; } else { result.result = false; result.value = "Faild"; } return result; }
public static nxt_result run_program(string name) { nxt_result result = new nxt_result(); if (nxt.RunProgram(name + ".RXE")) { result.result = true; result.value = "succed"; } else { result.result = false; result.value = "Failed"; } return result; }
/** * This function will read out a sensor * @param sensor sensor number * @return nxt_result */ public static nxt_result get_sensor(int sensor) { nxt_result result = new nxt_result(); NXTBrick.SensorValues sensorValues; result.result = false; result.type = "sensor"; result.value = "0"; int key; // get input values if (nxt.GetSensorValue(sensors[sensor-1], out sensorValues)) { result.result = sensorValues.IsValid; key = 0; foreach(NXTBrick.SensorType sensorType_sp in sensorTypes){ if (sensorValues.SensorType == sensorType_sp) { break; }else{ key++; } } result.value = sensorValues.Scaled.ToString() + unit[key]; } else { IrcBot.log += "Failed getting input values\n"; } return result; }
/** * This function will translate a text command to the proper function * @return nxt_result */ public static nxt_result command_translation(string text_command) { try { while (command_translation_inprogress == true) { Thread.Sleep(10); } command_translation_inprogress = true; nxt_result result = new nxt_result(); result.result = false; result.value = "Faild to translate"; arg_command = text_command.Split(' '); if (arg_command.Length == 4) { if (arg_command[1] == "run" && arg_command[2] == "program") { command_translation_inprogress = false; return run_program_mysql(arg_command[3]); } } //check if NXT is connect if (!nxt.IsConnected) { result.result = false; result.value = "NXT is not connect!"; command_translation_inprogress = false; return result; } //check type if (arg_command.Length == 4) { if (arg_command[1] == "motor" && arg_command[3] == "on") { command_translation_inprogress = false; return motor_on(arg_command[2]); } if (arg_command[1] == "motor" && arg_command[3] == "off") { command_translation_inprogress = false; return motor_off(arg_command[2]); } } if (arg_command.Length == 5) { if (arg_command[1] == "motor" && arg_command[2] == "speed") { command_translation_inprogress = false; return set_speed(arg_command[3], arg_command[4]); } if (arg_command[1] == "motor" && arg_command[2] == "degrees") { command_translation_inprogress = false; return motor_on(arg_command[3], arg_command[4]); } } if (arg_command.Length == 4) { if (arg_command[1] == "sensor" && arg_command[2] == "value") { command_translation_inprogress = false; return get_sensor(arg_command[3]); } } if (arg_command.Length == 5) { if (arg_command[1] == "run" && arg_command[2] == "rover" && arg_command[3] == "program") { command_translation_inprogress = false; return run_program(arg_command[4]); } } if (arg_command.Length == 6) { if (arg_command[1] == "download" && arg_command[2] == "rover" && arg_command[3] == "program") { command_translation_inprogress = false; return download_program_nbc(arg_command[5], config.nbc_path + arg_command[4] + ".nbc"); } } if (arg_command.Length == 4) { if (arg_command[1] == "stop" && arg_command[2] == "rover" && arg_command[2] == "program") { command_translation_inprogress = false; return stop_program(); } } return result; } catch { IrcBot.log += "ERROR: Failed translate command\n"; nxt_result failed = new nxt_result(); failed.result = false; failed.value = "failed"; command_translation_inprogress = false; return failed; } }
/** * This will delete a rover program * @param name Name of program without .RXE * @return nxt_result */ public static nxt_result delete_rover_program(string name) { nxt_result result = new nxt_result(); if (nxt.delete_command(name + ".rxe")) { result.result = true; result.value = "succed"; } else { result.result = false; result.value = "Failed"; } return result; }
public static void run_irc() { NetworkStream stream; TcpClient irc; string inputLine; StreamReader reader; try { irc = new TcpClient(SERVER, PORT); stream = irc.GetStream(); reader = new StreamReader(stream); writer = new StreamWriter(stream); // Start PingSender thread //PingSender ping = new PingSender (); //ping.Start(); inputLine = reader.ReadLine(); log += inputLine + "\n"; //MessageBox.Show(inputLine); ident(); IrcBot.log += "IRC chat ready\n"; nxt_result result = new nxt_result(); while (true) { while ((inputLine = reader.ReadLine()) != null) { //log += inputLine + "\n"; if (inputLine.Contains("PING")) { // inputLine.Split(' '); //MessageBox.Show(inputLine); //MessageBox.Show(inputLine.Substring(inputLine.IndexOf(" ") + 2)); //log += "send: " + "PONG " + inputLine.Substring(inputLine.IndexOf(" ") + 2) + "\n"; writer.WriteLine("PONG " + inputLine.Substring(inputLine.IndexOf(" ") + 2)); writer.Flush(); } if (inputLine.Contains("Register first.")) { ident(); } if (inputLine.Contains(CHANNEL + " :")) { //echo // :[email protected] PRIVMSG #control :hello mes_buffer = inputLine.Substring(inputLine.IndexOf(CHANNEL + " :") + CHANNEL.Length + 2); log += mes_buffer + "\n"; if (mes_buffer.StartsWith("cmd ")) { result = NXT_ROVER_CONTROL.command_translation(mes_buffer); if (result.result) { writer.WriteLine(":" + NICK + "! PRIVMSG " + CHANNEL + " :Result " + result.value); writer.Flush(); } else { writer.WriteLine(":" + NICK + "! PRIVMSG " + CHANNEL + " :Result faild"); writer.Flush(); } } else { writer.WriteLine(":" + NICK + "! PRIVMSG " + CHANNEL + " :Result false, Not a command"); writer.Flush(); } } } // Close all streams writer.Close(); reader.Close(); irc.Close(); } } catch (Exception e) { // Show the exception, sleep for a while and try to establish a new connection to irc server log += e.Message.ToString() + "\n"; timesleep *= 2; Thread.Sleep(timesleep); run_irc(); } }
public static nxt_result motor_off(NXTBrick.Motor motor) { nxt_result result = new nxt_result(); NXTBrick.MotorState motorState = new NXTBrick.MotorState(); // prepare motor's state to set motorState.Power = (sbyte)0; motorState.TurnRatio = (sbyte)0; motorState.Mode = NXTBrick.MotorMode.None; motorState.Regulation = NXTBrick.MotorRegulationMode.Speed; motorState.RunState = NXTBrick.MotorRunState.Idle; // tacho limit motorState.TachoLimit = 0; // set motor's state if (nxt.SetMotorState(motor, motorState) != true) { IrcBot.log += "Failed setting motor state\n"; result.result = false; result.value = "Failed"; return result; } result.result = true; result.value = "succed"; return result; }
public static nxt_result motor_degree(string motors, string degree) { string[] motors_array; nxt_result motor_result = new nxt_result(); motor_result.result = false; motor_result.value = "Motor not found."; motors_array = motors.ToLower().Split('v'); foreach (string motor in motors_array) { if (motor.ToUpper() == "A") { motor_result = motor_degree(NXTBrick.Motor.A,Convert.ToInt32(degree)); } if (motor.ToUpper() == "B") { motor_result = motor_degree(NXTBrick.Motor.B, Convert.ToInt32(degree)); } if (motor.ToUpper() == "C") { motor_result = motor_degree(NXTBrick.Motor.C, Convert.ToInt32(degree)); } if (motor.ToUpper() == "ALL") { motor_result = motor_degree(NXTBrick.Motor.All, Convert.ToInt32(degree)); } if (motor_result.result == false) { return motor_result; } } return motor_result; }
public static nxt_result command_translation(string text_command) { nxt_result result = new nxt_result(); arg_command = text_command.Split(' '); if (arg_command.Length == 4) { if (arg_command[1] == "run" && arg_command[2] == "program") { return run_program_mysql(arg_command[3]); } } //check if NXT is connect if (!nxt.IsConnected) { result.result = false; result.value = "NXT is not connect!"; return result; } //check type if (arg_command.Length == 4) { if (arg_command[1] == "motor" && arg_command[3] == "on") { return motor_on(arg_command[2]); } if (arg_command[1] == "motor" && arg_command[3] == "off") { return motor_off(arg_command[2]); } } if (arg_command.Length == 5) { if (arg_command[1] == "motor" && arg_command[2] == "speed") { return set_speed(arg_command[3],arg_command[4]); } if (arg_command[1] == "motor" && arg_command[2] == "degree") { return set_speed(arg_command[3], arg_command[4]); } } if (arg_command.Length == 4) { if (arg_command[1] == "sensor" && arg_command[2] == "value") { return get_sensor(arg_command[3]); } } if (arg_command.Length == 5) { if (arg_command[1] == "run" && arg_command[2] == "rover" && arg_command[3] == "program") { return run_program(arg_command[4]); } } if (arg_command.Length == 4) { if (arg_command[1] == "stop" && arg_command[2] == "rover" && arg_command[2] == "program") { return stop_program(); } } return result; }
public static void run_irc() { NetworkStream stream; TcpClient irc; string inputLine; StreamReader reader; try { irc = new TcpClient (SERVER, PORT); stream = irc.GetStream (); reader = new StreamReader (stream); writer = new StreamWriter (stream); // Start PingSender thread //PingSender ping = new PingSender (); //ping.Start(); inputLine = reader.ReadLine(); log += inputLine + "\n"; //MessageBox.Show(inputLine); ident(); IrcBot.log += "IRC chat ready\n"; nxt_result result = new nxt_result(); while (true) { while ( (inputLine = reader.ReadLine () ) != null ) { //log += inputLine + "\n"; if (inputLine.Contains("PING")) { // inputLine.Split(' '); //MessageBox.Show(inputLine); //MessageBox.Show(inputLine.Substring(inputLine.IndexOf(" ") + 2)); //log += "send: " + "PONG " + inputLine.Substring(inputLine.IndexOf(" ") + 2) + "\n"; writer.WriteLine("PONG " + inputLine.Substring(inputLine.IndexOf(" ") + 2)); writer.Flush(); } if (inputLine.Contains("Register first.")) { ident(); } if (inputLine.Contains(CHANNEL + " :")) { //echo // :[email protected] PRIVMSG #control :hello mes_buffer = inputLine.Substring(inputLine.IndexOf(CHANNEL + " :")+ CHANNEL.Length + 2); log += mes_buffer + "\n"; if (mes_buffer.StartsWith("cmd ")) { result = NXT_ROVER_CONTROL.command_translation(mes_buffer); if (result.result) { writer.WriteLine(":" + NICK + "! PRIVMSG " + CHANNEL + " :Result " + result.value); writer.Flush(); } else { writer.WriteLine(":" + NICK + "! PRIVMSG " + CHANNEL + " :Result faild"); writer.Flush(); } } else { writer.WriteLine(":" + NICK + "! PRIVMSG " + CHANNEL + " :Result false, Not a command"); writer.Flush(); } } } // Close all streams writer.Close (); reader.Close (); irc.Close (); } } catch (Exception e) { // Show the exception, sleep for a while and try to establish a new connection to irc server log += e.Message.ToString () + "\n"; timesleep *= 2; Thread.Sleep (timesleep); run_irc (); } }
public static nxt_result command_translation(string text_command) { nxt_result result = new nxt_result(); arg_command = text_command.Split(' '); if (arg_command.Length == 4) { if (arg_command[1] == "run" && arg_command[2] == "program") { return(run_program_mysql(arg_command[3])); } } //check if NXT is connect if (!nxt.IsConnected) { result.result = false; result.value = "NXT is not connect!"; return(result); } //check type if (arg_command.Length == 4) { if (arg_command[1] == "motor" && arg_command[3] == "on") { return(motor_on(arg_command[2])); } if (arg_command[1] == "motor" && arg_command[3] == "off") { return(motor_off(arg_command[2])); } } if (arg_command.Length == 5) { if (arg_command[1] == "motor" && arg_command[2] == "speed") { return(set_speed(arg_command[3], arg_command[4])); } if (arg_command[1] == "motor" && arg_command[2] == "degree") { return(set_speed(arg_command[3], arg_command[4])); } } if (arg_command.Length == 4) { if (arg_command[1] == "sensor" && arg_command[2] == "value") { return(get_sensor(arg_command[3])); } } if (arg_command.Length == 5) { if (arg_command[1] == "run" && arg_command[2] == "rover" && arg_command[3] == "program") { return(run_program(arg_command[4])); } } if (arg_command.Length == 4) { if (arg_command[1] == "stop" && arg_command[2] == "rover" && arg_command[2] == "program") { return(stop_program()); } } return(result); }
public static void check() { TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)); double unixTime = ts.TotalSeconds; mysql_results res = new mysql_results(); nxt_result result = new nxt_result(); //check mysql conntection if (mysql.connection.State == ConnectionState.Open || mysql.connection.State == ConnectionState.Connecting) { res = mysql.QueryCommand("SELECT `ID`,`message`,`when` FROM `log_current_session` WHERE `status`='' AND `when`< '" + unixTime.ToString().Replace(',', '.') + "' AND `when`> '" + (unixTime-100).ToString().Replace(',', '.') + "' AND `type`='cmd' ORDER BY `when` ASC LIMIT 0,1"); if (res.result) { IrcBot.log += DateTime.Now + "checking(" + res.msg + ")..."; result = NXT_ROVER_CONTROL.command_translation(res.msg); if (result.result) { //true cmd.CommandText = "UPDATE `log_current_session` SET `status`='" + result.value + "', `when`='" + unixTime.ToString().Replace(',', '.') + "' WHERE `ID`=" + res.ID; cmd.CommandType = CommandType.Text; MySqlDataReader reader = cmd.ExecuteReader(); reader.Close(); IrcBot.log += "succed\n"; } else { //false cmd.CommandText = "UPDATE `log_current_session` SET `status`='" + result.value + "' WHERE `ID`=" + res.ID; cmd.CommandType = CommandType.Text; MySqlDataReader reader = cmd.ExecuteReader(); reader.Close(); IrcBot.log += "faild\n"; } } } else { //make connecion IrcBot.log += "Mysql connection faild"; mysql.connect(); } }
public static nxt_result motor_on(string motors, string degrees) { string[] motors_array; nxt_result motor_result = new nxt_result(); motor_result.result = false; motor_result.value = "Motor not found."; motors = motors.ToLower(); int number = 1; if (motors == "all") { motors = "AvBvC"; } motors_array = motors.Split('v'); foreach (string motor in motors_array) { if (motor.ToUpper() == "A") { motor_result = motor_on(NXTBrick.Motor.A, Convert.ToInt32(degrees), (motors_array.Length)-number); number++; } if (motor.ToUpper() == "B") { motor_result = motor_on(NXTBrick.Motor.B, Convert.ToInt32(degrees), (motors_array.Length) - number); number++; } if (motor.ToUpper() == "C") { motor_result = motor_on(NXTBrick.Motor.C, Convert.ToInt32(degrees), (motors_array.Length) - number); number++; } if (motor.ToUpper() == "ALL") { motor_result = motor_on(NXTBrick.Motor.All, Convert.ToInt32(degrees), (motors_array.Length) - number); number++; } if (motor_result.result == false) { return motor_result; } } return motor_result; }
public static nxt_result get_sensor(int sensor) { nxt_result result = new nxt_result(); NXTBrick.SensorValues sensorValues; result.result = false; result.type = "sensor"; result.value = "0"; // get input values if (nxt.GetSensorValue(sensors[sensor-1], out sensorValues)) { result.result = sensorValues.IsValid; result.value = "Raw: " + sensorValues.Raw.ToString() + " normalized: " + sensorValues.Normalized.ToString(); } else { IrcBot.log += "Failed getting input values\n"; } return result; }
/** * This will set the motor on * @param motor type NXTBrick.Motor * @return nxt_result */ public static nxt_result motor_on(NXTBrick.Motor motor, int degrees, int number) { //Debug.WriteLine("Set motor on"); //Debug.WriteLine(motor); nxt_result result = new nxt_result(); NXTBrick.MotorState motorState = new NXTBrick.MotorState(); if (motor == NXTBrick.Motor.A) { // prepare motor's state to set motorState.Power = (sbyte)speed_motors[0]; motorState.TurnRatio = 50; } if (motor == NXTBrick.Motor.B) { // prepare motor's state to set motorState.Power = (sbyte)speed_motors[1]; motorState.TurnRatio = 50; } if (motor == NXTBrick.Motor.C) { // prepare motor's state to set motorState.Power = (sbyte)speed_motors[2]; motorState.TurnRatio = 50; } // TODO If al motor's on handel correct speed if (motor == NXTBrick.Motor.All) { // prepare motor's state to set motorState.Power = (sbyte)speed_motors[0]; motorState.TurnRatio = 50; } motorState.Mode = NXTBrick.MotorMode.On; motorState.Regulation = NXTBrick.MotorRegulationMode.Idle; motorState.RunState = NXTBrick.MotorRunState.Running; motorState.RotationCount = 0; // tacho limit motorState.TachoCount = 0; if (motorState.Power < 0) { motorState.TachoLimit = -degrees; } else { motorState.TachoLimit = degrees; } // set motor's state if (nxt.SetMotorState(motor, motorState) != true) { IrcBot.log += "Failed setting motor state\n"; result.result = false; result.value = "Failed"; return result; } result.result = true; result.value = "succed"; return result; }
public static nxt_result motor_degree(NXTBrick.Motor motor, int degree) { nxt_result result = new nxt_result(); NXTBrick.MotorState motorState = new NXTBrick.MotorState(); if (motor == NXTBrick.Motor.A) { // prepare motor's state to set motorState.Power = (sbyte)speed_motors[0]; motorState.TurnRatio = (sbyte)speed_motors[0]; } if (motor == NXTBrick.Motor.B) { // prepare motor's state to set motorState.Power = (sbyte)speed_motors[1]; motorState.TurnRatio = (sbyte)speed_motors[1]; } if (motor == NXTBrick.Motor.C) { // prepare motor's state to set motorState.Power = (sbyte)speed_motors[2]; motorState.TurnRatio = (sbyte)speed_motors[2]; } // TODO If al motor's on handel correct speed if (motor == NXTBrick.Motor.All) { // prepare motor's state to set motorState.Power = (sbyte)speed_motors[0]; motorState.TurnRatio = (sbyte)speed_motors[0]; } motorState.Mode = NXTBrick.MotorMode.On; motorState.Regulation = NXTBrick.MotorRegulationMode.Speed; motorState.RunState = NXTBrick.MotorRunState.Running; // tacho limit motorState.TachoLimit = degree; // set motor's state if (nxt.SetMotorState(motor, motorState) != true) { IrcBot.log += "Failed setting motor state\n"; result.result = false; result.value = "Failed"; return result; } result.result = true; result.value = "succed"; return result; }
/** * This will compile and download a rover program * @param name Name of program without .RXE * @param path Localtion of the file on the computer(NBC) * @return nxt_result */ public static nxt_result download_program_nbc(string name, string path) { nxt_result result = new nxt_result(); string output_data = ""; try{ output_data = compile("\""+path + "\" -O=result.rxe"); IrcBot.log += output_data; if (output_data.Length > 1) { result.result = false; result.value = output_data.Replace("\n", "<br />").Replace("\\", "/"); return result; } /* Process proc = new Process(); proc.StartInfo.FileName = "nbc.exe"; proc.StartInfo.Arguments = path + " -O=result.rxe"; //proc.StartInfo.RedirectStandardOutput = true; //proc.StartInfo.RedirectStandardError = true; // proc.StartInfo.UseShellExecute = true; proc.Start(); //System.IO.StreamReader myOutput = proc.StandardOutput; proc.WaitForExit(10000); //output_data = myOutput.ReadToEnd(); */ //remove old one delete_rover_program(name); return download_program(name, "result.rxe"); } catch { result.result = false; result.value = "Failed"; return result; } }
public static nxt_result motor_on(string motors) { string[] motors_array; nxt_result motor_result = new nxt_result(); motor_result.result = false; motor_result.value = "Motor not found."; motors_array = motors.ToLower().Split('v'); foreach (string motor in motors_array) { if (motor.ToUpper() == "A") { motor_result = motor_on(NXTBrick.Motor.A); } if (motor.ToUpper() == "B") { motor_result = motor_on(NXTBrick.Motor.B); } if (motor.ToUpper() == "C") { motor_result = motor_on(NXTBrick.Motor.C); } if (motor.ToUpper() == "ALL") { motor_result = motor_on(NXTBrick.Motor.All); } if (motor_result.result == false) { return motor_result; } } return motor_result; }
/** * This will download a rover program * @param name Name of program without .RXE * @param path Localtion of the file on the computer * @return nxt_result */ public static nxt_result download_program(string name, string path) { nxt_result result = new nxt_result(); try { FileStream textFile = new FileStream(path, FileMode.Open); byte[] buffer = new byte[textFile.Length]; textFile.Read(buffer, 0, buffer.Length); textFile.Close(); if (nxt.download_program(name + ".rxe", buffer)) { result.result = true; result.value = "succed"; } else { result.result = false; result.value = "Failed"; } return result; } catch { result.result = false; result.value = "Failed"; return result; } }