Hi all,
I am running Arducopter 4.5.5 on my Cube Orange Plus and I am trying to fetch some data from an Arduino via I2C using Lua Script.
I read this awesome blog here on this topic and tried to make my own piece of code: Unsupported Sensors? Try Arduino + Lua!
So I hooked up the wires from the I2C2 port of the Cube Orange to my Arduino Pro Mini like this:
> Pixhawk I2C2 SCL <==> Pro Mini SCL (A5)
> Pixhawk I2C2 SDA <==> Pro Mini SDA (A4)
> Pixhawk I2C2 GND <==> Pro Mini GND
Both the Pixhawk and Arduino are powered via different USB ports from my laptop.
So I made a simple Arduino code to see what kind of data comes from Lua I2C function calls, and it looks like this:
#include <Wire.h>
#define slave_address 0x24
void setup(){
Serial.begin(115200);
Wire.setClock(400000);
Wire.begin(slave_address);
Wire.onReceive(receive_data_func);
Wire.onRequest(request_data_func);
Serial.println("START");
Serial.println("================\n");
}
void loop(){
delay(1);
}
void receive_data_func(int howMany){
Serial.print("receive_data_func called with howMany = "); Serial.println(howMany);
while(Wire.available()){
Serial.print(" => Data Received: 0x");
Serial.println(Wire.read(), HEX);
}
Serial.print("\n===============\n\n");
}
void request_data_func(){
Serial.println("request_data_func called");
Serial.print("\n===============\n\n");
}
Then I made a simple Lua script to test i2c function calls. I used the safety switch as a condition, so the I2C functions get called only when I press the safety switch. The Lua script looks like this:
local i2c_bus_num = 0;
local slave_addr = 0x24;
local safety_switch_pressed = false;
local i2c_tried = false;
local arduino_i2c = i2c.get_device(i2c_bus_num, slave_addr);
arduino_i2c:set_retries(10);
function i2c_check_func()
safety_switch_pressed = not SRV_Channels:get_safety_state();
if(safety_switch_pressed == true and i2c_tried == false) then
gcs:send_text(6, " => Testing I2C");
arduino_i2c:write_register(0x10, 0x20);
i2c_tried = true;
end
if(safety_switch_pressed == false) then
i2c_tried = false;
end
return i2c_check_func, 100 -- Run at every 100ms i.e. 10Hz
end
gcs:send_text(6, " => Start")
return i2c_check_func()
With this setup, I am able to get the serial prints on my Arduino’s Serial Monitor when I press the Safety switch on my Pixhawk, like this:
My Questions are:
-
Since I am using the I2C2 port of the Cube Orange Plus, then why does the Lua script work with bus address 0? When I try with i2c bus address 1 (which is expected for I2C2), I do NOT receive any data on the Arduino’s serial monitor. Why is this? Isn’t I2C1 the expected bus 0?
-
The script only works if the Arduino starts after Pixhawk. If I reboot only the Pixhawk and try again (without making any changes to anything), then there is no data communication on the Arduino’s Serial Monitor. If I reset the Arduino after rebooting Pixhawk, then everything works normally. Why is this?
Please help.
@rmackay9 @tridge @Michael_Oborne @Yuri_Rage @iampete @ppoirier
Thanks,
Divyanshu