- Configure your sensor as an I2C Device and name it “irl”
Part 2: Testing the Sensor
- You will need to build TWO programs to make this work… one is a class to handle setting up the sensor and the other is the actual OpMode. First, the class…
- Add a new class and call it MRIIrLocator. Don’t use an example file. Once you have added the class, delete everything inside it and copy/paste the code below.
/*
DO NOT EDIT THIS PROGRAM
Modern Robotics IR Locator 360 Driver
Created 7/27/2017 by Colton Mehlhoff of Modern Robotics using FTC SDK 3.10 Reuse permitted with credit where credit is due
This class provides functions to use the IR Locator 360 http://modernroboticsinc.com/ir-locator-360 Support is available by emailing support@modernroboticsinc.com
*/
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.hardware.HardwareMap;
import com.qualcomm.robotcore.hardware.I2cAddr;
import com.qualcomm.robotcore.hardware.I2cDevice;
import com.qualcomm.robotcore.hardware.I2cDeviceSynch;
import com.qualcomm.robotcore.hardware.I2cDeviceSynchImpl;
public class MRIIrLocator {
private byte[] chache;
private I2cDevice sensor;
private I2cDeviceSynch reader;
HardwareMap hwMap = null;
public MRIIrLocator() {
}
public void init(HardwareMap ahwMap, String cfgName) {
init(ahwMap, cfgName, 0x1C); //Default I2C address for color beacon is 0x4c }
public void init(HardwareMap ahwMap, String cfgName, int i2cAddr8) { hwMap = ahwMap;
sensor = hwMap.i2cDevice.get(cfgName);
reader = new I2cDeviceSynchImpl(sensor, I2cAddr.create8bit(i2cAddr8), false);
reader.engage();
}
byte[] readAll(){
chache = reader.read(0x00, 8);
return chache;
}
int heading1200hz(){
return (reader.read8(0x04)&0xFF) * 5;
}
int strength1200hz(){
return reader.read8(0x05) & 0xFF;
}
int heading600hz(){
return (reader.read8(0x06) & 0xFF) * 5;
}
int strength600hz(){
return reader.read8(0x07) & 0xFF;
}
}
- The next program that will need to be added should be called Locator_Test. Following a similar procedure as above, copy and paste the code below into your new OpMode.
/*
Modern Robotics IR Locator 360 Example
Created 7/27/2017 by Colton Mehlhoff of Modern Robotics using FTC SDK 3.10 Reuse permitted with credit where credit is due
Configuration:
I2CDevice "irl" (MRI IR Locator 360 with default I2C address 0x1C)
MRIIrLocator class must be in the same folder as this program. Download from http://modernroboticsinc.com/ir-locator-360
To change I2C Addresses, go to http://modernroboticsedu.com/mod/lesson/view.php?id=96 Support is available by emailing support@modernroboticsinc.com
*/
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
@Autonomous (name = "IR Locator Test", group = "MRI")
public class Locator_Test extends LinearOpMode {
MRIIrLocator locator = new MRIIrLocator();
@Override
public void runOpMode() throws InterruptedException {
telemetry.addData("Status", "Initialized");
telemetry.update();
locator.init(hardwareMap, "irl"); //initializes the I2CDevice. Second parameter is the name of the sensor in the configuration file.
waitForStart();
while (opModeIsActive()) {
telemetry.addData("Dir 1200", locator.heading1200hz());
telemetry.addData("Dir 600", locator.heading600hz());
telemetry.addData("Str 1200", locator.strength1200hz());
telemetry.addData("Str 600", locator.strength600hz());
telemetry.update();
}
}
}
Now build everything and run your Locator_Test OpMode. Take some time to understand how your sensor responds to your beacon.
Part 3: Navigating to the Sensor
You should now begin to add your motors to the Locator_Test file right inside your main class area…
Next, complete hardware mapping to tell your robot controller where to find your motors…(feel free to copy/paste this in from other files!)
Your main loop should look like this:
Now run the OpMode with a beacon placed on the floor somewhere in the room. Your robot should be able to find the beacon even if you start it facing the other direction.