castlerocks
New Member
Dear all,
Can you help me translate the code below to picaxe 14M2? This is to run a robot through HM-something BLE module with a LOFI Robot control app for Android and iPhone. While I got it to connect and receive serial data from the app on picaxe in minutes with SERIN command, there does not seem to be any information on actual protocol and I have trouble figuring it out with trial and error. I'm failing to follow the logic of the code to work it out from it as well, but it should be possible - it is just me and C code. This is to run a differential drive robot with two motors and a few auxiliary functions. I'm specifically interested in the part that would allow me to decode the data from the app.
Thank you for your inputs,
Edmunds
Can you help me translate the code below to picaxe 14M2? This is to run a robot through HM-something BLE module with a LOFI Robot control app for Android and iPhone. While I got it to connect and receive serial data from the app on picaxe in minutes with SERIN command, there does not seem to be any information on actual protocol and I have trouble figuring it out with trial and error. I'm failing to follow the logic of the code to work it out from it as well, but it should be possible - it is just me and C code. This is to run a differential drive robot with two motors and a few auxiliary functions. I'm specifically interested in the part that would allow me to decode the data from the app.
Thank you for your inputs,
Edmunds
Code:
int val = 0;
int horizontal = 50;
int vertical = 50;
int A_state = 0;
int B_state = 0;
char data[5];
int m1;
int m2;
void setup()
{
// silnik M1
pinMode(2, OUTPUT);
pinMode(4, OUTPUT);
pinMode(3, OUTPUT);
// silnik M2
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
pinMode(5, OUTPUT);
//buzzer
pinMode(16, OUTPUT);
// diody LED
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
Serial.begin(9600); // komunikacja Arduino->Komputer przez kabel USB
Serial1.begin(9600); // komunikacja Arduino->HM-10 bluetooth
motor1(1, 255);
motor2(1, 255);
}
void loop()
{
if (Serial1.available())
{
Serial1.readBytes(data, 5); // odbior danych z aplikacji
horizontal = int(data[1]); // joystick os pozioma
vertical = int(data[2]); // joystick os pionowa
A_state = int(data[3]); // lewy przycisk
B_state = int(data[4]); // prawy przycisk
horizontal = horizontal * 2 - 100;
vertical = vertical * 2 - 100;
Serial.print("horizontal = ");
Serial.print(horizontal);
Serial.print(" vertical = ");
Serial.print(vertical);
Serial.print(" A = ");
Serial.print(A_state);
Serial.print(" B = ");
Serial.println(B_state);
}
digitalWrite(16, A_state);
digitalWrite(10, B_state);
digitalWrite(9, B_state);
m1 = vertical - horizontal;
m2 = vertical + horizontal;
int mm1 = min(255, 2.7 * abs(vertical - horizontal));
int mm2 = min(255, 2.7 * abs(vertical + horizontal));
if (m1 > 0)
motor1(true, mm1);
else
motor1(false, mm1);
if (m2 > 0)
motor2(true, mm2);
else
motor2(false, mm2);
}
void motor1(boolean direction, int power)
{
digitalWrite(2, direction);
digitalWrite(4, !direction);
analogWrite(3, power);
}
// funkcja sterująca silnikiem podłączonym do gniazda M2
void motor2(boolean direction, int power)
{
digitalWrite(7, direction);
digitalWrite(8, !direction);
analogWrite(5, power);
}