-
Notifications
You must be signed in to change notification settings - Fork 41
/
MotorNM.cpp
91 lines (78 loc) · 2.58 KB
/
MotorNM.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "MotorNM.h"
#include <iostream>
using namespace std;
MotorNM::MotorNM() :
StateMachine(ST_MAX_STATES),
m_currentSpeed(0)
{
}
// set motor speed external event
void MotorNM::SetSpeed(MotorNMData* data)
{
static const BYTE TRANSITIONS[] = {
ST_START, // ST_IDLE
CANNOT_HAPPEN, // ST_STOP
ST_CHANGE_SPEED, // ST_START
ST_CHANGE_SPEED, // ST_CHANGE_SPEED
};
ExternalEvent(TRANSITIONS[GetCurrentState()], data);
C_ASSERT((sizeof(TRANSITIONS)/sizeof(BYTE)) == ST_MAX_STATES);
// Alternate transition map using macro support
//BEGIN_TRANSITION_MAP // - Current State -
// TRANSITION_MAP_ENTRY (ST_START) // ST_IDLE
// TRANSITION_MAP_ENTRY (CANNOT_HAPPEN) // ST_STOP
// TRANSITION_MAP_ENTRY (ST_CHANGE_SPEED) // ST_START
// TRANSITION_MAP_ENTRY (ST_CHANGE_SPEED) // ST_CHANGE_SPEED
//END_TRANSITION_MAP(data)
}
// halt motor external event
void MotorNM::Halt()
{
static const BYTE TRANSITIONS[] = {
EVENT_IGNORED, // ST_IDLE
CANNOT_HAPPEN, // ST_STOP
ST_STOP, // ST_START
ST_STOP, // ST_CHANGE_SPEED
};
ExternalEvent(TRANSITIONS[GetCurrentState()], NULL);
C_ASSERT((sizeof(TRANSITIONS)/sizeof(BYTE)) == ST_MAX_STATES);
// Alternate transition map using macro support
//BEGIN_TRANSITION_MAP // - Current State -
// TRANSITION_MAP_ENTRY (EVENT_IGNORED) // ST_IDLE
// TRANSITION_MAP_ENTRY (CANNOT_HAPPEN) // ST_STOP
// TRANSITION_MAP_ENTRY (ST_STOP) // ST_START
// TRANSITION_MAP_ENTRY (ST_STOP) // ST_CHANGE_SPEED
//END_TRANSITION_MAP(NULL)
}
// state machine sits here when motor is not running
//STATE_DEFINE(Motor, Idle, NoEventData)
void MotorNM::ST_Idle(const NoEventData* data)
{
cout << "MotorNM::ST_Idle" << endl;
}
// stop the motor
//STATE_DEFINE(Motor, Stop, NoEventData)
void MotorNM::ST_Stop(const NoEventData* data)
{
cout << "MotorNM::ST_Stop" << endl;
m_currentSpeed = 0;
// perform the stop motor processing here
// transition to Idle via an internal event
InternalEvent(ST_IDLE);
}
// start the motor going
//STATE_DEFINE(Motor, Start, MotorData)
void MotorNM::ST_Start(const MotorNMData* data)
{
cout << "MotorNM::ST_Start : Speed is " << data->speed << endl;
m_currentSpeed = data->speed;
// set initial motor speed processing here
}
// changes the motor speed once the motor is moving
//STATE_DEFINE(Motor, ChangeSpeed, MotorData)
void MotorNM::ST_ChangeSpeed(const MotorNMData* data)
{
cout << "MotorNM::ST_ChangeSpeed : Speed is " << data->speed << endl;
m_currentSpeed = data->speed;
// perform the change motor speed to data->speed here
}