AnswerBun.com

Monobrick Communication Library-reading nxt sensors

Robotics Asked by jpcreeper13 on October 31, 2020

In my program, I’m need to detect if the NXT touch sensor is pressed.

var nxt = new Brick<Sensor, Sensor, Sensor, Sensor>("usb");
nxt.Connection.Open();
nxt.Sensor1 = new TouchSensor();
nxt.Sensor1.Reset(false);
nxt.Sensor1.Initialize();
Console.WriteLine(nxt.Sensor1);

When I start the program, the sensor value always reads 0. But I discovered that if I go into the “View” menu in the NXT and see the touch sensor value, the program value reads 1. I can’t do that for my setup. Also, I can’t use Bluetooth; my computer doesn’t have it. Can someone help me?

EDIT: my full code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MonoBrick.NXT;
using MonoBrick;
using System.Windows.Forms;
using System.Reflection;
using System.Threading;

namespace MonoBrick
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            try
            {
                var nxt = new Brick<Sensor, Sensor, Sensor, Sensor>("usb");
                nxt.Connection.Open();
                nxt.Sensor1 = new TouchSensor();
                nxt.Sensor1.Reset(false);
                nxt.Sensor1.Initialize();
                Console.WriteLine(nxt.Sensor1.ReadAsString());
                nxt.Beep(500);
                System.Windows.Application application = new System.Windows.Application();
                application.Run(new Window1());
                nxt.Connection.Close();
            }

            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.Message);
                Console.WriteLine("Press any key to end...");
                Console.ReadKey();
            }
        }
    }
}

2 Answers

For some reason MonoBrick is not able to force the NXT to configure the sensor, or it should be done in some other way, which I do not know of.

However, your program works, if you go to the "View" menu on NXT brick and select Touch sensor on Port1. NXT will be showing the value from the sensor, and your program as well.

Likewise, if you write a program for NXT in NXT-G for instance that will use a sensor read block to read value from touch sensor on Port1 first (and then do whatever else...), then your program will also read the value of the sensor, while the NXT program keeps running.

At least this is the way it worked for me.

Answered by Palo on October 31, 2020

You need to read the value continually in order to get the updated value, see the tutorial on there website.

Here is what the code to do that might look like (untested as I do not have a NXT):

using System;
using MonoBrick.EV3;
public static class Program{
    static void Main(string[] args)
    {
        var nxt = new Brick<Sensor, Sensor, Sensor, Sensor>("usb");
        nxt.Connection.Open();
        nxt.Sensor1 = new TouchSensor();
        nxt.Sensor1.Reset(false);
        nxt.Sensor1.Initialize();
        ConsoleKeyInfo cki;  
        Console.WriteLine("Press Q to quit");  
        do   
        {  
            cki = Console.ReadKey(true); //press a key  
            Console.WriteLine(nxt.Sensor1);                       
            }  
        } while (cki.Key != ConsoleKey.Q);
        ev3.Connection.Close();
    }
}

Answered by Mark Omo on October 31, 2020

Add your own answers!

Related Questions

How to calculate vehicle detection distance?

1  Asked on December 17, 2021 by assam-alzookery

         

How does a clustered particle filter work?

1  Asked on December 17, 2021 by s-e-k

 

How does 3D Lidar work?

1  Asked on December 17, 2021 by mjd-kassem

   

How to send commands to iRobot Create 2?

1  Asked on December 17, 2021 by mauricio-moreno

 

KUKA Robotics API IDE

3  Asked on December 4, 2021 by nikita-semenov

     

ROS sensor implementation

1  Asked on November 28, 2021

       

State propagation from uncertain control input

1  Asked on October 3, 2021 by astghik-hakobyan

     

output velocity equation of compliance control

0  Asked on October 3, 2021 by rrrruo

   

Bearing landmark localization

1  Asked on October 3, 2021 by r-go

   

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP