]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/robomon/Robot.cpp
robomon: Implement motor simulation
[eurobot/public.git] / src / robomon / Robot.cpp
1 /*
2  * Robot.cpp                    07/10/31
3  *
4  * Draw and move a robot on the playground.
5  *
6  * Copyright: (c) 2007 CTU Dragons
7  *            CTU FEE - Department of Control Engineering
8  * Authors: Martin Zidek, Michal Sojka, Tran Duy Khanh
9  * License: GNU GPL v.2
10  */
11
12 #include <math.h>
13 #include <QPen>
14
15 #include "PlaygroundScene.h"
16 #include "Robot.h"
17 #include <robodim.h>
18 #include <actuators.h>
19 #include <iostream>
20
21 Robot::Robot(const QString &text, const QPen &pen, const QBrush &brush) :
22         QObject(), QGraphicsItem()
23 {
24         this->text = text;
25         this->pen = pen;
26         this->brush = brush;
27         setVisible(false);
28         setVidle(0);
29         moveRobot(1, 1, 0);
30 }
31
32 Robot::~Robot()
33 {
34 }
35
36 QRectF Robot::boundingRect() const
37 {
38         return QRectF(0, -ROBOT_VIDLE_LENGTH_M*1000,
39                       ROBOT_WIDTH_MM, ROBOT_AXIS_TO_BACK_MM + ROBOT_AXIS_TO_FRONT_MM + ROBOT_VIDLE_LENGTH_M*1000);
40 }
41
42 void Robot::paint(QPainter *painter, 
43                 const QStyleOptionGraphicsItem *option, QWidget *widget)
44 {  
45         Q_UNUSED(option);
46         Q_UNUSED(widget);
47         Q_UNUSED(painter);
48         
49         painter->setPen(pen);
50         painter->setBrush(brush);
51         painter->drawRect(QRectF(0, 0, ROBOT_WIDTH_MM, 
52                           ROBOT_AXIS_TO_BACK_MM + ROBOT_AXIS_TO_FRONT_MM));
53         const float xa = ROBOT_WIDTH_MM/2.0;
54         const float ya = ROBOT_AXIS_TO_BACK_MM;
55         const float yb = ROBOT_AXIS_TO_BACK_MM + ROBOT_AXIS_TO_FRONT_MM*0.8;
56         const float yc = ROBOT_AXIS_TO_BACK_MM + ROBOT_AXIS_TO_FRONT_MM*0.4;
57         const float xd = ROBOT_WIDTH_MM/4.0;
58         QLineF arrow[] = { 
59                 QLineF(xa, ya, xa, yb),
60                 QLineF(xa, yb, xa-xd, yc),
61                 QLineF(xa, yb, xa+xd, yc)};
62         painter->drawLines(arrow, 3);
63         if (vidle != 0) {
64                 const float vidle_angle = (float)(vidle - VIDLE_DOWN)/(VIDLE_UP-VIDLE_DOWN)*M_PI/2.0;
65                 const float y = -ROBOT_VIDLE_LENGTH_M*1000*cos(vidle_angle);
66                 QLineF vidle[] = { 
67                         QLineF(0*ROBOT_WIDTH_MM/3, 0, 0*ROBOT_WIDTH_MM/3, y),
68                         QLineF(1*ROBOT_WIDTH_MM/3, 0, 1*ROBOT_WIDTH_MM/3, y),
69                         QLineF(2*ROBOT_WIDTH_MM/3, 0, 2*ROBOT_WIDTH_MM/3, y),
70                         QLineF(3*ROBOT_WIDTH_MM/3, 0, 3*ROBOT_WIDTH_MM/3, y)};
71                 painter->drawLines(vidle, 4);
72         }
73         
74         painter->setFont(QFont("Arial", 40, 0, 0));
75         QTransform mirror;
76         mirror.scale(1,-1);     
77         painter->setTransform(mirror, true);
78         painter->drawText(QRect(0, 0, ROBOT_WIDTH_MM, -ROBOT_AXIS_TO_BACK_MM),
79                           Qt::AlignCenter, text);
80 }
81
82 void Robot::moveRobot(double x, double y, double phi)
83 {
84         QPointF pos(x, y);
85
86         pos = PlaygroundScene::world2scene(pos);
87
88         setPos(pos);
89         setTransform(QTransform().rotateRadians(phi-M_PI/2.0).translate(-ROBOT_WIDTH_MM/2.0, -ROBOT_AXIS_TO_BACK_MM));
90 }
91
92 void Robot::mySetVisible(bool show)
93 {
94         setVisible(show);
95 }
96
97 void Robot::setVidle(int value)
98 {
99         QRectF r = boundingRect();
100         //r.setBottom(0);
101         vidle = value;
102         update(r);
103 }