Canada
Asked — Edited
Resolved Resolved by Rich!

I2cread

Good day,

I2CRead is no recognise in the scrip, it's on the list at the left but the only command for I2C functionning is I2CWrite.


ARC Pro

Upgrade to ARC Pro

Become a Synthiam ARC Pro subscriber to unleash the power of easy and powerful robot programming

United Kingdom
#9  

What is it you are trying to do? What device is it for? Do you have the datasheet?

#10  

Hell Rich, it's a compass, GY-26, the data sheet are easy to find, no trouble with the wire lenght too. I now anderstand a lot more about it because of you. I still have a big amount of work to do to anderstand the data I receive and principally to format the received data but I am progressing bit by bit. The I2C is easier than expected, probably because of the EZ two instructions. I know now I had to make a write and just after a read, now i have to look in the EZ instruction set to determine how to format the data. In the cloud I had posted a very little example of my progress. Thank you again Rich, you have opened my eyes.

United Kingdom
#11  

I see.

In that case, let's have a look at the code that's needed (Note: I do not have a GY-26 to test any of this with but basing it on the datasheets available, please post if anything is confusing or doesn't work and I'll do my best to help).

First I assume it would be good practice to calibrate the compass module, you may skip this if you don't think it needs calibrating. It is assumed the robot is mobile and you use a Movement Panel to control it. It is also assumed it takes 1000ms for one single rotation of the robot - adjust the variable $rotationtime to suit your robot design. It is also assumed you have not changed the default I2C address.

Add a new EZ-Script control or new script to the script manager and call it Calibrate Compass. Copy and paste the code below into the script and run to calibrate (or add as part of an init script with a ControlCommand)


# Calibrate GY-26 I2C Compass
# Default address 0xE0

# Variables
$rotationtime = 1000
$rotations = 2

# Do not alter calibraterotatetime variable
$calibraterotatetime = $rotationtime * $rotations

# Set the compass to calibrate
I2CWrite(0, 0xE0, 0xC0)

# Rotate the robot twice
Right(255, $calibraterotatetime)

# Set the compass to end calibration
I2CWrite(0, 0xE0, 0xC1)

# Compass now calibrated

Now it's calibrated you want to know the robot's direction so again, the same assumptions as above and add a new EZ-Script control or script to the script manager called Compass Read. Copy and paste the code below.


# Read From GY-26 I2C Compass
# Default address 0xE0

# Read the current angle
$Raw = I2CRead(0, auto, 0xE0, 0x31)

# Split the array of bytes and assign to each variable
$anglehundreths = GetByte($Raw[2])
$angletens = GetByte($Raw[3])
$anglebits = GetByte($Raw[4])
$angledecimalpoint = GetByte($Raw[5])
$angledecimal = GetByte($Raw[6])

# Combine results to make up angle
$currentangle = $anglehundreths$angletens$anglebits$angledecimalpoint$angledecimal

Print($currentangle)
# Done

The current angle should be stored in the variable $currentangle for use anywhere else in the project.

You can have this on a loop with a slight delay to avoid saturation of the comms, if that's what you need just add in a label at the top


:loop

and a sleep and goto at the end


Sleep(200)
Goto(loop)

Hopefully that will work but without the device I can't test it and it is based upon my interpretation of the datasheet. However there should be enough there for you to get cracking with and again, if not just ask and I'll delve in deeper.

United Kingdom
#12  

And to add, the format of the received data, according to the datasheet is this;

Byte0 is ASCII enter Byte1 is ASCII new line Byte2 is ASCII angle hundreths Byte3 is ASCII angle tens Byte4 is ASCII angle bits Byte5 is ASCII decimal Byte6 is ASCII angle decimals Byte7 is ASCII Calibration Sum

I kinda missed the explanation above however did show the script stripping the bytes individually and then putting them back together (which is something I've never done before so please say if it works or not).

It also can read the temperature from the looks of it, have a look at the datasheet and (if the above works for the angle) have a look to see if you can figure out how to read that:) If you can't and you need help feel free to ask.

#13  

You are incredible Rich, it's not working but it's because you do not have a unit. To be able to read I need to put the adress of the unit on the bus before:

Read From GY-26 I2C Compass

Default address 0xE0

Read the current angle

I2CWrite(0, 0xE0, 0x00, 0x31) $Raw = I2CRead(0, auto, 0xE0, 8)

Split the array of bytes and assign to each variable

$anglehundreths = GetByte($Raw[2]) $angletens = GetByte($Raw[3]) $anglebits = GetByte($Raw[4]) $angledecimalpoint = GetByte($Raw[5]) $angledecimal = GetByte($Raw[6])

Combine results to make up angle

$currentangle = $anglehundreths$angletens$anglebits$angledecimalpoint$angledecimal

Print($currentangle)

Done

With this I have a result but it look like I need to make a better installation, too much interference to have a constant reading.

Thank you very much Rich, i now anderstand much more the formating of the data for this unit and in script.

DJ Sure has make a very good scriting tool and you anderstand it very well. I keep looking at all your post because you give hints and very usefull advice,