introduced wait_ms() - made static variable static

This commit is contained in:
Jens Noack 2020-04-15 00:05:45 +02:00
parent 8cd52dd6ab
commit 4dc43aa247
3 changed files with 95 additions and 34 deletions

View File

@ -3,6 +3,30 @@
using namespace std;
static void global_stop_driving();
static void _count_steps_left();
static void _count_steps_right();
static volatile unsigned int left_step_counter=0;
static volatile unsigned int left_step_stopcount=0;
static volatile unsigned int right_step_counter=0;
static volatile unsigned int right_step_stopcount=0;
static volatile bool syncstop= 0;
static volatile bool _drive;
static volatile float timebeforeleft= 0;
static volatile float timebeforeright= 0;
static volatile float timeafterleft= 0;
static volatile float timeafterright= 0;
static volatile bool obstacle_stop= false;
static volatile bool obstacle_left_side= false;
static volatile bool obstacle_right_side= false;
static volatile bool coderracer_activ = false;
static volatile bool right_button_pressed =false;
static volatile unsigned int right_button_count=0;
static volatile unsigned long left_button_last_pressed_at_ms = millis();
static volatile unsigned long right_button_last_pressed_at_ms = millis();
/** @brief CodeRace constructor without pins. All pins settings taken from the coderacer header file
* @return nothing
*/
@ -1496,7 +1520,7 @@ void IRAM_ATTR CodeRacerMKII::_set_button_state_right() {
/** @brief basically the same as stop_driving(), but is required for all routines attached to an interrupt which can't call stop_driving()
* @return nothing
*/
void global_stop_driving(){
static void IRAM_ATTR global_stop_driving(){
ledcWrite(DRIVE_PWM_RIGHT_CHANNEL, 0);
if(true == syncstop)
{
@ -1513,26 +1537,24 @@ void global_stop_driving(){
/** @brief this routine is called each time the left PE barrier changes its state. it increments (and by that counts) the ticks driven by the left wheel and is also instructed to stop the racer if the stopcount set by drive_ticks() is reached using the global_stop_driving() routine
* @return nothing
*/
void IRAM_ATTR CodeRacerMKII::_count_steps_left() {
static void IRAM_ATTR _count_steps_left() {
timeafterleft= millis();
timebeforeleft = timebeforeleft == 0 ? timeafterleft : timebeforeleft;
left_step_counter++;
if (left_step_counter==left_step_stopcount)
{
//TODO: use stop_driving();
global_stop_driving();
}
}
/** @brief this routine is called each time the left PE barrier changes its state. it increments (and by that counts) the ticks driven by the right wheel and is also instructed to stop the racer if the stopcount set by drive_ticks() is reached using the global_stop_driving() routine
* @return nothing
*/
void IRAM_ATTR CodeRacerMKII::_count_steps_right() {
static void IRAM_ATTR _count_steps_right() {
timeafterright= millis();
timebeforeright = timebeforeright == 0 ? timeafterright : timebeforeright;
right_step_counter++;
if (right_step_counter==right_step_stopcount)
{
//TODO: use stop_driving();
global_stop_driving();
}
}
@ -1542,7 +1564,7 @@ void IRAM_ATTR CodeRacerMKII::_count_steps_right() {
void IRAM_ATTR CodeRacerMKII::_stop_obstacle_left(){
if(HIGH== digitalRead(H_INFRARED_SENSOR_LEFT)){
if(1==obstacle_stop){
global_stop_driving();
global_stop_driving();
}
obstacle_left_side=true;
}
@ -1568,3 +1590,8 @@ void IRAM_ATTR CodeRacerMKII::_stop_obstacle_right(){
//** Helper methods
//**********************************************************************************************************************
void wait_ms(long waittime_ms)
{
long started_ms = millis();
while(millis()-started_ms < waittime_ms){}
}

View File

@ -80,28 +80,9 @@
#define H_LED_BLUE_PIN 14
#define H_LED_WHITE_PIN 12
using namespace std;
static volatile unsigned int left_step_counter=0;
static volatile unsigned int left_step_stopcount=0;
static volatile unsigned int right_step_counter=0;
static volatile unsigned int right_step_stopcount=0;
static volatile bool syncstop= 0;
static volatile bool _drive;
static volatile float timebeforeleft= 0;
static volatile float timebeforeright= 0;
static volatile float timeafterleft= 0;
static volatile float timeafterright= 0;
static volatile bool obstacle_stop= false;
static volatile bool obstacle_left_side= false;
static volatile bool obstacle_right_side= false;
static volatile bool coderracer_activ = false;
static volatile bool right_button_pressed =false;
static volatile unsigned int right_button_count=0;
static volatile unsigned long left_button_last_pressed_at_ms = millis();
static volatile unsigned long right_button_last_pressed_at_ms = millis();
void global_stop_driving();
void wait_ms(long waittime_ms);
enum ledstate {
LEDOFF,
@ -183,8 +164,6 @@ class CodeRacerMKII {
//objects
Servo* _servo;
Servo* _servo_dummy;
static void _count_steps_left();
static void _count_steps_right();
static void _set_button_state_left();
static void _set_button_state_right();
static void _stop_obstacle_left();

View File

@ -27,6 +27,11 @@ int iTicks=31;
float callibration_drive(unsigned int tickstogo, float calfactor);
int Emit_vmin(bool state);
float calculate_min_veliocity(bool side);
unsigned int getcount_function(bool left_notright);
void set_speed_function(bool left_notright, unsigned int speed);
unsigned int get_inmin(bool left_notright);
//---- Hier startet der Code zum Einstellen aller wichtigen Dinge. Setup() wird einmal ausgeführt. ----
void setup() {
// Monitor
@ -52,21 +57,71 @@ void setup() {
void loop()
{
bool started = false;
while(coderacer.start_stop()== 1)
while(coderacer.start_stop()== 1)
{
if(false == started)
{
started = true;
delay(1000);
wait_ms(1000);
}
float vminright= calculate_min_veliocity(1);
Serial.println(vminright);
delay(5000);
coderacer.stop_driving();
unsigned int in_min_left = get_inmin(true);
wait_ms(1000); // do not use delay() this will create problems with interrupt routines! use the wait_ms() instead !!!!
unsigned int in_min_right = get_inmin(false);
Serial.printf("left in_min: %u right in_min: %u \n", in_min_left, in_min_right);
//float vminright= calculate_min_veliocity(1);
//Serial.println(vminright);
//speedleft = speedleft == 0 ? 128 : 0;
//speedright = speedright == 0 ? 128 : 0;
//coderacer.drive_forward(speedleft, speedright);
//coderacer.set_drives_speed_left_right(speedleft,speedright);
//coderacer.drive_forward();
//wait_ms(1000);
//coderacer.stop_driving();
wait_ms(1000);
coderacer.set_inactive();
}
}
unsigned int get_inmin(bool left_notright )
{
unsigned int steps_before_driving = getcount_function(left_notright);
Serial.printf("Steps before driving: %u \n", steps_before_driving );
for(unsigned int pwm_in = 0; pwm_in < 255; pwm_in = pwm_in+5)
{
unsigned int steps_after_driving = getcount_function(left_notright);
if(steps_after_driving > steps_before_driving)
{
Serial.printf("Steps after driving: %u \n", steps_before_driving );
coderacer.stop_driving();
return pwm_in*1.1;
}
set_speed_function(left_notright, pwm_in);
wait_ms(100);
}
coderacer.stop_driving();
return 0 ;
}
unsigned int getcount_function(bool left_notright)
{
if(true == left_notright){return coderacer.show_left_stepcounter();}
else{return coderacer.show_right_stepcounter();}
}
void set_speed_function(bool left_notright, unsigned int speed)
{
if(true == left_notright){coderacer.drive_forward(speed,0);}
else{coderacer.drive_forward(0,speed);}
}
float calculate_min_veliocity(bool side)
{
ispeed=Emit_vmin(side);