MindSqualls
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Using I2C sensors with Mindsqualls

2 posters

Go down

Using I2C sensors with Mindsqualls Empty Using I2C sensors with Mindsqualls

Post  Ahelios Sun Jul 31, 2011 4:06 am

Hi,
I am trying to convert code that uses the Aforge.net library to control a stepper motor using the PCF8574 chip. The code I wrote sends commands to sensor 1 in the NxT brick, using the LsWrite function. I send a sucession of &H1, &H2, &H4 and &H8 to address &H40 (hardwired in the PCF8574 chip). In turn, the chip generates digital 0s and 1s in four outputs that are used to drive a stepper motor. All is working fine, but I have some trouble when trying to convert the code from Aforge to Mindsqualls. Here are the relevant lines to setup the sensor and send out commands to make the motor to run one turn:

Dim NxtBrick As New NXTBrick ´defines the NxtBrick object
Dim instrucao() As Byte = New Byte(5) {} ´defines a byte array
NxtBrick.SetSensorMode(Robotics.Lego.NXTBrick.Sensor.First, Robotics.Lego.NXTBrick.SensorType.Lowspeed,
Robotics.Lego.NXTBrick.SensorMode.Raw) ´Setup sensor port 1 as a digital port to use I2C chip
instrucao(0) = CByte(&H40) ´address to access PCF8574 chip connected to sensor port 1.
For n = 1 To 50
instrucao(5) = CByte(&H1) ´sends byte &H1 to chip
NxtBrick.LsWrite(Robotics.Lego.NXTBrick.Sensor.First, instrucao, 0) ´sends byte &H1 to chip
instrucao(5) = CByte(&H2)
NxtBrick.LsWrite(Robotics.Lego.NXTBrick.Sensor.First, instrucao, 0) ´sends byte &H2 to chip
instrucao(5) = CByte(&H4)
NxtBrick.LsWrite(Robotics.Lego.NXTBrick.Sensor.First, instrucao, 0) ´sends byte &H4 to chip
instrucao(5) = CByte(&H8)
NxtBrick.LsWrite(Robotics.Lego.NXTBrick.Sensor.First, instrucao, 0) ´sends byte &H8 to chip
Next

I need help to convert the following lines to Mindsqualls:
NxtBrick.SetSensorMode(Robotics.Lego.NXTBrick.Sensor.First, Robotics.Lego.NXTBrick.SensorType.Lowspeed,
Robotics.Lego.NXTBrick.SensorMode.Raw)
NxtBrick.LsWrite(Robotics.Lego.NXTBrick.Sensor.First, instrucao, 0)

I will appreciate any help.
Thanks a lot,
Marcelo
Thanks,
Marcelo

Ahelios

Posts : 5
Join date : 2011-07-27

Back to top Go down

Using I2C sensors with Mindsqualls Empty Re: Using I2C sensors with Mindsqualls

Post  Niels Tue Aug 02, 2011 5:47 am

Translated directly it becomes:

Code:
        Dim brick As New NxtBrick(NxtCommLinkType.USB, 0)  ' Defines the NxtBrick object

        Dim instrucao() As Byte = New Byte(5) {}  ' Defines a byte array

        brick.CommLink.SetInputMode(NxtSensorPort.Port1, NxtSensorType.LOWSPEED, NxtSensorMode.RAWMODE)  ' Setup sensor port 1 as a digital port to use I2C chip

        instrucao(0) = CByte(&H40) ' Address to access PCF8574 chip connected to sensor port 1.
        For n = 1 To 50
            instrucao(5) = CByte(&H1)  ' Sends byte &H1 to chip
            brick.CommLink.LsWrite(NxtSensorPort.Port1, 0, instrucao)  ' Sends byte &H1 to chip
            instrucao(5) = CByte(&H2)
            brick.CommLink.LsWrite(NxtSensorPort.Port1, 0, instrucao)  ' Sends byte &H2 to chip
            instrucao(5) = CByte(&H4)
            brick.CommLink.LsWrite(NxtSensorPort.Port1, 0, instrucao)  ' Sends byte &H4 to chip
            instrucao(5) = CByte(&H8)
            brick.CommLink.LsWrite(NxtSensorPort.Port1, 0, instrucao)  ' Sends byte &H8 to chip
        Next

Niels

Posts : 19
Join date : 2011-07-15

Back to top Go down

Using I2C sensors with Mindsqualls Empty Re: Using I2C sensors with Mindsqualls

Post  Ahelios Wed Aug 03, 2011 2:55 am

