Main Page · Modules · All Classes · Class Hierarchy
MATorso.cpp
1 /*
2  * This file is part of the AiBO+ project
3  *
4  * Copyright (C) 2005-2016 Csaba Kertész (csaba.kertesz@gmail.com)
5  *
6  * AiBO+ is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * AiBO+ is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
19  *
20  */
21 
22 #include "MATorso.hpp"
23 
24 #include "MARobotState.hpp"
25 #include "MATypeRanges.hpp"
26 
27 #include <MCLog.hpp>
28 
29 #include <limits.h>
30 
32 {
33  Seconds.SetMinMax(0, INT_MAX);
34  Milliseconds.SetMinMax(0, 999);
35  Microseconds.SetMinMax(0, 999);
36 }
37 
38 
40 {
41  return Seconds*1000+Milliseconds;
42 }
43 
44 
46 {
47  All.SetMinMax(MA::StandardRangeMin, MA::StandardRangeMax);
48  Behaviors.SetMinMax(MA::StandardRangeMin, MA::StandardRangeMax);
49  Camera.SetMinMax(MA::StandardRangeMin, MA::StandardRangeMax);
50  Network.SetMinMax(MA::StandardRangeMin, MA::StandardRangeMax);
51  Audio.SetMinMax(MA::StandardRangeMin, MA::StandardRangeMax);
52 }
53 
54 
56 {
57  Link.SetMinMax(MA::StandardRangeMin, MA::StandardRangeMax);
58  Signal.SetMinMax(MA::StandardRangeMin, MA::StandardRangeMax);
59  Noise.SetMinMax(MA::StandardRangeMin, MA::StandardRangeMax);
60 }
61 
62 
63 MATorso::MATorso() : MARobotStateUpdater(MA::UpdateSensorIndicators),
64  CycleID(0),
65  CycleTime(0),
66  WritableStorage(1, 0, 1),
67  ActivatedStateTimer(MC::NoExpiration),
68  BackFPressedFactor(0, 0, 1000),
69  BackMPressedFactor(0, 0, 1000),
70  BackRPressedFactor(0, 0, 1000),
71  BackFTouched(MA::BackBtnMax),
72  BackMTouched(MA::BackBtnMax),
73  BackRTouched(MA::BackBtnMax),
74  BackStrokedFactor(0, 0, 2000),
75  BackGrabbedFactor(0, 0, 400)
76 {
77  BuildID.SetMinMax(0, std::numeric_limits<int32_t>::max());
78  SessionID.SetMinMax(0, std::numeric_limits<int32_t>::max());
79  CycleID.SetMinMax(0, std::numeric_limits<int32_t>::max());
80  // Maximum 32 seconds cycle time :)
81  CycleTime.SetMinMax(0, 32000);
82  FreeMemory.SetMinMax(0, std::numeric_limits<int32_t>::max());
83  // Note: The official battery capacity is 2200 mAh, but who knows...
84  BatteryCapacity.SetMinMax(0, 3200);
85  BatteryLevel.SetMinMax(MA::StandardRangeMin, MA::StandardRangeMax);
86  PowerConsumption.SetMinMax(0, 32000);
87  // Note: The precision is two decimals (see calculations in APServer::UpdateRobotState()).
90  // The design battery capacity (ERS-7) is 2200 mAh*7.4 V = 16280 mWh
91  // Note: The 7.4 V should be multiplied by 10 to avoid decimals and by 100 again
92  // because of the percentage based calculations in APServer::UpdateRobotState().
93  RealSessionPowerConsumption.SetMinMax(0, 16280*10*100);
95  Temperature.SetMinMax(MA::TemperatureMin, MA::TemperatureMax);
96  DataUpload.SetMinMax(0, std::numeric_limits<int32_t>::max());
97  // Note: Maximum 32 seconds long audio playback
98  TailPan.SetMinMax(MA::TailPanMin, MA::TailPanMax);
99  TailPanForce.SetMinMax(MA::JointForceMin, MA::JointForceMax);
100  TailTilt.SetMinMax(MA::TailTiltMin, MA::TailTiltMax);
101  TailTiltForce.SetMinMax(MA::JointForceMin, MA::JointForceMax);
102  BackButtonF.SetMinMax(MA::BackBtnMin, MA::BackBtnMax);
103  BackButtonM.SetMinMax(MA::BackBtnMin, MA::BackBtnMax);
104  BackButtonR.SetMinMax(MA::BackBtnMin, MA::BackBtnMax);
105  ChestIR.SetMinMax(MA::IRChestMin, MA::IRChestMax);
106  NearIR.SetMinMax(MA::IRNearMin, MA::IRNearMax);
107  FarIR.SetMinMax(MA::IRFarMin, MA::IRFarMax);
108  BackFButtonPressed.SetMinMax(MA::StandardRangeMin, MA::StandardRangeMax);
109  BackMButtonPressed.SetMinMax(MA::StandardRangeMin, MA::StandardRangeMax);
110  BackRButtonPressed.SetMinMax(MA::StandardRangeMin, MA::StandardRangeMax);
111  BackStroked.SetMinMax(MA::StandardRangeMin, MA::StandardRangeMax);
112  BackGrabbed.SetMinMax(MA::StandardRangeMin, MA::StandardRangeMax);
113  ConnectedClients.SetMinMax(0, 255);
114 }
115 
116 
118 {
119  // Create and start an activation timer if it has not been created yet.
120  if (unlikely(!ActivatedStateTimer.IsStarted()))
121  {
123  CycleTime = 0;
124  return;
125  }
126  // Cycle IDs from 1-
127  ++CycleID;
128  // Set the current activation base which is in fact the elapsed milliseconds
129  // since the last update.
131  // Restart the activation timer
133 }
134 
135 
137 {
138  int64_t Time = MCTimer::GetElapsedSystemTime();
139  int MSecs = Time / 1000;
140 
141  Timestamp.Seconds = MSecs / 1000;
142  Timestamp.Milliseconds = MSecs % 1000;
143  Timestamp.Microseconds = Time % 1000;
144 }
145 
146 
148 {
149  GenerateBackFButtonPressed(state.Torso->CycleTime);
150  GenerateBackMButtonPressed(state.Torso->CycleTime);
151  GenerateBackRButtonPressed(state.Torso->CycleTime);
152  GenerateBackStroked(state.Torso->CycleTime);
153  GenerateBackGrabbed(state.Torso->CycleTime);
154 }
155 
156 
157 void MATorso::GenerateBackFButtonPressed(int cycle_time)
158 {
159  if (BackButtonF >= 13)
160  {
161  // Pressed "state" can be reached in 1 second
162  BackFPressedFactor += cycle_time;
163  } else {
164  BackFPressedFactor -= cycle_time*3;
165  }
166  BackFButtonPressed = BackFPressedFactor.GetScaledValue();
167 }
168 
169 
170 void MATorso::GenerateBackMButtonPressed(int cycle_time)
171 {
172  if (BackButtonM >= 13)
173  {
174  // Pressed "state" can be reached in 1 second
175  BackMPressedFactor += cycle_time;
176  } else {
177  BackMPressedFactor -= cycle_time*3;
178  }
179  BackMButtonPressed = BackMPressedFactor.GetScaledValue();
180 }
181 
182 
183 void MATorso::GenerateBackRButtonPressed(int cycle_time)
184 {
185  if (BackButtonR >= 13)
186  {
187  // Pressed "state" can be reached in 1 second
188  BackRPressedFactor += cycle_time;
189  } else {
190  BackRPressedFactor -= cycle_time*3;
191  }
192  BackRButtonPressed = BackRPressedFactor.GetScaledValue();
193 }
194 
195 
196 void MATorso::GenerateBackStroked(int cycle_time)
197 {
198  BackFTouched.PutValue(BackButtonF);
199  BackMTouched.PutValue(BackButtonM);
200  BackRTouched.PutValue(BackButtonR);
201 
202  if (unlikely(!BackFTouched.IsValid() || !BackMTouched.IsValid() || !BackRTouched.IsValid()))
203  return;
204 
205  if (BackFTouched.IsStroked() || BackMTouched.IsStroked() || BackRTouched.IsStroked())
206  {
207  // The back stroked "state" can be reached in 1-2 seconds
208  BackStrokedFactor += (int)((float)cycle_time*MCRand(10.0, 20.0) / 10.0);
209  } else
210  // Keep the current value if the BackR is not touched, but it was touched a moment ago.
211  if (!BackFTouched.IsJustStroked() && !BackMTouched.IsJustStroked() && !BackRTouched.IsJustStroked())
212  {
213  BackStrokedFactor -= cycle_time;
214  }
215  BackStroked = BackStrokedFactor.GetScaledValue();
216 }
217 
218 
219 void MATorso::GenerateBackGrabbed(int cycle_time)
220 {
221  int TouchedButtons = 0;
222 
223  TouchedButtons += BackButtonF > 0;
224  TouchedButtons += BackButtonM > 0;
225  TouchedButtons += BackButtonR > 0;
226 
227  if (TouchedButtons > 1 && BackButtonF+BackButtonM+BackButtonR > 50)
228  {
229  // The back grabbed "state" can be reached in 400 msec
230  BackGrabbedFactor += cycle_time;
231  } else {
232  BackGrabbedFactor -= cycle_time;
233  }
234  BackGrabbed = BackGrabbedFactor.GetScaledValue();
235 }
MANum< int > BatteryCapacity
Battery capacity (mAh)
Definition: MATorso.hpp:132
void Start(int time_shift=0)
Start the timer.
Definition: MCTimer.cpp:183
MANum< int > Milliseconds
Milliseconds.
Definition: MATorso.hpp:53
MANum< int > BuildID
Build ID.
Definition: MATorso.hpp:118
MANum< int > SessionID
Session ID.
Definition: MATorso.hpp:120
MATouchButtonStroked BackFTouched
Stroke detection for a touch sensor on the back (fore)
Definition: MATorso.hpp:223
MANum< int > RealSessionPowerConsumption
Real power consumption of the current session (mWh)
Definition: MATorso.hpp:140
MANum< int > Microseconds
Microseconds.
Definition: MATorso.hpp:55
MAWlanStatistics()
Struct constructor.
Definition: MATorso.cpp:55
virtual void UpdateRobotState(MARobotState &state) override
Update the robot state.
Definition: MATorso.cpp:147
MCTimer ActivatedStateTimer
Activation timer.
Definition: MATorso.hpp:215
MANum< int > Seconds
Seconds.
Definition: MATorso.hpp:51
MANum< int > NearIR
Short-distance IR sensor on the head - 5 cm..50 cm.
Definition: MATorso.hpp:171
MANum< int > ChestIR
IR sensor on the chest - 19 cm..90 cm.
Definition: MATorso.hpp:169
MANum< int > CycleTime
The last cycle time in milliseconds.
Definition: MATorso.hpp:126
virtual void SetMinMax(T min_value, T max_value)
Set the minimal and maximal values.
Definition: MANum.hpp:235
MANum< int > PowerConsumption
Power consumption - 0..32000 mW.
Definition: MATorso.hpp:136
MANum< int > ConnectedClients
Connected client count.
Definition: MATorso.hpp:210
virtual void UpdateTimestamp()
Update the time stamp.
Definition: MATorso.cpp:136
MANum< int > FreeMemory
Free memory.
Definition: MATorso.hpp:128
bool IsStarted() const
Check if the timer has been started.
Definition: MCTimer.cpp:177
MATimestamp Timestamp
Timestamp.
Definition: MATorso.hpp:124
MAIntervalNum< int > BackStrokedFactor
Indicator for stroke detection on the back.
Definition: MATorso.hpp:229
T MCRand(const T &min, const T &max)
Get a random number generated with standard calls.
Definition: MCDefs.hpp:248
MATouchButtonStroked BackMTouched
Stroke detection for a touch sensor on the back (middle)
Definition: MATorso.hpp:225
MANum< int > BatteryLevel
Remaining power in percents - 0..100 %.
Definition: MATorso.hpp:134
MANum< int > EstimatedSessionPowerConsumption
Estimated power consumption (mWh)
Definition: MATorso.hpp:138
MATorso()
Struct constructor.
Definition: MATorso.cpp:63
MANum< int > TailTilt
Tail tilt joint degree - 0..62.
Definition: MATorso.hpp:159
MATimestamp()
Struct constructor.
Definition: MATorso.cpp:31
MANum< int > CycleID
Cycle ID.
Definition: MATorso.hpp:122
MACpuLoad()
Struct constructor.
Definition: MATorso.cpp:45
MANum< int > TailPanForce
Tail pan joint force.
Definition: MATorso.hpp:157
int GetElapsedTime()
Get the elapsed time since the timer has been started or since the last timeout.
Definition: MCTimer.cpp:205
MANum< int > TailPan
Tail pan joint degree - -60..60.
Definition: MATorso.hpp:155
Robot state updater base class.
MANum< int > TailTiltForce
Tail tilt joint force.
Definition: MATorso.hpp:161
MANum< int > FarIR
Long-distance IR sensor on the head - 20 cm..150 cm.
Definition: MATorso.hpp:173
void SetMagnitude(float value)
Set the magnitude.
Definition: MANum.hpp:299
MAIntervalNum< int > BackMPressedFactor
Press detection for a touch sensor on the back (middle)
Definition: MATorso.hpp:219
static int64_t GetElapsedSystemTime(bool relative_time=true)
Get the system time.
Definition: MCTimer.cpp:92
int ToMilliseconds() const
Convert the timestamp to milliseconds.
Definition: MATorso.cpp:39
MANum< int > BackButtonM
Middle touch sensor on the back - 0..60.
Definition: MATorso.hpp:165
virtual void UpdateCycleTime()
Update the cycle time.
Definition: MATorso.cpp:117
boost::scoped_ptr< MATorso > Torso
Torso.
Robot state.
MANum< int > Temperature
Temperature in Celsius degrees - -273..227 °C.
Definition: MATorso.hpp:146
MANum< int > BackButtonR
Rear touch sensor on the back - 0..60.
Definition: MATorso.hpp:167
MATouchButtonStroked BackRTouched
Stroke detection for a touch sensor on the back (rear)
Definition: MATorso.hpp:227
MAIntervalNum< int > BackRPressedFactor
Press detection for a touch sensor on the back (rear)
Definition: MATorso.hpp:221
MAIntervalNum< int > BackGrabbedFactor
Indicator when the robot is grabbed on the back.
Definition: MATorso.hpp:231
MANum< int > BackButtonF
Fore touch sensor on the back - 0..60.
Definition: MATorso.hpp:163
MAIntervalNum< int > BackFPressedFactor
Press detection for a touch sensor on the back (fore)
Definition: MATorso.hpp:217