Hello, I'm making single axis robots which have Wi-fi shield with Mega and Uno.
I designed my robot as follows:
-
Using Mega board with wifi shield, and I2C master mode. The mega board gets magnetometer sensing data(from UART sensor) and posts data to server.
-
The mega board gets moving information data from server, and transmits to uno board with I2C communication.
-
The uno board gets moving information data, and controls the step motor driver.
I think that the mega board sends a signal to the uno board and works independently with posting data to server, when the uno board receives a signal and operates the For loop.
Although the motor control(using the for loop) performed in the uno board, the robot doesn't posting sensing data to server when he is moving with motor.
Is there any other issue that I don't know about I2C communication?
Here is my code. Sorry for some deleted code.
Mega board ::
void loop(){
if(millis() - check_time > 500){
check_time = millis();
TX_loop(); // get sensor data and post data to server
}
else
RX_loop(); // send moving data to uno
}
void TX_loop(){// Posting protocol to server
digitalWrite(49,HIGH);
getSensorData(); // sensing data function
// Creating Magnetometer data
s_val = '{' + jsonform("x", String(s_data[3])) + ',';
s_val += jsonform("y", String(s_data[4])) + ',';
s_val += jsonform("z", String(s_data[5])) + '}';
s_tmp = s_val.length();
s_len = (int)s_tmp.toInt();
s_tmp.toCharArray(s_msg,s_len+1); //s_msg :: char array , s_val :: String class
s_msg[s_len] = '\0';
if(client.connected()){
client.write(s_msg);
Serial.write(s_msg);
tag++;
}
else{
Serial.println("client error");
}
}
void RX_loop(){// Getting protocol from server
if(client.available()) {
c_tmp = client.readStringUntil('\n');
c_tmp.trim();
c_tmp = c_tmp.substring(c_tmp.indexOf('{'), c_tmp.lastIndexOf('}') + 1);
c_tmp2 = c_tmp.length();
c_len = (int)c_tmp2.toInt();
c_tmp.toCharArray(c_msg, c_len+1);
c_msg[c_len]='\0';
}
Serial.write(c_msg);
Wire.beginTransmission(4); //transmit to device #4
Wire.write(c_msg,c_len+1);
Wire.write('\n');
Wire.endTransmission();
}
Uno board ::
void setup(){
Wire.begin(4); // Address : #4
Wire.onReceive(requestEvent);
}
void loop(){}
void requestEvent(int num){
digitalWrite(13,LOW);
String val = "";
String _len = "";
int len = 0;
char json[200];
if(Wire.available()){
val = Wire.readStringUntil('\n');
val.trim();
Serial.println(val);
_len = val.length();
len = (int)_len.toInt();
val.toCharArray(json,len+1);
json[len]='\0';
}
parser(json);
}
void parser(char* json) { // Receiving data
StaticJsonBuffer<200> RXBuffer;
JsonObject& RXdata = RXBuffer.parseObject(json);
if (!RXdata.success()) {
digitalWrite(13,HIGH);
} else {
digitalWrite(13,LOW);
robot._angle = RXdata["angle"];
robot._meter = RXdata["meter"];
robot._speed = RXdata["speed"];
robot._startsign = RXdata["startsign"];
motor_control(robot);
}
}
int motor_control(motor_signal motor) {
pulse = (int) (motor._meter * (int)pow(2,stepmode) * 1630);
pulsefunc(pulse, pwm)
}
void pulsefunc(int pulse, int pwm) {
for (int i = 0; i < pulse; i++) {
digitalWrite(L_clk, HIGH);
digitalWrite(R_clk, HIGH);
delay(pwm);
digitalWrite(L_clk, LOW);
digitalWrite(R_clk, LOW);
delay(pwm);
}
}