46 char all_led_state = 0;
47 char base_led_state = 0;
49 char bl_brightness = 100;
50 char base_ir_index = 0;
51 char side_ir_index = 0;
52 signed short left_speed = 0;
53 signed short right_speed = 0;
54 char both_motor_mode = 0;
55 char last_switch_pressed;
57 char handling_event = 0;
60 char demo_running = 0;
72 float lf_current_pos_of_line = 0.0;
73 float lf_previous_pos_of_line = 0.0;
74 float lf_derivative,lf_proportional,lf_integral = 0;
82 psi.
debug(
"- Starting Demo Mode\n");
83 if(use_flash_basic == 1) top_menu = 7;
97 if(demo_running == 0) {
118 psi.
debug(
"- Demo mode ended\n");
121 void Demo::demo_handle_switch_event(
char switch_pressed)
123 if(!handling_event) {
125 last_switch_pressed = switch_pressed;
126 demo_event.attach_us(
this,&Demo::demo_event_thread, 1000);
130 void Demo::demo_event_thread()
134 switch(last_switch_pressed) {
143 user_code_running = user_code_restore_mode;
149 if(sub_menu == psi_basic_file_count) level = 0;
153 if(led_state[sub_menu] == 0) led_state[sub_menu] = 3;
154 else led_state[sub_menu]--;
158 if(all_led_state == 0) all_led_state = 3;
159 else all_led_state--;
160 for(
int i=0; i<9; i++) {
161 led_state[i]=all_led_state;
166 base_led_state = 1 - base_led_state;
193 if(bl_brightness > 0) bl_brightness-=10;
196 if(sub_menu == 13) level = 0;
199 if(sub_menu == 4 || sub_menu == 5) {
200 if(base_ir_index == 0) base_ir_index = 4;
201 else base_ir_index --;
203 if(sub_menu > 5 && sub_menu < 9) {
204 if(side_ir_index == 0) side_ir_index = 7;
205 else side_ir_index --;
207 if(sub_menu == 11) level = 0;
212 if(left_speed > 100) left_speed = 100;
217 if(right_speed > 100) right_speed = 100;
221 if(both_motor_mode == 0) both_motor_mode=5;
222 else both_motor_mode--;
223 switch(both_motor_mode) {
249 if(sub_menu == 0) level = 0;
252 if(sub_menu == 6) level = 0;
256 if(demo_running == 0) {
265 if(demo_running == 0) {
266 start_obstacle_demo();
274 if(demo_running == 0) {
275 start_spinning_demo();
283 if(demo_running == 0) {
291 if(sub_menu == 4) level = 0;
305 user_code_running = user_code_restore_mode;
311 if(sub_menu == psi_basic_file_count) level = 0;
315 led_state[sub_menu]++;
316 if(led_state[sub_menu] == 4) led_state[sub_menu] = 0;
321 if(all_led_state == 4) all_led_state = 0;
322 for(
int i=0; i<9; i++) {
323 led_state[i]=all_led_state;
328 base_led_state = 1 - base_led_state;
356 if(bl_brightness < 100) bl_brightness+=10;
359 if(sub_menu == 13) level = 0;
363 if(sub_menu == 4 || sub_menu == 5) {
364 if(base_ir_index == 4) base_ir_index = 0;
365 else base_ir_index ++;
367 if(sub_menu > 5 && sub_menu < 9) {
368 if(side_ir_index == 7) side_ir_index = 0;
369 else side_ir_index ++;
371 if(sub_menu == 11) level = 0;
376 if(left_speed < -100) left_speed = -100;
381 if(right_speed < -100) right_speed = -100;
386 if(both_motor_mode == 6) both_motor_mode=0;
387 switch(both_motor_mode) {
413 if(sub_menu == 0) level = 0;
416 if(sub_menu == 6) level = 0;
420 if(demo_running == 0) {
429 if(demo_running == 0) {
430 start_obstacle_demo();
439 if(demo_running == 0) {
440 start_spinning_demo();
448 if(demo_running == 0) {
456 if(sub_menu == 4) level = 0;
469 if(use_flash_basic == 0 && top_menu == 7) top_menu = 6;
476 if(sub_menu == 0) sub_menu = 13;
480 if(sub_menu == 0) sub_menu = 11;
485 if(sub_menu == 0) sub_menu = 3;
489 if(sub_menu == 0) sub_menu = 6;
493 if(sub_menu == 0) sub_menu = 4;
497 if(sub_menu == 0) sub_menu = 2;
501 if(sub_menu == 0) sub_menu = psi_basic_file_count;
512 if((top_menu - use_flash_basic) > 7) top_menu = 0;
517 if(sub_menu == 13) sub_menu = 0;
521 if(sub_menu == 11) sub_menu = 0;
525 if(sub_menu == 3) sub_menu = 0;
529 if(sub_menu == 6) sub_menu = 0;
533 if(sub_menu == 4) sub_menu = 0;
537 if(sub_menu == 2) sub_menu = 0;
541 if(sub_menu == psi_basic_file_count) sub_menu = 0;
555 user_code_running = user_code_restore_mode;
562 for(
int i=0; i<9; i++) {
563 led_state[i]=all_led_state;
567 if(sub_menu == 13) level = 0;
570 if(sub_menu == 11) level = 0;
574 switch(both_motor_mode) {
600 if(sub_menu == 0) level = 0;
603 if(sub_menu == 6) level = 0;
606 if(sub_menu == 4) level = 0;
609 if(sub_menu == 2) level = 0;
612 if(sub_menu == psi_basic_file_count) level = 0;
626 strcpy(topline,
"---TEST LEDS----");
629 strcpy(topline,
"--TEST SENSORS--");
632 strcpy(topline,
"--TEST MOTORS---");
635 strcpy(topline,
"---TEST RADIO---");
638 strcpy(topline,
"------INFO------");
641 strcpy(topline,
"---CODE DEMOS---");
644 strcpy(topline,
"-MOTOR CALIBRATE");
647 strcpy(topline,
"---PSI BASIC----");
650 strcpy(topline,
"------EXIT------");
653 strcpy(bottomline,
"");
659 strcpy(topline,
"-PSI BASIC MENU-");
660 if(sub_menu == psi_basic_file_count) strcpy(bottomline,
"EXIT");
661 else strcpy(bottomline,basic_filenames.at(sub_menu).c_str());
664 strcpy(topline,
"----LED MENU----");
667 switch(led_state[sub_menu]) {
669 strcpy(led_status,
"OFF");
672 strcpy(led_status,
"RED");
675 strcpy(led_status,
"GREEN");
678 strcpy(led_status,
"YELLOW");
682 if(sub_menu < 8) sprintf(bottomline,
"OUTER %d: %s",(sub_menu + 1),led_status);
683 if(sub_menu == 8) sprintf(bottomline,
"CENTER: %s",led_status);
685 switch(all_led_state) {
687 strcpy(led_status,
"OFF");
690 strcpy(led_status,
"RED");
693 strcpy(led_status,
"GREEN");
696 strcpy(led_status,
"YELLOW");
699 sprintf(bottomline,
"SET ALL %s", led_status);
702 if(base_led_state == 0) strcpy(led_status,
"OFF");
703 else strcpy(led_status,
"ON");
704 sprintf(bottomline,
"BASE: %s",led_status);
706 if(sub_menu == 11) sprintf(bottomline,
"CLED BNESS %d%%", brightness);
707 if(sub_menu == 12) sprintf(bottomline,
"DISP BNESS %d%%", bl_brightness);
708 if(sub_menu == 13) strcpy(bottomline,
"EXIT");
712 strcpy(topline,
"--SENSORS MENU--");
716 sprintf(bottomline,
"BATTERY: %1.3fV",battery);
721 sprintf(bottomline,
"DC: %1.3fV",dc);
726 sprintf(bottomline,
"CURRENT: %1.3fA",current);
731 sprintf(bottomline,
"TEMP: %3.2fC", temperature);
751 sprintf(bottomline,
"SIR%dD:%3.1f",side_ir_index+1,0.0);
754 if(ultrasonic_distance_updated == 1) {
755 sprintf(bottomline,
"USONIC:%3dcm",ultrasonic_distance);
756 }
else sprintf(bottomline,
"USONIC:---------");
760 sensors.store_line_position();
761 if(line_found == 1)sprintf(bottomline,
"LINE:%1.3f",line_position);
762 else sprintf(bottomline,
"LINE:---------");
765 sprintf(bottomline,
"EXIT");
770 strcpy(topline,
"--MOTORS MENU---");
773 sprintf(bottomline,
"LEFT: %d%%", left_speed);
776 sprintf(bottomline,
"RIGHT: %d%%", right_speed);
779 char both_mode_string[16];
780 switch(both_motor_mode) {
782 strcpy(both_mode_string,
"OFF");
785 strcpy(both_mode_string,
"BRAKE");
788 strcpy(both_mode_string,
"+50%");
791 strcpy(both_mode_string,
"+100%");
794 strcpy(both_mode_string,
"-50%");
797 strcpy(both_mode_string,
"-100%");
800 sprintf(bottomline,
"BOTH TO %s",both_mode_string);
803 sprintf(bottomline,
"EXIT");
808 strcpy(topline,
"---RADIO MENU---");
812 sprintf(bottomline,
"EXIT");
817 strcpy(topline,
"---INFO MENU----");
820 sprintf(bottomline,
"ROBOT ID: %d",robot_id);
823 sprintf(bottomline,
"SOFTWARE: %1.2f",SOFTWARE_VERSION_CODE);
826 if(firmware_version > 0) sprintf(bottomline,
"FIRMWARE: %1.2f",firmware_version);
827 else sprintf(bottomline,
"FIRMWARE: ?????");
830 sprintf(bottomline,
"PROG:%s",program_name);
833 sprintf(bottomline,
"AUTH:%s",author_name);
836 sprintf(bottomline,
"VER:%s",version_name);
839 sprintf(bottomline,
"EXIT");
844 strcpy(topline,
"---CODE DEMOS---");
847 sprintf(bottomline,
"LINE FOLLOW");
850 sprintf(bottomline,
"OBST. AVOID");
853 sprintf(bottomline,
"COLOUR SPIN");
856 sprintf(bottomline,
"STRESS TEST");
859 sprintf(bottomline,
"EXIT");
864 strcpy(topline,
"-MOTOR CALIBRATE");
867 sprintf(bottomline,
"RUN A3 TEST");
870 sprintf(bottomline,
"ENTER VALUE");
873 sprintf(bottomline,
"EXIT");
883 if(top_menu == 1 && level == 1) {
884 demo_event.attach(
this,&Demo::demo_event_thread, 0.25);
888 void Demo::start_line_demo()
897 demo_timeout.attach_us(
this,&Demo::line_demo_cycle,1000);
900 void Demo::start_obstacle_demo()
909 demo_timeout.attach_us(
this,&Demo::obstacle_demo_cycle,1000);
912 void Demo::start_stress_demo()
925 demo_timeout.attach_us(
this,&Demo::stress_demo_cycle,1000);
928 void Demo::start_spinning_demo()
938 demo_timeout.attach_us(
this,&Demo::spinning_demo_cycle,1000);
941 void Demo::line_demo_cycle()
943 if(demo_timer.read() > time_out) {
944 sensors.store_line_position();
949 lf_current_pos_of_line = line_position;
950 lf_proportional = lf_current_pos_of_line;
953 lf_derivative = lf_current_pos_of_line - lf_previous_pos_of_line;
956 lf_integral += lf_proportional;
959 lf_previous_pos_of_line = lf_current_pos_of_line;
962 lf_power = (lf_proportional * (LF_P_TERM) ) + (lf_integral*(LF_I_TERM)) + (lf_derivative*(LF_D_TERM)) ;
965 lf_right = lf_speed-lf_power;
966 lf_left = lf_speed+lf_power;
971 else if (lf_right > 1.0f)
976 else if (lf_left > 1.0f)
980 if(lf_left > lf_right){
983 float d_step = state * 0.04;
984 lf_left = 0.2 + d_step;
985 lf_right = -0.2 - d_step;
991 if(time_out > 0.1f) demo_running = 0;
996 float d_step = state * 0.04;
997 lf_left = -0.2 - d_step;
998 lf_right = 0.2 + d_step;
1004 if(time_out > 0.1f) demo_running = 0;
1015 if(demo_running == 1)demo_timeout.attach_us(
this,&Demo::line_demo_cycle,500);
1022 void Demo::stress_demo_cycle()
1024 if(demo_timer.read() > time_out) {
1025 float pct = 0.25 + (0.25 * stress_step);
1028 if(spin_step % 2 == 0) {
1036 if(spin_step > 199) {
1042 if(stress_step < 3) stress_step ++;
1043 float pct = 0.25 + (0.25 * stress_step);
1046 switch(stress_step) {
1062 if(demo_running == 1)demo_timeout.attach_us(
this,&Demo::stress_demo_cycle,500);
1069 void Demo::spinning_demo_cycle()
1071 if(demo_timer.read() > time_out) {
1112 if(led_step == 8) led_step =0;
1139 if(led_step == 4) led_step = 0;
1141 if(spin_step == 40) {
1174 if(led_step == 0) led_step =8;
1223 if(led_step == 0) led_step =8;
1227 motors.
turn(-speed);
1250 if(led_step == 0) led_step = 4;
1253 if(spin_step == 40) {
1287 if(led_step == 8) led_step =0;
1290 motors.
turn(-speed);
1300 if(demo_running == 1)demo_timeout.attach_us(
this,&Demo::spinning_demo_cycle,500);
1307 void Demo::obstacle_demo_cycle()
1309 if(demo_timer.read() > time_out) {
1323 if(front_left > 400 || front_right > 400) {
1326 if(front_left > front_right)state=2;
1352 if(led_step == 5) led_step = 0;
1375 if(demo_running == 1)demo_timeout.attach_us(
this,&Demo::obstacle_demo_cycle,200);
1382 void Demo::demo_update_leds()
1386 for(
int i=0; i<8; i++) {
1387 if(led_state[i]==1 || led_state[i]==3) red+=(1<<i);
1388 if(led_state[i]==2 || led_state[i]==3) green+=(1<<i);
1391 float brightness_f = brightness / 100.0f;
void write_string(char *message)
void store_illuminated_raw_ir_values(void)
unsigned short read_illuminated_raw_ir_value(char index)
void store_background_raw_ir_values(void)
void set_leds(char green, char red)
void set_position(char row, char column)
void set_center_led(char state)
void store_ir_values(void)
void set_base_led(char state)
void update_ultrasonic_measure(void)
unsigned short get_background_base_ir_value(char index)
float get_dc_voltage(void)
void store_background_base_ir_values(void)
void set_backlight_brightness(float brightness)
float get_battery_voltage(void)
unsigned short get_illuminated_base_ir_value(char index)
void set_left_motor_speed(float speed)
void backward(float speed)
float get_temperature(void)
unsigned short get_background_raw_ir_value(char index)
void store_illuminated_base_ir_values(void)
void debug(const char *format,...)
void set_right_motor_speed(float speed)
void forward(float speed)
void start_demo_mode(void)
unsigned short get_illuminated_raw_ir_value(char index)