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 track robot position?

2  Asked on January 6, 2022 by michael-coss

   

What PID values should I keep?

3  Asked on January 6, 2022 by sulaiman-ayub

     

Why ODE for optimal control theory?

2  Asked on January 6, 2022 by bicepjai

   

How to set the output in a differential amplifier?

1  Asked on January 6, 2022 by siddiqui_902003

 

Building a stationary robot. ROS the right tool for me?

4  Asked on January 4, 2022 by frimann-bjornsson

 

Ask a Question

Get help from others!

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