ESP Line Following Buggy
2nd Year Embedded System Project (Group 48 - 2023/24)
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1
9#include "mbed.h"
10#include "QEI.h"
11
12#include "constants.h"
13#include "pin_assignments.h"
14#include "bluetooth.h"
15#include "motor.h"
16#include "PID.h"
17#include "motor_driver_board.h"
18#include "sensor_array.h"
19
20
21/* BT COMMAND CHARS */
23{
24 // 1 - cmd types
25 ch_execute = 'E', // E
26 ch_get = 'G', // G
27 ch_set = 'S', // S
28 ch_continous = 'C', // C
29
30 // 2 - exec types
31 ch_stop = 'S', // S
32 ch_active_stop = 'X', // X
33 ch_uturn = 'U', // U
34 ch_encoder_test = 'E', // E
36 ch_straight_test = 'Z', // C
37 ch_square_test = 'Q', // Q
38 ch_PID_test = 'P', // P
40 ch_line_follow = 'F', // F
44
45 // 2 - data types
46 ch_pwm_duty = 'D', // D
48 ch_speed = 'S', // S
49 ch_gains_PID = 'P', // P
50 ch_tau_PID = 'T', // T
51 ch_current_usage = 'C', // C
52 ch_runtime = 'R', // R
53 ch_loop_time = 'X', // X
54 ch_loop_count = 'Y', // Y
55
56 // 3 - obj types
57 ch_motor_left = 'L', // L // PID, Encoder Ticks, Velocity
58 ch_motor_right = 'R', // R // PID, Encoder Ticks, Velocity
59 ch_motor_both = 'B', // B
60 ch_sensor = 'S', // S
61 ch_no_obj = 'D', // default case
62};
63
64
65/* BUGGY MODES */
83
84
92{
94 float set_angle;
104 // Accel Curve Variables
108
109 // square task variables
113};
114
115
116/* GLOBAL VARIBLES DECLARATIONS */
117volatile bool pc_serial_update = false;
118volatile bool bt_serial_update = false;
121
122float bt_float_data[3] = {0};
123short int data_log[LOG_SIZE][5] = {0};
124int log_index = 0;
127float* pid_constants; // used only for sending datat to bluetooth
129
130Buggy_modes buggy_mode; // stores buggy states when performing actions
133
135
136
137/* OBJECTS DECLARATIONS */
138DigitalOut LED(LED_PIN); // Debug LED set
139Serial pc(USBTX, USBRX, 115200); // set up serial comm with pc
140Timer global_timer; // set up global program timer
145
150Motor motor_left (MOTORL_PWM_PIN, MOTORL_DIRECTION_PIN, MOTORL_BIPOLAR_PIN, MOTORL_CHA_PIN, MOTORL_CHB_PIN, PULSE_PER_REV, MOTOR_PWM_FREQ, CONTROL_UPDATE_RATE, LP_SPEED_A0, LP_SPEED_B0, LP_SPEED_B1, WHEEL_RADIUS);
151Motor motor_right(MOTORR_PWM_PIN, MOTORR_DIRECTION_PIN, MOTORR_BIPOLAR_PIN, MOTORR_CHA_PIN, MOTORR_CHB_PIN, PULSE_PER_REV, MOTOR_PWM_FREQ, CONTROL_UPDATE_RATE, LP_SPEED_A0, LP_SPEED_B0, LP_SPEED_B1, WHEEL_RADIUS);
156
157
158// Helper Function Prototypes:
159void stop_motors(void);
160void update_buggy_status(int tick_count_left, int tick_count_right);
161void reset_everything(void);
162bool bt_parse_rx(char* rx_buffer);
163void control_update_ISR(void);
164void serial_update_ISR(void);
165void stop_detect_ISR(void);
166void bt_send_data(void);
167void pc_send_data(void);
168void sensor_update_ISR();
169void slow_accel_ISR(void);
170
171
172/* MAIN FUNCTION */
173int main()
174{
177
178 while (!bt.is_ready()) {}; // while bluetooth not ready, loop and do nothing
179
181
182 global_timer.start(); // Starts the global program timer
183 sensor_ticker.attach_us(&sensor_update_ISR, SENSOR_UPDATE_PERIOD_US); // Starts the control ISR update ticker
184 control_ticker.attach_us(&control_update_ISR, CONTROL_UPDATE_PERIOD_US); // Starts the control ISR update ticker
185 serial_ticker.attach(&serial_update_ISR, SERIAL_UPDATE_PERIOD); // Starts the control ISR update ticker
186
187 while (1)
188 {
189 int curr_time = global_timer.read_us();
190
191 /* --- START OF COMMAND PROCESSING --- */
193 {
194 char* rx_buf = bt.get_rx_buffer();
195 if (!bt_parse_rx(rx_buf))
196 {
197 bt.send_fstring("Err: %s", rx_buf);
198 }
200 }
201
202 if (pc.readable())
203 {
204 switch (pc.getc())
205 {
206 case 'r':
209 break;
210 case 's':
212 stop_motors();
213 break;
214 case 'D':
216 for(int i = 0; i < log_index; i++)
217 {
218 pc.printf("%.5f,%.3f,%.3f,%.3f,%.3f,%.3f\n",
220 (float) (data_log[i][0] / 1000.0), //= set_point
221 (float) (data_log[i][1] / 1000.0), //= measurement
222 (float) (data_log[i][2] / 1000.0), //= propotional
223 (float) (data_log[i][3] / 1000.0), //= integrator
224 (float) (data_log[i][4] / 1000.0)); //= differentiator
225 }
226 log_index = 0;
227 break;
228 default:
229 break;
230 }
231
232 }
233 /* --- END OF COMMAND PROCESSING --- */
234
235
236 /* --- START OF BUGGY ACTIONS/STATE LOGIC CODE --- */
238
239 // Buggy mode transition code
241 {
244
245 switch (buggy_mode)
246 {
247 case PID_test:
250 bt.send_fstring("P:%.2f,I:%.2f", pid_constants[0], pid_constants[1]);
251 bt.send_fstring("D:%.2f,T:%.2f\n", pid_constants[2], pid_constants[3]);
254 break;
255 case square_mode:
256 case straight_test:
258 break;
259 case task_test:
261 bt.set_continous(true);
262 break;
263 case uturn:
267 break;
268 case static_tracking:
271 bt.send_fstring("\nP:%.3f\nI:%.3f\n", pid_constants[0], pid_constants[1]);
272 bt.send_fstring("D:%.3f\nT:%.3f\n", pid_constants[2], pid_constants[3]);
275 break;
276 case line_follow_auto:
277 case line_follow:
279
280 // if (!sensor_array.is_line_detected())
281 // {
282 // buggy_mode = inactive;
283 // bt.send_fstring("Line undetected\n", global_timer.read());
284 // break;
285 // }
286
288 bt.send_fstring("T:%.3f\n", global_timer.read());
289 bt.send_fstring("\nP:%.3f\nI:%.3f\n", pid_constants[0], pid_constants[1]);
290 bt.send_fstring("D:%.3f\nT:%.3f\n", pid_constants[2], pid_constants[3]);
291
294
299 break;
300 case active_stop:
304 bt.send_fstring("T:%.3f\n", global_timer.read());
305 break;
306 case stop_detect_line:
311 break;
312 case calibration:
316 for (int i = 0; i < 6; i++)
317 {
318 bt.send_fstring("%d:%.4f\n", i, cal_constants[i]);
319 }
321 break;
322 default:
323 break;
324 }
326 }
327
328 // Buggy mode continous logic
329 switch (buggy_mode)
330 {
331 case inactive:
333 stop_motors();
334 break;
335 case square_mode:
336 switch (buggy_status.sq_stage)
337 {
338 case 0:
343 break;
344 case 7:
345 case 1:
346 case 3:
347 case 5:
348 if (buggy_status.distance_travelled >= buggy_status.sq_set_distance) // wait to move 1m then, start turning right
349 {
350 if (buggy_status.sq_stage == 7)
351 {
353 }
358 }
359 break;
360 case 2:
361 case 4:
362 case 6:
363 case 8:
364 if (buggy_status.cumulative_angle_deg >= buggy_status.sq_set_angle) // wait to turn 90 and start moving straight
365 {
370 }
371 break;
372 case 9:
373 case 11:
374 case 13:
375 if (buggy_status.distance_travelled >= buggy_status.sq_set_distance) // wait to move 1m then, start turning left
376 {
381 }
382 break;
383 case 10:
384 case 12:
385 case 14:
386 if (buggy_status.cumulative_angle_deg <= buggy_status.sq_set_angle) // wait to turn -90 and start moving straight
387 {
392 }
393 break;
394 case 15:
396 {
398 stop_motors();
400 }
401 break;
402 default:
403 break;
404 }
405 break;
406 case PID_test:
408 {
410 }
411 // if (buggy_status.distance_travelled >= 0.1)
412 // {
413 // buggy_status.set_velocity = 0.5;
414 // }
415 // if (buggy_status.distance_travelled >= 0.4)
416 // {
417 // buggy_status.set_velocity = 1;
418 // }
419 // if (buggy_status.distance_travelled >= 1)
420 // {
421 // buggy_status.set_velocity = 0.5;
422 // }
424 {
427 }
428 break;
429
430 case uturn:
432 {
434 {
437 }
438 else
439 {
441 }
442 }
443 break;
444 case line_follow_auto:
446 {
448 }
450 {
451 // buggy_mode = stop_detect_line;
452 // stop_motors();
453 // float prev_speed = buggy_status.set_velocity;
454 // while (!sensor_array.is_line_detected())
455 // {
456 // buggy_status.set_velocity = -1;
457 // }
458 // buggy_status.set_velocity = prev_speed;
459 }
460 break;
461 case line_follow:
463 // if (buggy_status.is_accelerating)
464 // {
465 // float accel_distance = buggy_status.distance_travelled - buggy_status.accel_start_distance;
466 // if (accel_distance > MANUAL_ACCEL_DISTANCE)
467 // {
468 // // pc.printf("SLOWING DOWN\n");
469 // buggy_status.set_velocity = lf_velocity;
470 // buggy_status.accel_start_angle = buggy_status.cumulative_angle_deg;
471 // buggy_status.is_accelerating = false;
472 // }
473 // else
474 // {
475 // // pc.printf("fast\n");
476 // buggy_status.set_velocity = MANUAL_ACCEL_SPEED;
477 // }
478 // }
479 // else
480 // {
481 // float accel_angle = fabsf(buggy_status.cumulative_angle_deg - buggy_status.accel_start_angle);
482 // // pc.printf("%f\n", accel_angle);
483 // if (accel_angle > MANUAL_ACCEL_ANGLE)
484 // {
485 // // pc.printf("Accelerating!!! %f\n", accel_angle);
486 // buggy_status.set_velocity = MANUAL_ACCEL_SPEED;
487 // buggy_status.accel_start_distance = buggy_status.distance_travelled;
488 // buggy_status.is_accelerating = true;
489 // }
490 // else
491 // {
492 // // pc.printf("slowstuff\n");
493 // buggy_status.set_velocity = lf_velocity;
494 // }
495 // }
496 break;
497 case stop_detect_line:
499 {
501 };
502 default:
503 break;
504 }
505 /* --- END OF BUGGY ACTIONS/STATE LOGIC CODE --- */
506
507
508 /* --- START OF SERIAL UPDATE CODE --- */
510 {
511 bt_send_data();
512 bt_serial_update = false;
513 }
514
516 {
517 pc_send_data();
518 pc_serial_update = false;
519 }
520 /* --- END OF SERIAL UPDATE CODE --- */
521
522
523 /* END OF LOOP */
524 loop_exec_time = global_timer.read_us() - curr_time;
525 }
526}
527
528
529/* HELPER FUNCTIONS */
531{
532 // Get the current time to measure the execution time
533 int curr_time = global_timer.read_us();
534
535 /* Run all the update functions: */
538
539 /* Calculate and apply PID output on certain modes only*/
540 if (buggy_mode == PID_test ||
544 buggy_mode == uturn ||
547 {
548 // Update set PID_angle and calculate motor set speed depending on buggy mode
549 if (buggy_mode == square_mode ||
550 buggy_mode == PID_test ||
551 buggy_mode == uturn ||
553 {
555
558 }
559 else if (buggy_mode == static_tracking)
560 {
563 }
564 else
565 {
566 float base_speed = buggy_status.set_velocity;
567
568 // float sens_out_abs = fabsf(sensor_array.get_filtered_output());
569 // if (sens_out_abs > SLOW_TURNING_THRESH)
570 // {
571 // base_speed = buggy_status.set_velocity * SLOW_TURNING_GAIN; // (1 - pid_out_abs / (PID_S_MAX_OUT * SLOW_TURNING_GAIN));
572 // }
573 // else
574 // {
575 // base_speed = buggy_status.set_velocity;
576 // }
577
578 // buggy_status.left_set_speed = base_speed + PID_sensor.get_output();
579 // buggy_status.right_set_speed = base_speed - PID_sensor.get_output();
580
581
582 float speed_offset = PID_sensor.get_output();
583 if (speed_offset > 0)
584 {
585 buggy_status.left_set_speed = base_speed;
586 buggy_status.right_set_speed = base_speed - 2 * speed_offset;
587 }
588 else
589 {
590 buggy_status.left_set_speed = base_speed - 2 * -speed_offset;
591 buggy_status.right_set_speed = base_speed;
592 }
593 }
594
595 // Calculate Motor PID and apply the output:
600
601 // PID Data Logging
602 if(log_index < LOG_SIZE)
603 {
604 float** out_arr = PID_motor_left.get_terms();
605 // float** out_arr = PID_motor_right.get_terms();
606 // float** out_arr = PID_sensor.get_terms();
607 // data_log[log_index][0] = *out_arr[0]; //= time_index
608 data_log[log_index][0] = (short int) (*out_arr[1] * 1000); //= set_point
609 data_log[log_index][1] = (short int) (*out_arr[2] * 1000); //= measurement
610 // data_log[log_index][3] = *out_arr[3]; //= error
611 data_log[log_index][2] = (short int) (*out_arr[4] * 1000); //= propotional
612 data_log[log_index][3] = (short int) (*out_arr[5] * 1000); //= integrator
613 data_log[log_index][4] = (short int) (*out_arr[6] * 1000); //= differentiator
614 // data_log[log_index][7] = *out_arr[7]; //= output
615 log_index++;
616 }
617
618 // Sends PID Data to the PC (WARNING: CAN CAUSE BT MALFUNCTION)
619 // float** out_arr = PID_motor_left.get_terms();
620 // pc.printf("%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f\n",
621 // *out_arr[0], //= time_index
622 // *out_arr[1], //= set_point
623 // *out_arr[2], //= measurement
624 // *out_arr[3], //= error
625 // *out_arr[4], //= propotional
626 // *out_arr[5], //= integrator
627 // *out_arr[6], //= differentiator
628 // *out_arr[7]); //= output
629
630 // float** out_arr = PID_sensor.get_terms();
631 // pc.printf("o:%.2f,", *out_arr[7]);
632 // pc.printf("d:%.2f\n", *out_arr[6]);
633
634 // pc.printf("o:%.2f,", sensor_array.get_array_output());
635 // pc.printf("f:%.2f\n", sensor_array.get_filtered_output());
636
637 }
638
639 // Motor LP Filter Debug:
640 // pc.printf("%.4f,%.4f\n", motor_left.get_speed(), motor_left.get_filtered_speed());
641 // pc.printf("%.4f\n", buggy_status.cumulative_angle_deg);
642
643 // Measure control ISR execution time
644 ISR_exec_time = global_timer.read_us() - curr_time;
645}
646
647
653
654
656{
659 {
661 }
662}
663
665{
667 {
669 }
670}
671
672
673void stop_motors(void)
674{
677}
678
679
680void update_buggy_status(int tick_count_left, int tick_count_right)
681{
682 // Angle calculations
683 buggy_status.cumulative_angle_deg = (float) (360 * WHEEL_RADIUS) * (tick_count_left - tick_count_right) / (WHEEL_SEPERATION * 4 * PULSE_PER_REV);
684
685 // distance travelled calculation
686 buggy_status.distance_travelled = ((float) (tick_count_left + tick_count_right) / (2 * PULSE_PER_REV * 4)) * 2 * PI * WHEEL_RADIUS;
687}
688
689
691{
698
699 memset(&buggy_status, 0, sizeof(buggy_status));
700}
701
702
703bool bt_parse_rx(char* rx_buffer)
704{
705 /* This function reads the incoming data and checks for
706 specific command format and returns the command type
707 returns false if parsing failed */
708
709 // command parsing -> to-do
710
711 char cmd_type = rx_buffer[0];
712 char exec_type = rx_buffer[1];
713 char data_type = rx_buffer[1];
714 char obj_type = rx_buffer[2];
715
716 int data_amount;
717
718 switch(rx_buffer[0])
719 {
720 case ch_continous:
722 break;
723 case ch_get:
724 bt.set_send_once(true);
725 bt_data_sent = data_type;
726 bt_obj_sent = obj_type;
727 break;
728 case ch_set:
729 data_amount = (data_type == ch_gains_PID) ? 3 : 1;
730 if (sscanf(rx_buffer, "%*s %f %f %f", &bt_float_data[0], &bt_float_data[1], &bt_float_data[2]) != data_amount)
731 {
732 return false;
733 }
734
735 switch (data_type)
736 {
737 case ch_pwm_duty: // P
738 switch (obj_type)
739 {
740 case ch_motor_left:
742 break;
743 case ch_motor_right:
745 break;
746 default:
747 break;
748 }
749 break;
750 case ch_speed: // S
752 break;
753 case ch_gains_PID:
754 switch (obj_type)
755 {
756 case ch_motor_left:
758 break;
759 case ch_motor_right:
761 break;
762 case ch_motor_both:
765 break;
766 case ch_sensor:
768 break;
769 default:
770 break;
771 }
772 break;
773 case ch_tau_PID:
774 switch (obj_type)
775 {
776 case ch_motor_left:
778 break;
779 case ch_motor_right:
781 break;
782 case ch_motor_both:
785 break;
786 case ch_sensor:
788 break;
789 default:
790 break;
791 }
792 break;
793 default:
794 break;
795 }
796 break;
797 case ch_execute:
798 switch (exec_type)
799 {
800 case ch_stop:
802 break;
803 case ch_active_stop:
805 break;
806 case ch_uturn:
808 break;
809 case ch_encoder_test:
813 break;
818 break;
819 case ch_straight_test:
821 break;
822 case ch_square_test:
824 break;
825 case ch_PID_test:
827 break;
829 LED = !LED;
830 break;
831 case ch_line_follow:
833 break;
836 break;
839 break;
840 case ch_calibrate:
842 break;
843 default:
844 break;
845 }
846 break;
847
848 default:
849 return false;
850 break;
851 }
852 return true;
853}
854
855
857{
858 pc_serial_update = true;
859 bt_serial_update = true;
860}
861
862
863void bt_send_data(void)
864{
865 // Handling sending data through BT
866 if (bt.is_continous() || bt.is_send_once())
867 {
868 switch (bt_data_sent)
869 {
870 case ch_pwm_duty: // P
871 switch (bt_obj_sent)
872 {
873 case ch_motor_left:
874 bt.send_fstring("DC L: %.2f", motor_left.get_duty_cycle());
875 break;
876 case ch_motor_right:
877 bt.send_fstring("DC R: %.2f", motor_right.get_duty_cycle());
878 break;
879 case ch_motor_both:
881 break;
882 default:
883 break;
884 }
885 break;
886 case ch_ticks_cumulative: // T
887 switch (bt_obj_sent)
888 {
889 case ch_motor_left:
890 bt.send_fstring("Ticks L: %d", motor_left.get_tick_count());
891 break;
892 case ch_motor_right:
893 bt.send_fstring("Ticks R: %d", motor_right.get_tick_count());
894 break;
895 case ch_motor_both:
897 break;
898 default:
899 break;
900 }
901 break;
902 case ch_speed: // V
903 switch (bt_obj_sent)
904 {
905 case ch_motor_left:
906 bt.send_fstring("Speed L: %f", motor_left.get_speed());
907 break;
908 case ch_motor_right:
909 bt.send_fstring("Speed R: %f", motor_right.get_speed());
910 break;
911 case ch_motor_both:
912 bt.send_fstring("S L:%.3f/ R:%.3f", motor_left.get_speed(), motor_right.get_speed());
913 break;
914 default:
915 break;
916 }
917 break;
918 case ch_gains_PID: // G
919 switch (bt_obj_sent)
920 {
921 case ch_motor_left:
923 break;
924 case ch_motor_right:
926 break;
927 case ch_sensor:
929 break;
930 default:
931 break;
932 }
933 bt.send_fstring("P:%.2f,I:%.2f", pid_constants[0], pid_constants[1]);
934 bt.send_fstring("D:%.2f,T:%.2f\n", pid_constants[2], pid_constants[3]);
935 break;
936 case ch_current_usage: // C
939 break;
940 case ch_runtime: // R
941 bt.send_fstring("Runtime: %f", global_timer.read());
942 break;
943 case ch_loop_time: // X
944 bt.send_fstring("ISR: %dus", ISR_exec_time);
945 break;
946 case ch_loop_count: // Y
947 bt.send_fstring("removed feature");
948 break;
949 default:
950 bt.send_fstring("Err: No Data\n");
951 break;
952 }
953 bt.set_send_once(false);
954 }
955}
956
957
958void pc_send_data(void)
959{
960 // pc.printf("\n");
961
963
964 // pc.printf(" L | R \n");
965 // pc.printf("Duty Cycle: %6.2f | %6.2f \n", motor_left.get_duty_cycle(), motor_right.get_duty_cycle());
966 // pc.printf("Encoder Ticks: %6d | %6d \n", motor_left.get_tick_count(), motor_right.get_tick_count());
967 // pc.printf("Motor Speed (m/s): %6.4f | %6.4f \n", motor_left.get_speed(), motor_right.get_speed());
968
969 // pc.printf("\n");
970
971 // pc.printf("Cumulative Angle: %7.4f Degrees \n", buggy_status.cumulative_angle_deg);
972 // pc.printf("Distance Travelled: %7.4f Metres \n", buggy_status.distance_travelled);
973 // pc.printf("Sensor Values: \n");
974
975 // pc.printf("Calculation ISR Time: %d us / Main Loop Time: %d us / Global Time: %d us \n", ISR_exec_time, loop_exec_time, global_timer.read_us());
976
977 // pc.printf("%d, %d\n", ISR_exec_time, loop_exec_time);
978
979
981
982 // float* sens = sensor_array.get_sens_output_array();
983 // for (int i = 0; i < 6; i++)
984 // {
985 // pc.printf("%d:%6.4f,", i, sens[i]);
986 // }
987 // pc.printf("Out:%f,F:%f\n", sensor_array.get_array_output(), sensor_array.get_filtered_output());
988
989
991
992 // float** out_arr = PID_sensor.get_terms();
993 // pc.printf("%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f\n",
994 // *out_arr[0], //= time_index
995 // *out_arr[1], //= set_point
996 // *out_arr[2], //= measurement
997 // *out_arr[3], //= error
998 // *out_arr[4], //= propotional
999 // *out_arr[5], //= integrator
1000 // *out_arr[6], //= differentiator
1001 // *out_arr[7]); //= output
1002
1003 // pc.printf("err:%.2f,", *out_arr[3]);
1004 // pc.printf("out:%.2f\n", *out_arr[7]);
1005
1006 // pc.printf("O: %.2f | %.4f\n", *out_arr[7], *out_arr[3]);
1007 // pc.printf("O: %.2f | %.4f\n", *out_arr2[7], *out_arr2[3]);
1008 // pc.printf("D: %.2f | %.2f\n", motor_left.get_duty_cycle(), motor_right.get_duty_cycle());
1009 // pc.printf("S: %.4f | %.4f\n", motor_left.get_speed(), motor_right.get_speed());
1010}
PID class library.
BLE HM-10 Library.
BLE HM-10 Interface Class.
Definition bluetooth.h:26
bool data_recieved_complete(void)
Returns true if incoming data is fully recieved.
Definition bluetooth.cpp:35
void set_send_once(bool status)
Set the send once property to the bool value passed.
char * get_rx_buffer(void)
returns the raw data recieved as a character array
Definition bluetooth.cpp:93
void reset_rx_buffer(void)
Resets the recieved data buffer for new incoming data.
Definition bluetooth.cpp:42
bool is_send_once(void)
returns true if send once is enabled
void send_fstring(const char *format,...)
Sends formatted string to the bluetooth.
Definition bluetooth.cpp:67
void set_continous(bool status)
Set the continous property to the bool value passed.
bool is_continous(void)
returns true if continous update is enabled
Definition bluetooth.cpp:87
bool is_ready(void)
returns true if bluetooth module is ready
Definition bluetooth.cpp:80
Contains all the functionality of the ESP motor driver board.
float get_voltage(void)
returns the actual voltage value
void update_measurements(void)
updates the measurements of voltage and current values
void disable(void)
causes the enable pin to be set to LOW
void set_enable(bool expression)
enable pin will be set to the boolean value in the paremeter
float get_current(void)
returns the actual curent value
Represents a motor with integrated quadrature encoder for the buggy.
Definition motor.h:22
float get_speed(void)
Get the tangential speed of the wheel.
Definition motor.cpp:127
float get_filtered_speed(void)
Get the low-pass filtered speed of the wheel.
Definition motor.cpp:132
void set_duty_cycle(float DutyCycle)
Set the duty cycle of the motor.
Definition motor.cpp:70
void update(void)
Calculate and update all the speed variables.
Definition motor.cpp:33
float get_duty_cycle()
Get the duty cycle of the motor.
Definition motor.cpp:107
void reset(void)
Reset the encoder tick count.
Definition motor.cpp:56
int get_tick_count(void)
Get the cumulative tick count.
Definition motor.cpp:112
PID Control Class.
Definition PID.h:21
float ** get_terms(void)
Get the PID terms.
Definition PID.cpp:144
float get_output(void)
Get the output of the PID controller.
Definition PID.cpp:108
void set_tau(float tau_)
Definition PID.cpp:139
float * get_constants(void)
Definition PID.cpp:149
void set_constants(float kp_, float ki_, float kd_)
Takes in the 3 parameters to set the PID coefficients values.
Definition PID.cpp:132
void update(float set_point, float measurement)
Updates the output of PID controller based on the real time measurement.
Definition PID.cpp:57
void reset()
Reset the PID controller.
Definition PID.cpp:114
Represents an array of sensors with corresponding LEDs for detection.
void calibrate_sensors(void)
float get_filtered_output(void)
void update(void)
Updates the sensor array.
float * get_calibration_constants(void)
bool is_line_detected(void)
Checks if a line is detected (in the last update).
void set_all_led_on(bool status)
Sets the status of all LEDs.
contains all the constant variables for easy access from one file
#define SQUARE_DISTANCE
Definition constants.h:85
#define PID_M_R_KI
Definition constants.h:27
#define SENS_ANGLE_COEFF
Definition constants.h:58
#define PID_M_L_KP
Definition constants.h:21
#define CONTROL_UPDATE_PERIOD
Definition constants.h:89
#define STOP_DETECT_TIME
Definition constants.h:70
#define PID_S_MIN_OUT
Definition constants.h:31
#define PID_A_KP
Definition constants.h:49
#define PID_M_MAX_INT
Definition constants.h:18
#define LINE_FOLLOW_STOP_DISTANCE
Definition constants.h:64
#define PID_M_R_KD
Definition constants.h:28
#define UTURN_ANGLE
Definition constants.h:65
#define SLOW_ACCEL_TIME
Definition constants.h:67
#define PID_A_KI
Definition constants.h:50
#define PID_A_MIN_INT
Definition constants.h:47
#define SQUARE_VELOCITY_SET
Definition constants.h:82
#define LINE_FOLLOW_VELOCITY_UTURN
Definition constants.h:63
#define PID_A_MAX_OUT
Definition constants.h:46
#define PID_M_MIN_OUT
Definition constants.h:15
#define SLOW_ACCEL_DIVIDER
Definition constants.h:68
#define PID_M_MAX_OUT
Definition constants.h:16
#define SQUARE_TURNING_LEFT_ANGLE
Definition constants.h:84
#define SQUARE_TURNING_RIGHT_ANGLE
Definition constants.h:83
#define CONTROL_UPDATE_RATE
Definition constants.h:88
#define PID_A_MAX_INT
Definition constants.h:48
#define PID_S_MAX_INT
Definition constants.h:34
#define SENSOR_UPDATE_PERIOD
Definition constants.h:94
#define PID_S_MAX_OUT
Definition constants.h:32
#define PID_M_MIN_INT
Definition constants.h:17
#define PID_S_KD
Definition constants.h:39
#define SERIAL_UPDATE_PERIOD
Definition constants.h:98
#define PID_S_TAU
Definition constants.h:37
#define PID_A_MIN_OUT
Definition constants.h:45
#define PID_A_KD
Definition constants.h:51
#define PID_S_MIN_INT
Definition constants.h:33
#define PID_M_TAU
Definition constants.h:14
#define LINE_FOLLOW_VELOCITY
Definition constants.h:62
#define SENS_DETECT_RANGE
Definition constants.h:59
#define CONTROL_UPDATE_PERIOD_US
Definition constants.h:90
#define PID_S_KI
Definition constants.h:35
#define PID_A_TAU
Definition constants.h:44
#define SENSOR_UPDATE_PERIOD_US
Definition constants.h:95
#define PID_M_R_KP
Definition constants.h:26
#define PID_M_L_KD
Definition constants.h:23
#define PID_S_KP
Definition constants.h:38
#define PID_M_L_KI
Definition constants.h:22
#define SENS_SAMPLE_COUNT
Definition constants.h:57
int loop_exec_time
Definition main.cpp:120
short int data_log[LOG_SIZE][5]
Definition main.cpp:123
Buggy_status buggy_status
Definition main.cpp:132
char bt_obj_sent
Definition main.cpp:126
float * pid_constants
Definition main.cpp:127
char bt_data_sent
Definition main.cpp:125
void bt_send_data(void)
Send data to the bt module.
Definition main.cpp:863
PID PID_angle(PID_A_KP, PID_A_KI, PID_A_KD, PID_A_TAU, PID_A_MIN_OUT, PID_A_MAX_OUT, PID_A_MIN_INT, PID_A_MAX_INT, CONTROL_UPDATE_PERIOD)
void reset_everything(void)
Reset all buggy values and all objects variables.
Definition main.cpp:690
PID PID_sensor(PID_S_KP, PID_S_KI, PID_S_KD, PID_S_TAU, PID_S_MIN_OUT, PID_S_MAX_OUT, PID_S_MIN_INT, PID_S_MAX_INT, SENSOR_UPDATE_PERIOD)
volatile bool pc_serial_update
Definition main.cpp:117
void pc_send_data(void)
Send data to the pc.
Definition main.cpp:958
Bluetooth bt(BT_TX_PIN, BT_RX_PIN, BT_BAUD_RATE)
void stop_motors(void)
Set pwm dc to 0 for both motors.
Definition main.cpp:673
void sensor_update_ISR()
Definition main.cpp:648
Buggy_modes
Definition main.cpp:67
@ line_test
Definition main.cpp:71
@ stop_detect_line
Definition main.cpp:80
@ square_mode
Definition main.cpp:68
@ PID_test
Definition main.cpp:70
@ task_test
Definition main.cpp:73
@ task_test_inactive
Definition main.cpp:74
@ static_tracking
Definition main.cpp:78
@ inactive
Definition main.cpp:75
@ active_stop
Definition main.cpp:76
@ line_follow_auto
Definition main.cpp:79
@ calibration
Definition main.cpp:81
@ straight_test
Definition main.cpp:69
@ line_follow
Definition main.cpp:72
@ uturn
Definition main.cpp:77
Buggy_modes buggy_mode
Definition main.cpp:130
Ticker control_ticker
Definition main.cpp:141
DigitalOut LED(LED_PIN)
void serial_update_ISR(void)
ISR to update flag to send data to pc/bt in main()
Definition main.cpp:856
void update_buggy_status(int tick_count_left, int tick_count_right)
Update buggy status variables.
Definition main.cpp:680
Serial pc(USBTX, USBRX, 115200)
Ticker sensor_ticker
Definition main.cpp:143
Buggy_modes prev_buggy_mode
Definition main.cpp:131
Motor motor_right(MOTORR_PWM_PIN, MOTORR_DIRECTION_PIN, MOTORR_BIPOLAR_PIN, MOTORR_CHA_PIN, MOTORR_CHB_PIN, PULSE_PER_REV, MOTOR_PWM_FREQ, CONTROL_UPDATE_RATE, LP_SPEED_A0, LP_SPEED_B0, LP_SPEED_B1, WHEEL_RADIUS)
PID PID_motor_right(PID_M_R_KP, PID_M_R_KI, PID_M_R_KD, PID_M_TAU, PID_M_MIN_OUT, PID_M_MAX_OUT, PID_M_MIN_INT, PID_M_MAX_INT, CONTROL_UPDATE_PERIOD)
int log_index
Definition main.cpp:124
volatile float lf_velocity
Definition main.cpp:134
void slow_accel_ISR(void)
Definition main.cpp:655
void control_update_ISR(void)
ISR updating the control algorithm.
Definition main.cpp:530
MotorDriverBoard driver_board(DRIVER_ENABLE_PIN, DRIVER_MONITOR_PIN)
int ISR_exec_time
Definition main.cpp:119
Motor motor_left(MOTORL_PWM_PIN, MOTORL_DIRECTION_PIN, MOTORL_BIPOLAR_PIN, MOTORL_CHA_PIN, MOTORL_CHB_PIN, PULSE_PER_REV, MOTOR_PWM_FREQ, CONTROL_UPDATE_RATE, LP_SPEED_A0, LP_SPEED_B0, LP_SPEED_B1, WHEEL_RADIUS)
void stop_detect_ISR(void)
Definition main.cpp:664
Ticker serial_ticker
Definition main.cpp:142
float * cal_constants
Definition main.cpp:128
volatile bool bt_serial_update
Definition main.cpp:118
bool bt_parse_rx(char *rx_buffer)
Parse recieved bluetooth data.
Definition main.cpp:703
Bt_cmd_chars
Definition main.cpp:23
@ ch_execute
Definition main.cpp:25
@ ch_speed
Definition main.cpp:48
@ ch_stop
Definition main.cpp:31
@ ch_calibrate
Definition main.cpp:43
@ ch_square_test
Definition main.cpp:37
@ ch_line_follow_auto
Definition main.cpp:42
@ ch_sensor
Definition main.cpp:60
@ ch_continous
Definition main.cpp:28
@ ch_get
Definition main.cpp:26
@ ch_pwm_duty
Definition main.cpp:46
@ ch_motor_both
Definition main.cpp:59
@ ch_straight_test
Definition main.cpp:36
@ ch_static_tracking
Definition main.cpp:41
@ ch_ticks_cumulative
Definition main.cpp:47
@ ch_tau_PID
Definition main.cpp:50
@ ch_motor_left
Definition main.cpp:57
@ ch_motor_pwm_test
Definition main.cpp:35
@ ch_active_stop
Definition main.cpp:32
@ ch_loop_count
Definition main.cpp:54
@ ch_current_usage
Definition main.cpp:51
@ ch_loop_time
Definition main.cpp:53
@ ch_toggle_led_test
Definition main.cpp:39
@ ch_no_obj
Definition main.cpp:61
@ ch_line_follow
Definition main.cpp:40
@ ch_encoder_test
Definition main.cpp:34
@ ch_PID_test
Definition main.cpp:38
@ ch_gains_PID
Definition main.cpp:49
@ ch_uturn
Definition main.cpp:33
@ ch_set
Definition main.cpp:27
@ ch_motor_right
Definition main.cpp:58
@ ch_runtime
Definition main.cpp:52
SensorArray sensor_array(SENSOR0_IN_PIN, SENSOR1_IN_PIN, SENSOR2_IN_PIN, SENSOR3_IN_PIN, SENSOR4_IN_PIN, SENSOR5_IN_PIN, SENSOR0_OUT_PIN, SENSOR1_OUT_PIN, SENSOR2_OUT_PIN, SENSOR3_OUT_PIN, SENSOR4_OUT_PIN, SENSOR5_OUT_PIN, SENS_SAMPLE_COUNT, SENS_DETECT_RANGE, SENS_ANGLE_COEFF)
Timer global_timer
Definition main.cpp:140
float bt_float_data[3]
Definition main.cpp:122
int main()
Definition main.cpp:173
Timeout logic_timout
Definition main.cpp:144
PID PID_motor_left(PID_M_L_KP, PID_M_L_KI, PID_M_L_KD, PID_M_TAU, PID_M_MIN_OUT, PID_M_MAX_OUT, PID_M_MIN_INT, PID_M_MAX_INT, CONTROL_UPDATE_PERIOD)
Motor and Encoder class library.
ESP Motor Driver Board Interface library
MCU pin assigments in one file for easy modification.
#define BT_TX_PIN
#define MOTORR_DIRECTION_PIN
#define SENSOR3_IN_PIN
#define SENSOR1_OUT_PIN
#define SENSOR3_OUT_PIN
#define SENSOR1_IN_PIN
#define SENSOR4_IN_PIN
#define MOTORR_BIPOLAR_PIN
#define MOTORL_CHB_PIN
#define BT_RX_PIN
#define SENSOR2_IN_PIN
#define SENSOR0_IN_PIN
#define SENSOR5_IN_PIN
#define MOTORR_CHA_PIN
#define MOTORL_PWM_PIN
#define SENSOR0_OUT_PIN
#define SENSOR4_OUT_PIN
#define MOTORR_CHB_PIN
#define MOTORL_CHA_PIN
#define LED_PIN
#define MOTORL_DIRECTION_PIN
#define SENSOR5_OUT_PIN
#define SENSOR2_OUT_PIN
#define MOTORL_BIPOLAR_PIN
#define MOTORR_PWM_PIN
#define DRIVER_ENABLE_PIN
#define DRIVER_MONITOR_PIN
Sensor Array PCB interface class library
Represents the status of a buggy.
Definition main.cpp:92
float left_set_speed
The set speed of the left wheel.
Definition main.cpp:96
float set_velocity
The set velocity for the buggy.
Definition main.cpp:93
float right_set_speed
The set speed of the right wheel.
Definition main.cpp:97
float accel_start_angle
Definition main.cpp:106
float sq_set_distance
The set distance for the square task.
Definition main.cpp:111
float lf_line_last_seen
The position of the last seen line by the left front sensor.
Definition main.cpp:102
float sq_set_angle
The set angle for the square task.
Definition main.cpp:110
float set_angle
The set angle for the buggy.
Definition main.cpp:94
float distance_travelled
The distance travelled by the buggy.
Definition main.cpp:100
float cumulative_angle_deg
The cumulative angle (in degrees) traveled by the buggy.
Definition main.cpp:99
bool is_accelerating
Definition main.cpp:107
int sq_stage
The current stage of the square task.
Definition main.cpp:112
float accel_start_distance
Definition main.cpp:105