Revision e404e6c0 devices/DiWheelDrive/userthread.cpp
devices/DiWheelDrive/userthread.cpp | ||
---|---|---|
1 |
// #include "userthread.hpp" |
|
2 | 1 |
#include "global.hpp" |
3 | 2 |
#include <cmath> |
4 | 3 |
#include "linefollow.hpp" |
5 | 4 |
#include "userthread.hpp" |
6 |
// #include <cmath>
|
|
7 |
// #include "global.hpp" |
|
5 |
#include "amiro_map.hpp"
|
|
6 |
|
|
8 | 7 |
using namespace amiro; |
9 | 8 |
|
10 | 9 |
extern Global global; |
... | ... | |
248 | 247 |
return devCor.currentDeviation; |
249 | 248 |
} |
250 | 249 |
|
250 |
void setAttributes(uint8_t (&map)[MAX_NODES][NODE_ATTRIBUTES], |
|
251 |
uint8_t id, uint8_t l, uint8_t r, uint8_t att) { |
|
252 |
map[id][0] = l; |
|
253 |
map[id][1] = r; |
|
254 |
map[id][2] = att; |
|
255 |
} |
|
251 | 256 |
|
252 | 257 |
UserThread::UserThread() : |
253 | 258 |
chibios_rt::BaseStaticThread<USER_THREAD_STACK_SIZE>() |
... | ... | |
272 | 277 |
running = false; |
273 | 278 |
LineFollowStrategy lStrategy = LineFollowStrategy::EDGE_RIGHT; |
274 | 279 |
LineFollow lf(&global); |
280 |
AmiroMap map(&global); |
|
275 | 281 |
/* |
276 | 282 |
* LOOP |
277 | 283 |
*/ |
... | ... | |
298 | 304 |
global.msgReceived = false; |
299 | 305 |
// running = true; |
300 | 306 |
switch(global.lfStrategy){ |
301 |
case msg_content::MSG_START:
|
|
302 |
newState = states::CALIBRATION_CHECK;
|
|
307 |
case msg_content::MSG_START: |
|
308 |
newState = states::CALIBRATION_CHECK; |
|
303 | 309 |
break; |
304 |
case msg_content::MSG_STOP:
|
|
305 |
newState = states::IDLE;
|
|
310 |
case msg_content::MSG_STOP: |
|
311 |
newState = states::IDLE; |
|
306 | 312 |
break; |
307 |
case msg_content::MSG_EDGE_RIGHT:
|
|
308 |
// newState = states::FOLLOW_LINE;
|
|
309 |
lStrategy = LineFollowStrategy::EDGE_RIGHT;
|
|
313 |
case msg_content::MSG_EDGE_RIGHT: |
|
314 |
// newState = states::FOLLOW_LINE; |
|
315 |
lStrategy = LineFollowStrategy::EDGE_RIGHT; |
|
310 | 316 |
break; |
311 |
case msg_content::MSG_EDGE_LEFT:
|
|
312 |
// newState = states::FOLLOW_LINE;
|
|
313 |
lStrategy = LineFollowStrategy::EDGE_LEFT;
|
|
317 |
case msg_content::MSG_EDGE_LEFT: |
|
318 |
// newState = states::FOLLOW_LINE; |
|
319 |
lStrategy = LineFollowStrategy::EDGE_LEFT; |
|
314 | 320 |
break; |
315 |
case msg_content::MSG_FUZZY:
|
|
316 |
// newState = states::FOLLOW_LINE;
|
|
317 |
lStrategy = LineFollowStrategy::FUZZY;
|
|
321 |
case msg_content::MSG_FUZZY: |
|
322 |
// newState = states::FOLLOW_LINE; |
|
323 |
lStrategy = LineFollowStrategy::FUZZY; |
|
318 | 324 |
break; |
319 |
case msg_content::MSG_DOCK:
|
|
320 |
newState = states::DETECT_STATION;
|
|
325 |
case msg_content::MSG_DOCK: |
|
326 |
newState = states::DETECT_STATION; |
|
321 | 327 |
break; |
322 |
case msg_content::MSG_UNDOCK: |
|
323 |
newState = states::RELEASE; |
|
328 |
case msg_content::MSG_UNDOCK: |
|
329 |
newState = states::RELEASE; |
|
330 |
break; |
|
331 |
case msg_content::MSG_CHARGE: |
|
332 |
newState = states::CHARGING; |
|
324 | 333 |
break; |
325 |
case msg_content::MSG_CHARGE:
|
|
326 |
newState = states::CHARGING;
|
|
334 |
case msg_content::MSG_RESET_ODOMETRY:
|
|
335 |
global.odometry.resetPosition();
|
|
327 | 336 |
break; |
328 |
case msg_content::MSG_RESET_ODOMETRY: |
|
329 |
global.odometry.resetPosition(); |
|
337 |
case msg_content::MSG_CALIBRATE_BLACK: |
|
338 |
proxCalib.calibrateBlack = true; |
|
339 |
// global.odometry.resetPosition(); |
|
340 |
newState = states::CALIBRATION; |
|
330 | 341 |
break; |
331 |
case msg_content::MSG_CALIBRATE_BLACK: |
|
332 |
proxCalib.calibrateBlack = true; |
|
333 |
// global.odometry.resetPosition(); |
|
334 |
newState = states::CALIBRATION; |
|
342 |
case msg_content::MSG_CALIBRATE_WHITE: |
|
343 |
proxCalib.calibrateBlack = false; |
|
344 |
newState = states::CALIBRATION; |
|
335 | 345 |
break; |
336 |
case msg_content::MSG_CALIBRATE_WHITE: |
|
337 |
proxCalib.calibrateBlack = false; |
|
338 |
newState = states::CALIBRATION; |
|
346 |
case msg_content::MSG_TEST_MAP_STATE: |
|
347 |
newState = states::TEST_MAP_STATE; |
|
339 | 348 |
break; |
340 |
default: |
|
341 |
newState = states::IDLE; |
|
349 |
|
|
350 |
default: |
|
351 |
newState = states::IDLE; |
|
342 | 352 |
break; |
343 | 353 |
} |
344 | 354 |
} |
... | ... | |
490 | 500 |
break; |
491 | 501 |
} |
492 | 502 |
// --------------------------------------- |
493 |
case states::DETECT_STATION:
|
|
503 |
case states::DETECT_STATION: |
|
494 | 504 |
|
495 | 505 |
if (global.forwardSpeed != DETECTION_SPEED){ |
496 | 506 |
global.forwardSpeed = DETECTION_SPEED; |
... | ... | |
516 | 526 |
} |
517 | 527 |
break; |
518 | 528 |
// --------------------------------------- |
519 |
case states::CORRECT_POSITIONING:
|
|
529 |
case states::CORRECT_POSITIONING: |
|
520 | 530 |
if (global.forwardSpeed != CHARGING_SPEED){ |
521 | 531 |
global.forwardSpeed = CHARGING_SPEED; |
522 | 532 |
} |
... | ... | |
535 | 545 |
} |
536 | 546 |
break; |
537 | 547 |
// --------------------------------------- |
538 |
case states::REVERSE:
|
|
548 |
case states::REVERSE: |
|
539 | 549 |
if(lf.getStrategy() != LineFollowStrategy::REVERSE){ |
540 | 550 |
lf.setStrategy(LineFollowStrategy::REVERSE); |
541 | 551 |
} |
... | ... | |
584 | 594 |
// setRpmSpeed(rpmSpeed); |
585 | 595 |
break; |
586 | 596 |
// --------------------------------------- |
587 |
case states::DEVIATION_CORRECTION:
|
|
597 |
case states::DEVIATION_CORRECTION: |
|
588 | 598 |
// if(lf.getStrategy() != LineFollowStrategy::REVERSE){ |
589 | 599 |
// lf.setStrategy(LineFollowStrategy::REVERSE); |
590 | 600 |
// } |
... | ... | |
628 | 638 |
// } |
629 | 639 |
break; |
630 | 640 |
// --------------------------------------- |
631 |
case states::PUSH_BACK:
|
|
641 |
case states::PUSH_BACK: |
|
632 | 642 |
if(lf.getStrategy() != LineFollowStrategy::REVERSE){ |
633 | 643 |
lf.setStrategy(LineFollowStrategy::REVERSE); |
634 | 644 |
} |
... | ... | |
642 | 652 |
} |
643 | 653 |
break; |
644 | 654 |
// --------------------------------------- |
645 |
case states::CHECK_POSITIONING:
|
|
655 |
case states::CHECK_POSITIONING: |
|
646 | 656 |
setRpmSpeed(stop); |
647 | 657 |
checkForMotion(); |
648 | 658 |
if(checkDockingSuccess()){ |
... | ... | |
656 | 666 |
} |
657 | 667 |
break; |
658 | 668 |
// --------------------------------------- |
659 |
case states::CHECK_VOLTAGE:
|
|
669 |
case states::CHECK_VOLTAGE: |
|
660 | 670 |
if(!checkPinEnabled()){ |
661 | 671 |
global.robot.requestCharging(1); |
662 | 672 |
} else { |
... | ... | |
684 | 694 |
} |
685 | 695 |
break; |
686 | 696 |
// --------------------------------------- |
687 |
case states::RELEASE_TO_CORRECT:
|
|
697 |
case states::RELEASE_TO_CORRECT: |
|
688 | 698 |
|
689 | 699 |
global.distcontrol.setTargetPosition(0, ROTATION_20, ROTATION_DURATION); |
690 | 700 |
checkForMotion(); |
... | ... | |
700 | 710 |
newState = states::CORRECT_POSITIONING; |
701 | 711 |
break; |
702 | 712 |
// --------------------------------------- |
703 |
case states::CHARGING:
|
|
713 |
case states::CHARGING: |
|
704 | 714 |
global.motorcontrol.setMotorEnable(false); |
705 | 715 |
utCount.errorCount = 0; |
706 | 716 |
// Formulate Request to enable charging |
... | ... | |
712 | 722 |
} |
713 | 723 |
break; |
714 | 724 |
// --------------------------------------- |
715 |
case states::RELEASE:
|
|
725 |
case states::RELEASE: |
|
716 | 726 |
if (global.forwardSpeed != DETECTION_SPEED){ |
717 | 727 |
global.rpmForward[0] = DETECTION_SPEED; |
718 | 728 |
} |
... | ... | |
742 | 752 |
// lightAllLeds(Color::BLACK); |
743 | 753 |
break; |
744 | 754 |
// --------------------------------------- |
745 |
case states::DOCKING_ERROR:
|
|
746 |
newState = states::RELEASE;
|
|
755 |
case states::DOCKING_ERROR: |
|
756 |
newState = states::RELEASE; |
|
747 | 757 |
break; |
748 | 758 |
// --------------------------------------- |
749 |
case states::REVERSE_TIMEOUT_ERROR:
|
|
750 |
newState = states::IDLE;
|
|
759 |
case states::REVERSE_TIMEOUT_ERROR: |
|
760 |
newState = states::IDLE; |
|
751 | 761 |
break; |
752 | 762 |
// --------------------------------------- |
753 |
case states::CALIBRATION_ERROR:
|
|
754 |
newState = states::IDLE;
|
|
763 |
case states::CALIBRATION_ERROR: |
|
764 |
newState = states::IDLE; |
|
755 | 765 |
break; |
756 | 766 |
// --------------------------------------- |
757 |
case states::WHITE_DETECTION_ERROR:
|
|
758 |
newState = states::IDLE;
|
|
767 |
case states::WHITE_DETECTION_ERROR: |
|
768 |
newState = states::IDLE; |
|
759 | 769 |
break; |
760 | 770 |
// --------------------------------------- |
761 |
case states::PROXY_DETECTION_ERROR:
|
|
762 |
newState = states::IDLE;
|
|
771 |
case states::PROXY_DETECTION_ERROR: |
|
772 |
newState = states::IDLE; |
|
763 | 773 |
break; |
764 | 774 |
// --------------------------------------- |
765 |
case states::NO_CHARGING_POWER_ERROR:
|
|
766 |
newState = states::IDLE;
|
|
775 |
case states::NO_CHARGING_POWER_ERROR: |
|
776 |
newState = states::IDLE; |
|
767 | 777 |
break; |
768 | 778 |
// --------------------------------------- |
769 |
case states::UNKNOWN_STATE_ERROR:
|
|
779 |
case states::UNKNOWN_STATE_ERROR: |
|
770 | 780 |
newState = states::IDLE; |
771 | 781 |
break; |
772 | 782 |
// --------------------------------------- |
773 |
default: |
|
774 |
newState = states::UNKNOWN_STATE_ERROR; |
|
775 |
break; |
|
776 |
} |
|
777 |
if (currentState != newState){ |
|
778 |
chprintf((BaseSequentialStream*)&global.sercanmux1, "Transmit state %d\n", newState); |
|
779 |
global.robot.transmitState(newState); |
|
780 |
if (newState == states::IDLE) |
|
781 |
{global.stateTracker[states::IDLE] += 1;} |
|
782 |
else if (newState == states::FOLLOW_LINE) |
|
783 |
{global.stateTracker[states::FOLLOW_LINE] += 1;} |
|
784 |
else if (newState == states::DETECT_STATION) |
|
785 |
{global.stateTracker[states::DETECT_STATION] += 1;} |
|
786 |
else if (newState == states::REVERSE) |
|
787 |
{global.stateTracker[states::REVERSE] += 1;} |
|
788 |
else if (newState == states::PUSH_BACK) |
|
789 |
{global.stateTracker[states::PUSH_BACK] += 1;} |
|
790 |
else if (newState == states::CHECK_POSITIONING) |
|
791 |
{global.stateTracker[states::CHECK_POSITIONING] += 1;} |
|
792 |
else if (newState == states::CHECK_VOLTAGE) |
|
793 |
{global.stateTracker[states::CHECK_VOLTAGE] += 1;} |
|
794 |
else if (newState == states::CHARGING) |
|
795 |
{global.stateTracker[states::CHARGING] += 1;} |
|
796 |
else if (newState == states::RELEASE) |
|
797 |
{global.stateTracker[states::RELEASE] += 1;} |
|
798 |
else if (newState == states::RELEASE_TO_CORRECT) |
|
799 |
{global.stateTracker[states::RELEASE_TO_CORRECT] += 1;} |
|
800 |
else if (newState == states::CORRECT_POSITIONING) |
|
801 |
{global.stateTracker[states::CORRECT_POSITIONING] += 1;} |
|
802 |
else if (newState == states::TURN) |
|
803 |
{global.stateTracker[states::TURN] += 1;} |
|
804 |
else if (newState == states::INACTIVE) |
|
805 |
{global.stateTracker[states::INACTIVE] += 1;} |
|
806 |
else if (newState == states::CALIBRATION) |
|
807 |
{global.stateTracker[states::CALIBRATION] += 1;} |
|
808 |
else if (newState == states::CALIBRATION_CHECK) |
|
809 |
{global.stateTracker[states::CALIBRATION_CHECK] += 1;} |
|
810 |
else if (newState == states::DEVIATION_CORRECTION) |
|
811 |
{global.stateTracker[states::DEVIATION_CORRECTION] += 1;} |
|
812 |
|
|
813 |
else if (newState == states::DOCKING_ERROR){global.stateTracker[16+(-states::DOCKING_ERROR)] += 1;} |
|
814 |
else if (newState == states::REVERSE_TIMEOUT_ERROR){global.stateTracker[16+(-states::REVERSE_TIMEOUT_ERROR)] += 1;} |
|
815 |
else if (newState == states::CALIBRATION_ERROR){global.stateTracker[16+(-states::CALIBRATION_ERROR)] += 1;} |
|
816 |
else if (newState == states::WHITE_DETECTION_ERROR){global.stateTracker[16+(-states::WHITE_DETECTION_ERROR)] += 1;} |
|
817 |
else if (newState == states::PROXY_DETECTION_ERROR){global.stateTracker[16+(-states::PROXY_DETECTION_ERROR)] += 1;} |
|
818 |
else if (newState == states::NO_CHARGING_POWER_ERROR){global.stateTracker[16+(-states::NO_CHARGING_POWER_ERROR)] += 1;} |
|
819 |
else if (newState == states::UNKNOWN_STATE_ERROR){global.stateTracker[16+(-states::UNKNOWN_STATE_ERROR)] += 1;} |
|
783 |
case states::TEST_MAP_STATE:{ |
|
784 |
/* Test suit for amiro map */ |
|
785 |
|
|
786 |
|
|
787 |
setAttributes(global.testmap, 0, 1, 2, 1); |
|
788 |
setAttributes(global.testmap, 1, 2, 2, 0); |
|
789 |
setAttributes(global.testmap, 2, 1, 0, 0); |
|
790 |
setAttributes(global.testmap, 3, 0, 0, 0xff); |
|
791 |
// AmiroMap map = AmiroMap(&global); |
|
792 |
|
|
793 |
// // -------------------------------------------------- |
|
794 |
|
|
795 |
global.tcase = 0; |
|
796 |
map.initialize(); |
|
797 |
global.testres[global.tcase] = map.get_state()->valid; |
|
798 |
|
|
799 |
global.tcase++; // 1 |
|
800 |
setAttributes(global.testmap, 0, 1, 2, 0xff); |
|
801 |
map.initialize(); |
|
802 |
global.testres[global.tcase] = !map.get_state()->valid; |
|
803 |
|
|
804 |
global.tcase++; // 2 |
|
805 |
setAttributes(global.testmap, 0, 1, 2, 0); |
|
806 |
setAttributes(global.testmap, 2, 1, 0, 1); |
|
807 |
map.initialize(); |
|
808 |
global.testres[global.tcase] = map.get_state()->current == 2; |
|
809 |
|
|
810 |
global.tcase++; // 3 |
|
811 |
setAttributes(global.testmap, 3, 0, 0, 0); |
|
812 |
setAttributes(global.testmap, 4, 0, 0, 0xff); |
|
813 |
map.initialize(); |
|
814 |
global.testres[global.tcase] = !map.get_state()->valid; |
|
815 |
|
|
816 |
int failed = 0; |
|
817 |
int passed = 0; |
|
818 |
for (int i = 0; i <= global.tcase; i++) { |
|
819 |
if (global.testres[i]) { |
|
820 |
passed++; |
|
821 |
chprintf((BaseSequentialStream *)&global.sercanmux1, |
|
822 |
"Test %d Passed!\n", i); |
|
823 |
} else { |
|
824 |
failed++; |
|
825 |
chprintf((BaseSequentialStream *)&global.sercanmux1, |
|
826 |
"Test %d Failed\n", i); |
|
827 |
} |
|
820 | 828 |
} |
821 |
prevState = currentState; |
|
822 |
currentState = newState; |
|
823 |
// if (utCount.stateCount > CAN_TRANSMIT_STATE_THRESH){ |
|
824 |
// utCount.stateCount = 0; |
|
825 |
// // chprintf((BaseSequentialStream*)&global.sercanmux1, "Transmit state %d\n", newState); |
|
826 |
// global.robot.transmitState(currentState); |
|
827 |
// // global.robot.setOdometry(global.odometry.getPosition()); |
|
828 |
|
|
829 |
// }else{ |
|
830 |
// utCount.stateCount++; |
|
831 |
// } |
|
829 |
chprintf((BaseSequentialStream *)&global.sercanmux1, |
|
830 |
"Total: %d, Passed: %d, Failed: %d\n", global.tcase + 1, passed, |
|
831 |
failed); |
|
832 |
|
|
833 |
newState = states::IDLE; |
|
834 |
break; |
|
835 |
} |
|
836 |
// -------------------------------------------------- |
|
837 |
default: |
|
838 |
newState = states::UNKNOWN_STATE_ERROR; |
|
839 |
break; |
|
840 |
} |
|
841 |
|
|
842 |
if (currentState != newState){ |
|
843 |
chprintf((BaseSequentialStream*)&global.sercanmux1, "Transmit state %d\n", newState); |
|
844 |
global.robot.transmitState(newState); |
|
845 |
// if (newState == states::IDLE) |
|
846 |
// {global.stateTracker[states::IDLE] += 1;} |
|
847 |
// else if (newState == states::FOLLOW_LINE) |
|
848 |
// {global.stateTracker[states::FOLLOW_LINE] += 1;} |
|
849 |
// else if (newState == states::DETECT_STATION) |
|
850 |
// {global.stateTracker[states::DETECT_STATION] += 1;} |
|
851 |
// else if (newState == states::REVERSE) |
|
852 |
// {global.stateTracker[states::REVERSE] += 1;} |
|
853 |
// else if (newState == states::PUSH_BACK) |
|
854 |
// {global.stateTracker[states::PUSH_BACK] += 1;} |
|
855 |
// else if (newState == states::CHECK_POSITIONING) |
|
856 |
// {global.stateTracker[states::CHECK_POSITIONING] += 1;} |
|
857 |
// else if (newState == states::CHECK_VOLTAGE) |
|
858 |
// {global.stateTracker[states::CHECK_VOLTAGE] += 1;} |
|
859 |
// else if (newState == states::CHARGING) |
|
860 |
// {global.stateTracker[states::CHARGING] += 1;} |
|
861 |
// else if (newState == states::RELEASE) |
|
862 |
// {global.stateTracker[states::RELEASE] += 1;} |
|
863 |
// else if (newState == states::RELEASE_TO_CORRECT) |
|
864 |
// {global.stateTracker[states::RELEASE_TO_CORRECT] += 1;} |
|
865 |
// else if (newState == states::CORRECT_POSITIONING) |
|
866 |
// {global.stateTracker[states::CORRECT_POSITIONING] += 1;} |
|
867 |
// else if (newState == states::TURN) |
|
868 |
// {global.stateTracker[states::TURN] += 1;} |
|
869 |
// else if (newState == states::INACTIVE) |
|
870 |
// {global.stateTracker[states::INACTIVE] += 1;} |
|
871 |
// else if (newState == states::CALIBRATION) |
|
872 |
// {global.stateTracker[states::CALIBRATION] += 1;} |
|
873 |
// else if (newState == states::CALIBRATION_CHECK) |
|
874 |
// {global.stateTracker[states::CALIBRATION_CHECK] += 1;} |
|
875 |
// else if (newState == states::DEVIATION_CORRECTION) |
|
876 |
// {global.stateTracker[states::DEVIATION_CORRECTION] += 1;} |
|
877 |
// else if (newState == states::DOCKING_ERROR) |
|
878 |
// {global.stateTracker[16+(-states::DOCKING_ERROR)] += 1;} |
|
879 |
// else if (newState == states::REVERSE_TIMEOUT_ERROR) |
|
880 |
// {global.stateTracker[16+(-states::REVERSE_TIMEOUT_ERROR)] += 1;} |
|
881 |
// else if (newState == states::CALIBRATION_ERROR) |
|
882 |
// {global.stateTracker[16+(-states::CALIBRATION_ERROR)] += 1;} |
|
883 |
// else if (newState == states::WHITE_DETECTION_ERROR) |
|
884 |
// {global.stateTracker[16+(-states::WHITE_DETECTION_ERROR)] += 1;} |
|
885 |
// else if (newState == states::PROXY_DETECTION_ERROR) |
|
886 |
// {global.stateTracker[16+(-states::PROXY_DETECTION_ERROR)] += 1;} |
|
887 |
// else if (newState == states::NO_CHARGING_POWER_ERROR) |
|
888 |
// {global.stateTracker[16+(-states::NO_CHARGING_POWER_ERROR)] += 1;} |
|
889 |
// else if (newState == states::UNKNOWN_STATE_ERROR) |
|
890 |
// {global.stateTracker[16+(-states::UNKNOWN_STATE_ERROR)] += 1;} |
|
891 |
} |
|
892 |
prevState = currentState; |
|
893 |
currentState = newState; |
|
894 |
|
|
832 | 895 |
this->sleep(CAN::UPDATE_PERIOD); |
833 | 896 |
} |
834 | 897 |
|
Also available in: Unified diff