simplified code to one example

This commit is contained in:
Jens Noack 2021-07-12 09:32:26 +02:00
parent a964d96ad4
commit 5e4dc1040d

View file

@ -1,16 +1,17 @@
// Example Code of a CodeRacer sketch with implementation of a webserver
#include "CodeRacer_MKII.h"
#include "Webserver.h"
//#include "Webserver.h"
CodeRacerMKII Coderacer; // Inizialization of your CodeRacer
AsyncWebServer server(80); // IMPORTANT definition of the asynchronous Web server, including port number
//AsyncWebServer server(80); // IMPORTANT definition of the asynchronous Web server, including port number
int Distance;
int DemoMode=0;
int maximum= 0;
int Array[160];
int Degrees[160];
int location= 0; // Some numbers we need for later...
Codeserver Test((char*)"coderacer_ap", (char*)"007coderacer"); // Creation of the webserver. enter your network's SSID and password here
//Codeserver Test((char*)"coderacer_ap", (char*)"007coderacer"); // Creation of the webserver. enter your network's SSID and password here
void setup()
@ -26,9 +27,9 @@ void setup()
Serial.begin(115200);
Test.Run(); // Calls the Run() routine, which manages everything the webserver needs to work
Coderacer.wifi_printf("Activate a switch to choose a Demonstration program.");
wait_ms(1000);
//Test.Run(); // Calls the Run() routine, which manages everything the webserver needs to work
//Coderacer.wifi_printf("Activate a switch to choose a Demonstration program.");
//wait_ms(1000);
/*the IP adress of the server is given out on the Serial monitor. It is currently connected to the CodeRacer we used for testing and programming this sample code.
If the IP adress of YOUR CodeRacer differs from the one specified in Webserver.cpp, you have to change the following part of the HTML char array:
var Url ="http://192.168.1.146/"; --> var Url ="your_IP_adress"; */
@ -53,14 +54,14 @@ instructions on how to get it running are shown and executed above. */
for a small amount of time and turning 90 degrees to the right. NOTE: if you want to print out debug message on the Webserver,
build in a certain delay time so the AJAX protocoll can process the message (otherwise it won't get displayed). */
{
Coderacer.wifi_printf("Selected: Demo 1, activate your CodeRacer to start");
//Coderacer.wifi_printf("Selected: Demo 1, activate your CodeRacer to start");
Coderacer.set_leds_all_off();
wait_ms(5000);
//wait_ms(5000);
while(Coderacer.is_active()) // If the left button is pressed, the CodeRacer becomes active and the loop starts
{
wait_ms(300);
//wait_ms(300);
Distance= Coderacer.usonic_measure_single_shot_cm();
while(Distance>25)
while(Distance>25 && Coderacer.is_active() )
{
Distance=Coderacer.usonic_measure_single_shot_cm();
Coderacer.servo_sweep();
@ -70,154 +71,17 @@ instructions on how to get it running are shown and executed above. */
Coderacer.stop_driving();
Coderacer.servo_set_to_center();
wait_ms(500);
if(Coderacer.is_active())
{
Coderacer.drive_backward_for_ms(500);
wait_ms(500);
Coderacer.wifi_printf("Drehung!"); //prints out a message on the webserver
wait_ms(500);
//Coderacer.wifi_printf("Drehung!"); //prints out a message on the webserver
//wait_ms(500);
Coderacer.turn_degree(80); // Due to the inertia of the wheels, lower the degrees of the turn a bit- in this case, although 80 are stated, the Racer does an almost perfect 90 degree turn...
wait_ms(500);
}
break;
}
case 2:
/* a more "intelligent" way to tell the CodeRacer where to drive. Basically the idea is to let the CodeRacer drive forward until a certain distance is reached (as done before).
Then, the servo sweeps from left to right filling an array of distances, getting the index of the highest value and then turning a precise amount of degrees,
depending on that value. The scheme of the code is explained below*/
{
Coderacer.wifi_printf("Selected: Demo 2, activate your CodeRacer to start");
Coderacer.set_leds_all_off();
wait_ms(5000);
while(Coderacer.is_active())
{
Distance= Coderacer.usonic_measure_cm();
while (Distance>25) // Again a simple way to tell the Racer "drive until a distance of __ cm is reached"
{
Distance= Coderacer.usonic_measure_single_shot_cm();
Coderacer.servo_sweep();
Coderacer.drive_forward(210, 190);
}
Coderacer.stop_driving();
wait_ms(1000);
for(int i=0, k=10; i<160; i++, k++) // sweeps the servo from right to leftmost position and fills an array with the measured distances
{
Coderacer.servo_set_position_wait(k);
Array[i]=Coderacer.usonic_measure_single_shot_cm();
}
Coderacer.servo_set_to_center(); // Reset servo
wait_ms(200);
maximum= Array[0]; // Defines the maximum value of the array at the index 0
for (int c = 0; c < 160; c++) // Emitts the highest value of the distance- array. One flaw: only the first time this value shows up is saved.
{
if (Array[c] > maximum)
{
maximum = Array[c];
location = c;
}
}
for(int i=0, k=80; i<160; i++, k--) // Finally, create an array of degrees from 80 to -80 (representing the distance array from 0 to 160)
{
Degrees[i]= k;
}
Coderacer.turn_degree(Degrees[location]); // ...And turn the servo by the number of degrees roughly representing the biggest distance!
wait_ms(500);
}
break;
}
case 3:
/* a demo program featuring: an alternative way telling the CodeRacer to stop when a ceratin distance is reached. the Racer will drive around
happily, turning left and right until the specified value is measured. NOTE: it might occur that the Racer won't get to measure the stop distance
- if that is the case, just hold your hand in front of it so it breaks out of the while- loop ;) */
{
Coderacer.wifi_printf("Selected: Demo 3, activate your CodeRacer to start");
Coderacer.set_leds_all_off();
wait_ms(5000);
while(Coderacer.is_active())
{
Distance = Coderacer.usonic_measure_cm(); // measure the distance - at the position of the servo
Coderacer.start_stop_at_min_distance(20); // Select a distance- the CodeRacer will stop if the Usonic Sensor measures it
while(!Coderacer.stopped_at_min_distance()) // WHILE the CodeRacer hasn't stopped at the previous declared distance...
{
if(Distance > 50)
{
Coderacer.drive_forward();
Coderacer.servo_sweep();
}
else if(Distance > 40)
{
Coderacer.turn_right();
}
else if(Distance > 30)
{
Coderacer.turn_left();
}
else
{
Coderacer.drive_backward();
}
Distance = Coderacer.usonic_measure_cm();
// Measure the distance again- be careful with usonic_measure_cm(), as this method takes a while to complete. Calling it too fast will crash your Code.
}
Coderacer.wifi_printf("Stopped at minimal stopped distance selected by user!!");
wait_ms(2000);
Coderacer.set_inactive();
}
break;
}
case 4:
/* a demo program showing the scheme of displaying messages in the Webserver. After doing so, take a look at the dashboard on the website!
the racer will drive a specific amounts of ticks for the left and right wheel afterwards, you can check if that works correctly looking into
the table. NOTE: you might want to disable the CodeRacer from driving away- no stopping condition built in this time, so it might
bump into something while driving... just let the wheels turn freely and watch the dashboard... */
{
Coderacer.wifi_printf("Selected: Demo 4, activate CodeRacer to start");
Coderacer.set_leds_all_off();
wait_ms(5000);
while(Coderacer.is_active())
{
Coderacer.set_obstacle_stop(true);
Coderacer.wifi_printf("Welcome...");
wait_ms(1000);
// As previously mentioned, the website resfrehes internally, but only in an interval of 750ms
// So if you want your message to be properly displayed, you got to build in a delay using wait_ms...
Coderacer.wifi_printf("Using wifi_printf");
wait_ms(1000);
Coderacer.wifi_printf("You can display custom messages on the Webserver!");
wait_ms(1000);
Coderacer.wifi_printf("But you knew that already!");
wait_ms(1000);
Coderacer.wifi_printf("Dont forget to build in small waiting windows...");
wait_ms(1000);
Coderacer.wifi_printf("So your message is transmitted properly!");
wait_ms(1000);
Coderacer.wifi_printf("Also, check out the clear button!");
wait_ms(1000);
Coderacer.wifi_printf("Now, watch the stepcounters in the table!");
wait_ms(2000);
Coderacer.drive_ticks_left(200, true);
while (true== Coderacer.is_driving()){} // IMPORTANT! halts the Code while the Racer is driving its __ steps on each side.
wait_ms(3000);
Coderacer.Reset_Stats(); // Resets the stats of the Racer, including stepcounters and speed settings to default.
Coderacer.speed_settings(255, 245); // New Speed settings for both drives...
wait_ms(3000); // The delay is once again needed so the debug message of reset_stats() is properly displayed...
Coderacer.drive_ticks_right(200, true);
while (true== Coderacer.is_driving()){}
Coderacer.wifi_printf("End of demo 3!");
wait_ms(3000);
Coderacer.set_inactive();
}
break;
}
}
}