Thanks Niels,
Your code worked! Interestingly, the transmission speed with the usb port is much lower than bluetooth in my computer. When I run the stepper motor code using usb, I only get one step every two seconds. (i.e. one LsWrite message). With Bluetooth it is much faster and the steps get out of synch so I used Threading.Thread.Sleep(100) between each LsWrite message. I have two questions:
- Should the usb transmission be slower than BT ?
- With bluetooth I tried to use the comand LsGetStatus to wait for the commlink to be free but I got an exception error:

instrucao(5) = CByte(&H1) ' Sends byte &H1 to chip
brick.CommLink.LsWrite(NxtSensorPort.Port1, 0, instrucao) ' Sends byte &H1 to chip
While Not brick.CommLink.LsGetStatus(NxtSensorPort.Port1) = &H0 'this line triggers the exception
End While

Exception message: The Status byte indicates an error: Request: 0x00 0x0E 0x00; Reply: 0x02 0x0E 0x20 0x06

It will be great if you can shed some more light into this!
Thanks,
Marcelo

Ahelios

Posts : 5
Join date : 2011-07-27

Back to top Go down

Using I2C sensors with Mindsqualls Empty Re: Using I2C sensors with Mindsqualls

Post  Niels Wed Aug 03, 2011 5:45 am

As said, this was a direct translation. I would probably do it a bit different:

Either go the low-level way and do away with the Brick since we don’t really use it:

Code:
        Dim commLink As New NxtUsbConnection()  ' Define a usb-comlink

        Dim instrucao() As Byte = New Byte(5) {}  ' Defines a byte array

        commLink.SetInputMode(NxtSensorPort.Port1, NxtSensorType.LOWSPEED, NxtSensorMode.RAWMODE)  ' Setup sensor port 1 as a digital port to use I2C chip

        instrucao(0) = CByte(&H40) ' Address to access PCF8574 chip connected to sensor port 1.
        For n = 1 To 50
            instrucao(5) = CByte(&H1)  ' Sends byte &H1 to chip
            commLink.LsWrite(NxtSensorPort.Port1, 0, instrucao)  ' Sends byte &H1 to chip
            instrucao(5) = CByte(&H2)
            commLink.LsWrite(NxtSensorPort.Port1, 0, instrucao)  ' Sends byte &H2 to chip
            instrucao(5) = CByte(&H4)
            commLink.LsWrite(NxtSensorPort.Port1, 0, instrucao)  ' Sends byte &H4 to chip
            instrucao(5) = CByte(&H8)
            commLink.LsWrite(NxtSensorPort.Port1, 0, instrucao)  ' Sends byte &H8 to chip
        Next

Or go the high-level way and model the PCF8574-chip as a class that inherits from the NxtDigitalSensor class in much the same way as the NxtUltrasonicSensor-class does.

Ahelios wrote:
Interestingly, the transmission speed with the usb port is much lower than bluetooth in my computer. When I run the stepper motor code using usb, I only get one step every two seconds. (i.e. one LsWrite message). With Bluetooth it is much faster and the steps get out of synch so I used Threading.Thread.Sleep(100) between each LsWrite message. I have two questions:
- Should the usb transmission be slower than BT ?

No. USB is the fastest.

Perhaps you are simply sending the BT commands too quickly, meaning that not all of them actually manage to get executed before the next one interrupt them.

Ahelios wrote:
- With bluetooth I tried to use the comand LsGetStatus to wait for the commlink to be free but I got an exception error:

instrucao(5) = CByte(&H1) ' Sends byte &H1 to chip
brick.CommLink.LsWrite(NxtSensorPort.Port1, 0, instrucao) ' Sends byte &H1 to chip
While Not brick.CommLink.LsGetStatus(NxtSensorPort.Port1) = &H0 'this line triggers the exception
End While

Exception message: The Status byte indicates an error: Request: 0x00 0x0E 0x00; Reply: 0x02 0x0E 0x20 0x06

For some obscure reason LsGetStatus() tends to return an error the very first time it is called. MindSqualls deals with this by looking for an error and call LsGetStatus() once more is it encounters one.

MindSqualls, and Aforge it would seem, has chosen a design where errors are converted into an exception and that is the one you get.

In MindSqualls I handle the error like this:

Code:
                byte bytesReady;
                try
                {
                    bytesReady = commLink.LsGetStatus(sensorPort);
                }
                catch (NxtCommunicationProtocolException)
                {
                    bytesReady = commLink.LsGetStatus(sensorPort);
                }
                byte[] garbage = commLink.LsRead(sensorPort);

I.e. by catching the thrown exception and calling LsGetStatus again in the catch-block.

Niels

Posts : 19
Join date : 2011-07-15

Back to top Go down

Using I2C sensors with Mindsqualls Empty Re: Using I2C sensors with Mindsqualls

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum