Hi noch mal,
habe nun den ersten Teil mal soweit zusammen.
Würde sich jemand mit mehr Sachverstand den bisherigen Code mal ansehen? Es ist im Prinzip das Beispiel 12 der M256. Ich habe in der Main.c nur die beiden Funktion "void behaviour_wifiControl(void)" verändert und eine Funktion "int cmdsplit(void)" hinzugefügt.
Keine Fehler beim Kompilieren, nur die Warnung "RP6M256_12_WIFI_REMOTE_2.c:487: warning: 'x' may be used uninitialized in this function".
Meine Fragen:
Klappt das so??
und in der Funktion "int cmdsplit(void)" habe ich anstelle der "normalen" str_fill einfach eine for-Schleife gelegt. Klappt das so???
Und wenn ich den atoi-Aufruf mache, ist mein Array "tmp" mit einigen Ziffern besetzt, der Rest wurde vorher mit "x" aufgefüllt. Sieht also z.B. so aus (für 10 Einträge) : |#|3| : |2| : |5|0|*|x|x|, wenn der gesendete String dieser war: #3:2:50*
Wird das dann richtig übernommen? Das resultierende Array params sollte danach so aussehen: |3|2|50|.
Danke schon einmal! Möchte erst einen Sachkundigen Rat hören, bevor ich daran weiterbastle. Sonst wird das debugging später etwas schwer für mich.
In die untere Funktion kommt dann noch ein Funktionsaufruf für das Bearbeiten dieses gesplitteten Strings.
Hier der Code:
Code:
//This Funktion splits the incoming String. This String looks like: #1:30:50:0:13*.
// "#" = Begin of Command
// "*" = End of Command
// "1" is for "driving"
// "30" ist for left engine, "50" ist for right engine -> driving a curve
// "0" is for "driving forwards"
// "13" is just an increasing number, increased after every command
#define MAX_PARAMS 6
int params[MAX_PARAMS];
int cmdsplit(void)
{
int cursor;
cursor=0;
int param_nr;
param_nr =0;
// Parameterarray initialisieren
int i;
for (i=0;i<MAX_PARAMS;i++)
params[i] = -1;
/* Prüfen ob der command-string gültig beginnt */
if ((receiveBuffer_WIFI[cursor] != '#'))
return -1;
while (receiveBuffer_WIFI[cursor] != '\0' && receiveBuffer_WIFI[cursor] != '*') // prüfen ob das ende erreicht wurde
{
char tmp[10];
char x;
for (i=0;i<10;i++)
tmp[i] = x;
//Str_Fill(tmp,'x',10);//!!!!!!!!!!!!!!!!!!!
cursor++;
int j;
j= 0;
while(receiveBuffer_WIFI[cursor] != ':' && receiveBuffer_WIFI[cursor] != '*')
{
tmp[j] = receiveBuffer_WIFI[cursor];
cursor++;
j++;
}
params[param_nr] = atoi(tmp); // wert auslesen
if (param_nr >= MAX_PARAMS) // sind mehr parameter verfügbar als erwartet? Stimmt was mit dem Commando nicht...
return -1;
param_nr++;
// Überspingen der ausgelesen Zeichen und ermitteln des nächsten "interessanten" Teils im string.
while((receiveBuffer_WIFI[cursor] != ':') && (receiveBuffer_WIFI[cursor] != '*') && (receiveBuffer_WIFI[cursor] != '\0'))
cursor++;
}
return param_nr;
}
/**
* This behaviour allows to remotely control the Robot with Text commands that
* are sent over a WIFI connection (with a small modification also via standard serial interface).
*
* It basically receives a text line (with \r or \n or both at the end) and checks if
* it is a command or a number. If it is a number it sets the speed of the motors.
*
* This behaviour first waits to be activated, then it surpresses all other behaviours
* (except for the low battery behaviour).
*
*/
void behaviour_wifiControl(void)
{
static uint8_t speedl = 0;
static uint8_t speedr = 0;
switch(wifiControl.state)
{
case IDLE:
if(getInputLine_WIFI())
{
if(strcmp(receiveBuffer_WIFI,"cmd")==0 || strcmp(receiveBuffer_WIFI,"command")==0)
{
writeString_P_WIFI("\nWIFI COMMAND MODE ACTIVE\n");
wifiControl.state = WIFI_ACTIVE;
break;
}
else if(!behaviour_command_interpreter()) // Call behaviour control interpreter code
writeString_P_WIFI("\nYou must enter cmd or command to enable command mode!\n");
}
break;
case WIFI_ACTIVE:
if(getInputLine_WIFI())
{
if(strcmp(receiveBuffer_WIFI,"z")==0 || strcmp(receiveBuffer_WIFI,"quit")==0)
{
writeString_P_WIFI("\nWIFI COMMAND MODE EXIT!\n");
wifiControl.state = IDLE;
}
else if( strcmp(receiveBuffer_WIFI,"fwd")==0 || strcmp(receiveBuffer_WIFI,"f")==0 || strcmp(receiveBuffer_WIFI,"w")==0)
{
wifiControl.speed_left = speedl;
wifiControl.speed_right = speedr;
writeString_P_WIFI("\nFWD!\n");
wifiControl.dir = FWD;
}
else if(strcmp(receiveBuffer_WIFI,"bwd")==0 || strcmp(receiveBuffer_WIFI,"b")==0 || strcmp(receiveBuffer_WIFI,"s")==0)
{
wifiControl.speed_left = speedl;
wifiControl.speed_right = speedr;
writeString_P_WIFI("\nBWD!\n");
wifiControl.dir = BWD;
}
else if(strcmp(receiveBuffer_WIFI,"left")==0 || strcmp(receiveBuffer_WIFI,"l")==0 || strcmp(receiveBuffer_WIFI,"a")==0)
{
if(speedl/2 >= 30)
{
wifiControl.speed_left = speedl / 2;
wifiControl.speed_right = speedr / 2;
}
else if(speedl > 10)
{
wifiControl.speed_left = 30;
wifiControl.speed_right = 30;
}
writeString_P_WIFI("\nLEFT!\n");
wifiControl.dir = LEFT;
}
else if(strcmp(receiveBuffer_WIFI,"right")==0 || strcmp(receiveBuffer_WIFI,"r")==0 || strcmp(receiveBuffer_WIFI,"d")==0)
{
if(speedl/2 >= 30)
{
wifiControl.speed_left = speedl / 2;
wifiControl.speed_right = speedr / 2;
}
else if(speedl > 10)
{
wifiControl.speed_left = 30;
wifiControl.speed_right = 30;
}
writeString_P_WIFI("\nRIGHT!\n");
wifiControl.dir = RIGHT;
}
else if(strcmp(receiveBuffer_WIFI,"fl")==0 || strcmp(receiveBuffer_WIFI,"q")==0)
{
if(speedl/2 >= 30)
{
wifiControl.speed_left = speedl / 2;
wifiControl.speed_right = speedr;
}
else if(speedl > 10)
{
wifiControl.speed_left = 20;
wifiControl.speed_right = 40;
}
writeString_P_WIFI("\nCURVE FWD LEFT!\n");
wifiControl.dir = FWD;
}
else if(strcmp(receiveBuffer_WIFI,"fr")==0 || strcmp(receiveBuffer_WIFI,"e")==0)
{
if(speedr/2 >= 30)
{
wifiControl.speed_left = speedl;
wifiControl.speed_right = speedr / 2;
}
else if(speedr > 10)
{
wifiControl.speed_left = 40;
wifiControl.speed_right = 20;
}
writeString_P_WIFI("\nCURVE FWD LEFT!\n");
wifiControl.dir = FWD;
}
else if(strcmp(receiveBuffer_WIFI,"stp")==0 || strcmp(receiveBuffer_WIFI,"x")==0)
{
writeString_P_WIFI("\nSTOP!\n");
wifiControl.dir = FWD;
wifiControl.speed_left = 0;
wifiControl.speed_right = 0;
}
else if(!behaviour_command_interpreter()) // Call behaviour control interpreter code
{
cmdsplit();
/*uint8_t pwm_tmp = atoi(receiveBuffer_WIFI);
if(pwm_tmp == 0)
{
uint8_t not_a_number = 0;
int8_t i;
for(i = strlen(receiveBuffer_WIFI)-1; i >= 0 ; i--)
if((receiveBuffer_WIFI[i] < '0') || (receiveBuffer_WIFI[i] > '9'))
{
not_a_number = 1;
writeString_P_WIFI("\n### NOT A NUMBER! ###\n");
}
if(not_a_number) {
pwm_tmp = speedl;
break;
}
}
else if(pwm_tmp > 160){pwm_tmp = 160; writeString_P("\n--> Power limited to 160!");}
else if(pwm_tmp < 0) {pwm_tmp = 0;}
writeString_P_WIFI("\n--> Change speed to:");
writeInteger_WIFI(pwm_tmp,DEC);
writeChar_WIFI('\n');
wifiControl.speed_left = pwm_tmp;
wifiControl.speed_right = pwm_tmp;
speedl = pwm_tmp;
speedr = pwm_tmp;*/
}
}
break;
case WIFI_EXECUTE_CMD:
wifiControl.state = WIFI_ACTIVE;
break;
default:
wifiControl.state = IDLE;
break;
}
}
EDIT: Nun, was ich möchte, ist eine Remote wie die von FabianE., gepaart mit den Cruise- und Avoid-Funktionen des Beispiels 12. Nur zur Info
Lesezeichen