codetemplates/coderacer_mkII/simple_template/src/CodeRacer_simple.cpp

87 lines
4.3 KiB
C++

// Example Code of a CodeRacer sketch with implementation of a webserver
#include "CodeRacer_MKII.h"
//#include "Webserver.h"
CodeRacerMKII Coderacer; // Inizialization of your CodeRacer
//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
void setup()
{
Coderacer.begin();
Coderacer.servo_sweep_left_pos=120; // Just a few adjustments to the servo_sweep parameters- that way, the servo sweeps more narrowly
Coderacer.servo_sweep_right_pos= 60;
/* One problem we came across during the testing of the Coderacer was, that the two engines don't run equally fast- meaning that even with
identical speed (let's say 130, 130), the Racer will slowly pull to one side on longer distances. To adjust this issue, all you can do for now is test
which engine is more powerful- and adjust your speed for the left and right side drives accordingly. */
Coderacer.speed_settings(140, 130);
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);
/*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"; */
}
void loop()
/* the loop contains four demonstration example codes, seperated by switch/ case which are supposed to give you the idea of the
CodeRacer routines, what they do, how you can use them and what you need to consider before doing so. By turning on one of the switches, you select one of the demos.
Activate the CodeRacer by pressing the left button, and watch the CodeRacer doing (hopefully) what it is advised to. Don't hesitate to watch the Webserver as well,
instructions on how to get it running are shown and executed above. */
{
DemoMode= Coderacer.switch_check();
switch (DemoMode)
{
case 0:
{
Coderacer.kitt(); // This is just some fun stuff happening while no program has been selected... feel free to edit it out :)
break;
}
case 1:
/* a Demo featuring: measuring the distance, driving forward until it falls below a certain value, then driving back
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.set_leds_all_off();
//wait_ms(5000);
while(Coderacer.is_active()) // If the left button is pressed, the CodeRacer becomes active and the loop starts
{
//wait_ms(300);
Distance= Coderacer.usonic_measure_single_shot_cm();
while(Distance>25 && Coderacer.is_active() )
{
Distance=Coderacer.usonic_measure_single_shot_cm();
Coderacer.servo_sweep();
Coderacer.drive_forward();
// tells the CodeRacer to drive forward while sweeping the servo from left to right (the sweeping range is defined eariler in this code) and measure the distance
}
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.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;
}
}
}