Integrating with Electronics - SCHS-Robotics/Software-Wiki GitHub Wiki
Keeping Track of Modules (Modern Robotics)
Challenge: When you plug in and scan for modules on the Robot Controller phone, they show up in no particular order. THE ORDER DOES NOT MATCH THE USB PORT ORDER. (As of the 2016-2017 season)
If you have more than one motor controller or other module, how do you tell which is which?
Solution: LABEL YOUR MODULES WITH THEIR SERIAL NUMBER
- Plug in each module one at a time and look at the serial number
- Label it. The last 4 characters will probably suffice.
- It’s pretty easy and will pay off at the beginning of the season when electronics are constantly changing.
Changing the Configuration File
Challenge: In order to edit a configuration file on the phone, you have to deal with navigating through the file structure and typing on a phone keyboard.
Solution: Edit it on a computer.
- If you are on a Mac, download Android File Transfer.
- Plug in the phone to your computer and make sure the phone is set up to transfer media.
- Find the “FIRST” folder on the phone and find the xml file with the name of your configuration. Drag to somewhere on your computer. Drag back to the phone when done editing.
- You have the entire configuration file in front of you with super intuitive organization!
Using I2C ("eye squared see") Sensors
Challenge: OMFG WHY ARE THESE SENSORS RETURNING JUNK DATA?!?!?
You are in the right place if you are using more than one of the same sensor, and it plugs into the I2C port on the Device interface Module.
How I2C works: It communicates with each sensor via its I2C address. All sensors of the same type (Color, Range, etc.) have the same I2C address by default. If you have more than one, it can’t differentiate between them.
Solution Part 1: Change the I2C addresses of the sensors. If the sensors already are set with different I2C addresses, skip to Solution Part 2.
- Download Core Device Discovery.
- Swap out the mini to micro USB cable on the Core Power Module with a mini to regular USB cable. Plug it in to your computer.
- Turn the power on, open Core Device Discovery, and click “Refresh.”
- Find the Device Interface Module and click “Advanced.”
- Make sure only the sensor you want to change is plugged in. Find the “Address List” and click “Refresh List.”
- Under “Change Address,” enter the current address you see under “Address List” and enter a new address. The new address doesn’t matter too much. You may simply add 1 to the first digit (after the 0x), for example.
- Do this for any I2C sensors if you are using more than one. You can leave one unchanged, they just have to be different from each other.
- LABEL EACH SENSOR WITH ITS I2C ADDRESS.
Solution Part 2: Specify the I2C address of each sensor in the code (this is why labeling is important).
//These variables should be at the top of your class.
ColorSensor colorSensor1;
I2cAddr colorSensor1I2c = I2cAddr.create8bit(0x4c);// replace "0x4c" with the actual address
colorSensor1 = hardwareMap.colorSensor.get("colorSensor1");
colorSensor1.setI2cAddress(colorSensor1I2c);
Using Multiple Range Sensors
Challenge: As of the 2016-2017 season, there is a bug in the FTC code that prevents you from using more than one range sensor, which has to do with I2C addresses.
The easiest (and the buggy) way to use range sensors is the following:
ModernRoboticsI2cRangeSensor rangeSensor1;
rangeSensor1 = hardwareMap.get(ModernRoboticsI2cRangeSensor.class, "rangeSensor1");
//If not using default address:
I2cAddr rangeSensor1I2c = I2cAddr.create8bit(0x38);// replace "0x38" with the actual address
rangeSensor1.setI2cAddress(rangeSensor1I2c);
BUT for some reason, trying to use a non-default I2C address doesn’t actually work. The source of the bug seems to be one simple line in the ModernRoboticsI2cRangeSensor class.
Solution: Copy the ModernRoboticsI2cRangeSensor code into your own class and fix the bug.
-
In Android Studio, simply type “ModernRoboticsI2cRangeSensor” on a random new line in your code. Press Command/CTRL and click on the class name to open it in a new tab.
-
Command+A or CTRL+A to select the entire class.
-
Right click on your teamcode package and create a New Java Class. Name it “RangeSensor” or “MyRangeSensor” or something like that.
-
Delete everything in the newly-generated class except for the package declaration.
-
Paste the copied text below the package declaration. Then delete the package declaration that was copied in (the one that ends in “modernrobotics”).
-
There will be a couple places where the name “ModernRoboticsI2cRangeSensor” is used. Change them to the new class name.
-
Finally, navigate to the constructor and comment out the line that sets the default I2c address:
public RangeSensor(I2cDeviceSynch deviceClient) { super(deviceClient, true); this.setOptimalReadWindow(); // this.deviceClient.setI2cAddress(ADDRESS_I2C_DEFAULT); - Comment this line this.deviceClient.engage(); super.registerArmingStateCallback(); }
-
Almost done! Now here’s how to set up your range sensor with the custom class:
//These variables should be at the top of your class. RangeSensor rangeSensor1; I2cAddr rangeSensor1I2c = I2cAddr.create8bit(0x38);// replace "0x38" with the actual address. If you haven't changed the address, use 0x28. I2cDevice rangeDevice1 = hardwareMap.i2cDevice.get("rangeSensor1"); I2cDeviceSynch rangeReader1 = new I2cDeviceSynchImpl(rangeDevice1, rangeSensor1I2c, false); rangeSensor1 = new RangeSensor(rangeReader1);
-
Now just use the RangeSensor object like normal. (i.e.
rangeSensor1.rawUltrasonic()
)