darwin_walk_toward_target
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
darwin_walk_toward_target [2017/11/19 17:20] – yuhanghe | darwin_walk_toward_target [2017/11/20 16:12] (current) – [Motivation and Audience] yuhanghe | ||
---|---|---|---|
Line 18: | Line 18: | ||
* Familiar with handling Darwin OP 2 | * Familiar with handling Darwin OP 2 | ||
- | * Familiar with C++/C programming language | + | * Familiar with Cplusplus/C programming language |
- | * Familiar with C++/C codes on Darwin OP 2 | + | * Familiar with Cplusplus/C codes on Darwin OP 2 |
The rest of this tutorial is presented as follows: | The rest of this tutorial is presented as follows: | ||
- | * [[new_mode_demonstration# | + | * [[darwin_walk_toward_target# |
- | * [[new_mode_demonstration# | + | * [[darwin_walk_toward_target# |
- | * [[new_mode_demonstration# | + | * [[darwin_walk_toward_target# |
===== Programming ===== | ===== Programming ===== | ||
- | This tutorial will only demonstrate parts of program that needs modification to include a new mode. | + | Following a target can be accomplished through BallFollower Class. There is a [[http:// |
+ | \\ | ||
+ | This tutorial will only demonstrate parts of program that needs modification to include | ||
<code cpp | StatusCheck.h> | <code cpp | StatusCheck.h> | ||
Line 39: | Line 41: | ||
MOTION, | MOTION, | ||
VISION, | VISION, | ||
- | SPRINT, //Add a new mode called "NEW_MODE" to an enumerated list of modes | + | SPRINT, //Add a new mode called "SPRINT" to an enumerated list of modes |
MAX_MODE | MAX_MODE | ||
}; | }; | ||
Line 47: | Line 49: | ||
//At the end of if statement Line 141 | //At the end of if statement Line 141 | ||
if(m_old_btn & BTN_MODE) | if(m_old_btn & BTN_MODE) | ||
- | /////////////////////////// | + | ////////////////////////////// |
- | | + | |
- | | + | |
else if(m_cur_mode == SPRINT) //adding LED and play mp3 for new mode | else if(m_cur_mode == SPRINT) //adding LED and play mp3 for new mode | ||
{ | { | ||
cm730.WriteByte(CM730:: | cm730.WriteByte(CM730:: | ||
//Signaling Darwin OP 2 which LED to turn on | //Signaling Darwin OP 2 which LED to turn on | ||
- | LinuxActionScript:: | + | LinuxActionScript:: |
//play an mp3 file (you can use any mp3 file but make sure file name matches) | //play an mp3 file (you can use any mp3 file but make sure file name matches) | ||
} | } | ||
} | } | ||
- | | + | ////////////////////////////// |
+ | ///Skipping Unmodified Codes// | ||
+ | ////////////////////////////// | ||
// Line 267 Add | // Line 267 Add | ||
else if(m_cur_mode == SPRINT) | else if(m_cur_mode == SPRINT) | ||
Line 66: | Line 70: | ||
// | // | ||
m_is_started = 1; | m_is_started = 1; | ||
- | LinuxActionScript:: | + | LinuxActionScript:: |
//play an mp3 file (you can use any mp3 file but make sure file name matches) | //play an mp3 file (you can use any mp3 file but make sure file name matches) | ||
Line 95: | Line 99: | ||
//copied status check condition from soccer mode to new mode | //copied status check condition from soccer mode to new mode | ||
//These codes initialize Darwin OP 2 for movement and walking | //These codes initialize Darwin OP 2 for movement and walking | ||
+ | </ | ||
+ | |||
+ | Modified ColorFinder class | ||
+ | \\ | ||
+ | Directory '' | ||
+ | <code cpp | ColorFinder.cpp> | ||
+ | //Creted a new function that take image pointer and int reference as parameter and return center coordinates and pixel count | ||
+ | Point2D& | ||
+ | { | ||
+ | int sum_x = 0, sum_y = 0, count = 0; | ||
+ | |||
+ | Filtering(hsv_img); | ||
+ | |||
+ | ImgProcess:: | ||
+ | ImgProcess:: | ||
+ | |||
+ | for(int y = 0; y < m_result-> | ||
+ | { | ||
+ | for(int x = 0; x < m_result-> | ||
+ | { | ||
+ | if(m_result-> | ||
+ | { | ||
+ | sum_x += x; | ||
+ | sum_y += y; | ||
+ | count++; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | if(count <= (hsv_img-> | ||
+ | { | ||
+ | m_center_point.X = -1.0; | ||
+ | m_center_point.Y = -1.0; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | m_center_point.X = (int)((double)sum_x / (double)count); | ||
+ | m_center_point.Y = (int)((double)sum_y / (double)count); | ||
+ | } | ||
+ | |||
+ | return m_center_point; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Modifed its respective header file | ||
+ | \\ | ||
+ | Directory '' | ||
+ | <code cpp | ColorFinder.h> | ||
+ | //Add declaration for new function | ||
+ | under public: | ||
+ | |||
+ | Point2D& | ||
</ | </ | ||
<code cpp | main.cpp> | <code cpp | main.cpp> | ||
- | //L317 Add | + | //inside main loop |
+ | int main() { | ||
+ | ////////////////////////////// | ||
+ | ///Skipping Unmodified Codes// | ||
+ | ////////////////////////////// | ||
+ | //Around Line 80 | ||
+ | |||
+ | ColorFinder* green_finder = new ColorFinder(120, | ||
+ | ColorFinder* blue_finder = new ColorFinder(225, | ||
+ | //created 2 new ColorFinder objects to find color green and blue | ||
+ | |||
+ | BallTracker marker_tracker = BallTracker(); | ||
+ | //created a new BallTracker object to use camera tracking | ||
+ | BallFollower marker_follower = BallFollower(); | ||
+ | //created a new BallFollower object to follow target/ | ||
+ | int _marker_found = 0; | ||
+ | //created a flag for marker | ||
+ | ////////////////////////////// | ||
+ | ///Skipping Unmodified Codes// | ||
+ | ////////////////////////////// | ||
+ | //inside while loop | ||
+ | while(1) { | ||
+ | |||
+ | int greenCount = 0, blueCount = 0; | ||
+ | //created 2 int to store filtered pixel counts | ||
+ | Point2D green, blue, center; | ||
+ | //created 3 objects of Point2D class | ||
+ | |||
+ | else if(StatusCheck:: | ||
+ | { | ||
+ | |||
+ | green = green_finder-> | ||
+ | //using the modified GetPosition function that also return filtered pixel counts | ||
+ | blue = blue_finder-> | ||
+ | //store position of green and blue color | ||
+ | |||
+ | if(green.X < 0 || blue.X < 0) | ||
+ | center.X = -1; | ||
+ | else | ||
+ | center.X = (green.X + blue.X)/ | ||
+ | if(green.Y < 0 || blue.Y < 0) | ||
+ | center.Y = -1; | ||
+ | else | ||
+ | center.Y = (green.Y + blue.Y)/ | ||
+ | //calculate center of blue and green | ||
+ | |||
+ | _marker_found = marker_tracker.SearchAndTracking(center); | ||
+ | //use camera tracking and raise the flag if marker is found | ||
+ | |||
+ | } | ||
+ | ////////////////////////////// | ||
+ | ///Skipping Unmodified Codes// | ||
+ | ////////////////////////////// | ||
+ | //around Line 350 Add | ||
case SPRINT: | case SPRINT: | ||
//implement new codes here to execute after pressing start button | //implement new codes here to execute after pressing start button | ||
+ | if(Action:: | ||
+ | { | ||
+ | Head:: | ||
+ | Walking:: | ||
+ | // | ||
+ | | ||
+ | if(Walking:: | ||
+ | Walking:: | ||
+ | //Set forward and back step length to 30cm | ||
+ | | ||
+ | Walking:: | ||
+ | //Set yaw step length to 0 | ||
+ | |||
+ | Walking:: | ||
+ | } | ||
+ | | ||
+ | if(_marker_found == 1) | ||
+ | { | ||
+ | marker_follower.Process(marker_tracker.ball_position); | ||
+ | //use BallFollower object to start following marker | ||
+ | | ||
+ | if(greenCount >= 10500 || blueCount >= 10500) | ||
+ | //pixel count can be used to approximate distance, but in this case, | ||
+ | //this stop condition is sufficient | ||
+ | { | ||
+ | Walking:: | ||
+ | fprintf(stderr, | ||
+ | while(Walking:: | ||
+ | } | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
</ | </ | ||
Line 107: | Line 252: | ||
In this demonstration, | In this demonstration, | ||
- | {{ youtube>WFf2_SOmUEI?large }} | + | {{ youtube>HaZladeEsJo?large }} |
===== Final Words ===== | ===== Final Words ===== | ||
- | This tutorial' | + | This tutorial' |
\\ | \\ | ||
\\ | \\ |
darwin_walk_toward_target.1511140859.txt.gz · Last modified: by yuhanghe