当前位置:   article > 正文

睿抗足球机器人Day4_足球机器人 c++代码

足球机器人 c++代码

一、一次传球(踢球)代码

(1)C++代码

 这是C++代码里面的chuanqiu2代码

  1. #include "ball.h"
  2. #include "basevision.h"
  3. #include "constants.h"
  4. #include "FilteredObject.h"
  5. #include "game_state.h"
  6. #include "historylogger.h"
  7. #include "matchstate.h"
  8. #include "maths.h"
  9. #include "PlayerTask.h"
  10. #include "referee_commands.h"
  11. #include "robot.h"
  12. #include "singleton.h"
  13. #include "util.h"
  14. #include "vector.h"
  15. #include "worldmodel.h"
  16. extern "C"_declspec(dllexport) PlayerTask player_plan(const WorldModel* model, int robot_id);
  17. PlayerTask player_plan(const WorldModel* model, int robot_id)
  18. {
  19. PlayerTask task;
  20. //传球机器人的位置朝向
  21. //获取中场机器人的向量坐标
  22. const point2f&reciver_pos = model->get_our_player_pos(2);
  23. //获取中场机器人的朝向
  24. const float& reciver_dir = model->get_our_player_dir(2);
  25. //获取球的向量坐标
  26. const point2f&ball_pos = model->get_ball_pos();
  27. //接球机器人的位置朝向
  28. //获取前锋机器人的向量坐标
  29. const point2f&kicker_pos = model->get_our_player_pos(1);
  30. //获取前锋机器人的朝向
  31. const float& kicker_dir = model->get_our_player_dir(1);
  32. //判断球是否在小车控球嘴上,从两个参数着手:1.判断ball到车的距离是否小于某个值,2.车头方向和车到球矢量角度之差值是否小于某个值
  33. bool get_ball = (ball_pos - kicker_pos).length() < get_ball_threshold - 25 && (fabs(anglemod(kicker_dir - (ball_pos - kicker_pos).angle())) < PI / 6);
  34. if (get_ball)
  35. {
  36. //执行平击踢球,力度为最大127
  37. task.kickPower = 127;
  38. //踢球开关
  39. task.needKick = true;
  40. //挑球开关
  41. task.isChipKick = false;
  42. }
  43. return task;
  44. }
 重点:
bool get_ball = (ball_pos - kicker_pos).length() < get_ball_threshold - 25 && (fabs(anglemod(kicker_dir - (ball_pos - kicker_pos).angle())) < PI / 6);

 修改这个代码的数值会让机器人在找到自己位置之后,判断是否要踢球的时候更加的精确,失误率更小

(2)lua脚本代码
  1. gPlayTable.CreatePlay{
  2. firstState = "GetBall",
  3. ["GetBall"] = {
  4. switch = function()
  5. if CIsGetBall("Kicker")then
  6. return "PassBall"
  7. end
  8. end,
  9. Kicker = task.KickerTask("chuanqiu1"),
  10. receiver = task.ReceiverTask("jieqiu")
  11. },
  12. ["PassBall"] = {
  13. switch = function()
  14. if CIsBallKick("Kicker")then
  15. return "Finish"
  16. end
  17. end,
  18. Kicker = task.KickerTask("chuanqiu2"),
  19. },
  20. name = "chuanqiu3"
  21. }
(3)C++里面的chuanqiu1和jieqiu代码

 chuanqiu1

  1. #include "ball.h"
  2. #include "basevision.h"
  3. #include "constants.h"
  4. #include "FilteredObject.h"
  5. #include "game_state.h"
  6. #include "historylogger.h"
  7. #include "matchstate.h"
  8. #include "maths.h"
  9. #include "PlayerTask.h"
  10. #include "referee_commands.h"
  11. #include "robot.h"
  12. #include "singleton.h"
  13. #include "util.h"
  14. #include "vector.h"
  15. #include "worldmodel.h"
  16. extern "C"_declspec(dllexport) PlayerTask player_plan(const WorldModel* model, int robot_id);
  17. PlayerTask player_plan(const WorldModel* model, int robot_id)
  18. {
  19. PlayerTask task;
  20. //获取对方机器人的向量坐标
  21. const point2f& kicker_pos = model->get_opp_player_pos(2);
  22. //获取对方球员的朝向
  23. const float&kicker_dir = model->get_opp_player_dir(2);
  24. //获取球的向量坐标
  25. const point2f&ball_pos = model->get_ball_pos();
  26. //这里设定face_dir是接球机器人朝向球的方向
  27. const float&face_dir = (kicker_pos - ball_pos).angle();
  28. //到达目标点朝向:队员正对球
  29. task.orientate = face_dir;
  30. //需要到达目标点的坐标=球的位置+向量偏移距离//fac_dir反方向偏移200
  31. task.target_pos = ball_pos - Maths::vector2polar(20, face_dir);
  32. return task;
  33. }

jieqiu

  1. #include "ball.h"
  2. #include "basevision.h"
  3. #include "constants.h"
  4. #include "FilteredObject.h"
  5. #include "game_state.h"
  6. #include "historylogger.h"
  7. #include "matchstate.h"
  8. #include "maths.h"
  9. #include "PlayerTask.h"
  10. #include "referee_commands.h"
  11. #include "robot.h"
  12. #include "singleton.h"
  13. #include "util.h"
  14. #include "vector.h"
  15. #include "worldmodel.h"
  16. extern "C"_declspec(dllexport) PlayerTask player_plan(const WorldModel* model, int robot_id);
  17. PlayerTask player_plan(const WorldModel* model, int robot_id)
  18. {
  19. PlayerTask task;
  20. //获取对方机器人的向量坐标
  21. const point2f& player_pos = model->get_opp_player_pos(robot_id);
  22. //获取对方球员的朝向
  23. const float&player_dir = model->get_opp_player_dir(robot_id);
  24. //获取球的向量坐标
  25. const point2f&ball_pos = model->get_ball_pos();
  26. //这里设定face_dir是接球机器人朝向球的方向
  27. const float&face_dir = (player_pos - ball_pos).angle();
  28. //到达目标点朝向:队员正对球
  29. task.orientate = face_dir;
  30. //需要到达目标点的坐标=球的位置+向量偏移距离//fac_dir反方向偏移200
  31. task.target_pos = ball_pos - Maths::vector2polar(200, face_dir);
  32. return task;
  33. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/769727
推荐阅读
相关标签
  

闽ICP备14008679号