public static void Main() { //distance sensor //triger (12), echo (13), 5V + G HC_SR04 sensor = new HC_SR04(Pins.GPIO_PIN_D12, Pins.GPIO_PIN_D13); //buzzer OutputPort Klakson = new OutputPort(Pins.GPIO_PIN_D0, false); //motor driver HBridge MotorDriver = new HBridge(SecretLabs.NETMF.Hardware.Netduino.PWMChannels.PWM_PIN_D5, Pins.GPIO_PIN_D4, SecretLabs.NETMF.Hardware.Netduino.PWMChannels.PWM_PIN_D6, Pins.GPIO_PIN_D7); //led indicator if there is an object in the front of robot OutputPort WarningLed = new OutputPort(Pins.GPIO_PIN_D1, false); OutputPort GoLed = new OutputPort(Pins.GPIO_PIN_D2, false); //waiting till connect... if (!Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].IsDhcpEnabled) { // using static IP while (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) { ; // wait for network connectivity } } else { // using DHCP while (IPAddress.GetDefaultLocalAddress() == IPAddress.Any) { ; // wait for DHCP-allocated IP address } } //Debug print our IP address Debug.Print("Device IP: " + Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].IPAddress); // create mqtt client instance client = new MqttClient(IPAddress.Parse(MQTT_BROKER_ADDRESS)); string clientId = Guid.NewGuid().ToString(); client.Connect(clientId); SubscribeMessage(); PublishMessage("/robot/state", "ONLINE"); while (true) { long ticks = sensor.Ping(); if (ticks > 0L) { double inches = sensor.TicksToInches(ticks); Debug.Print("jarak ke object :" + inches); if (inches < 4) { jalan = Arah.Berhenti; Klakson.Write(true); WarningLed.Write(true); GoLed.Write(false); PublishMessage("/robot/status", "Watchout there is an object in the front of robot!"); } else { Klakson.Write(false); WarningLed.Write(false); GoLed.Write(true); } } //stop first before change direction or turn if (lastDirection != jalan) { //stop before start new direction MotorDriver.SetState(HBridge.Motors.Motor1, 0); MotorDriver.SetState(HBridge.Motors.Motor2, 0); } switch (jalan) { case Arah.Maju: MotorDriver.SetState(HBridge.Motors.Motor1, Speed); MotorDriver.SetState(HBridge.Motors.Motor2, Speed); break; case Arah.Mundur: MotorDriver.SetState(HBridge.Motors.Motor1, (sbyte)(-Speed)); MotorDriver.SetState(HBridge.Motors.Motor2, (sbyte)(-Speed)); break; case Arah.Kiri: MotorDriver.SetState(HBridge.Motors.Motor1, -50); MotorDriver.SetState(HBridge.Motors.Motor2, 50); break; case Arah.Kanan: MotorDriver.SetState(HBridge.Motors.Motor1, 50); MotorDriver.SetState(HBridge.Motors.Motor2, -50); break; case Arah.Berhenti: MotorDriver.SetState(HBridge.Motors.Motor1, 0); MotorDriver.SetState(HBridge.Motors.Motor2, 0); break; } lastDirection = jalan; } }
public static void Main() { //distance sensor //triger (12), echo (13), 5V + G HC_SR04 sensor = new HC_SR04(Pins.GPIO_PIN_D12, Pins.GPIO_PIN_D13); //buzzer OutputPort Klakson = new OutputPort(Pins.GPIO_PIN_D0, false); //motor driver HBridge MotorDriver = new HBridge(SecretLabs.NETMF.Hardware.Netduino.PWMChannels.PWM_PIN_D5, Pins.GPIO_PIN_D4, SecretLabs.NETMF.Hardware.Netduino.PWMChannels.PWM_PIN_D6, Pins.GPIO_PIN_D7); //led indicator if there is an object in the front of robot OutputPort WarningLed = new OutputPort(Pins.GPIO_PIN_D1, false); OutputPort GoLed = new OutputPort(Pins.GPIO_PIN_D2, false); //waiting till connect... if (!Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].IsDhcpEnabled) { // using static IP while (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) ; // wait for network connectivity } else { // using DHCP while (IPAddress.GetDefaultLocalAddress() == IPAddress.Any) ; // wait for DHCP-allocated IP address } //Debug print our IP address Debug.Print("Device IP: " + Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].IPAddress); // create mqtt client instance client = new MqttClient(IPAddress.Parse(MQTT_BROKER_ADDRESS)); string clientId = Guid.NewGuid().ToString(); client.Connect(clientId); SubscribeMessage(); PublishMessage("/robot/state", "ONLINE"); while (true) { long ticks = sensor.Ping(); if (ticks > 0L) { double inches = sensor.TicksToInches(ticks); Debug.Print("jarak ke object :" + inches); if (inches < 4) { jalan = Arah.Berhenti; Klakson.Write(true); WarningLed.Write(true); GoLed.Write(false); PublishMessage("/robot/status", "Watchout there is an object in the front of robot!"); } else { Klakson.Write(false); WarningLed.Write(false); GoLed.Write(true); } } //stop first before change direction or turn if (lastDirection != jalan) { //stop before start new direction MotorDriver.SetState(HBridge.Motors.Motor1, 0); MotorDriver.SetState(HBridge.Motors.Motor2, 0); } switch (jalan) { case Arah.Maju: MotorDriver.SetState(HBridge.Motors.Motor1, Speed); MotorDriver.SetState(HBridge.Motors.Motor2, Speed); break; case Arah.Mundur: MotorDriver.SetState(HBridge.Motors.Motor1,(sbyte)(-Speed)); MotorDriver.SetState(HBridge.Motors.Motor2, (sbyte)(-Speed)); break; case Arah.Kiri: MotorDriver.SetState(HBridge.Motors.Motor1, -50); MotorDriver.SetState(HBridge.Motors.Motor2, 50); break; case Arah.Kanan: MotorDriver.SetState(HBridge.Motors.Motor1, 50); MotorDriver.SetState(HBridge.Motors.Motor2, -50); break; case Arah.Berhenti: MotorDriver.SetState(HBridge.Motors.Motor1, 0); MotorDriver.SetState(HBridge.Motors.Motor2, 0); break; } lastDirection = jalan; } }