-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPWMDevice.cpp
147 lines (125 loc) · 3.99 KB
/
PWMDevice.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* PWMDevice.cpp
*
* Robovero class to communicate with PWM device attached to the Gumstix Robovero expansion board.
*
* This class is basically a conversion of the python code written by Neil
* MacMunn ([email protected]) at Gumstix.
* https://github.com/robovero
*
*
* Andrew C. Smith
* Aerospace Robotics Lab
* Stanford University
*
* v1.1 June 26 2013
*
*/
#include "PWMDevice.h"
#include "lpc_types.h"
#include "lpc17xx_pwm.h"
#include "LPC17xx.h"
#include <string.h>
bool PWMDevice::isPeriodSet;
unsigned int PWMDevice::pwm_period;
// issue new position command to servo
void PWMDevice::move(unsigned int pwm_command)
{
unsigned int args[4];
char command_string[128];
// make sure command is valid
if (pwm_command > 0)
{
args[0] = (unsigned int)LPC_PWM1;
args[1] = pwm_channel;
args[2] = pwm_command;
//args[3] = PWM_MATCH_UPDATE_NOW;
args[3] = PWM_MATCH_UPDATE_NEXT_RST;
strcpy(command_string, "PWM_MatchUpdate");
//robo_host->robocaller(command_string,false,args,4);
robo_host->robocaller(command_string,args,4);
}
}
// initializing script for PWMDevice
void PWMDevice::initPWM()
{
unsigned int args[5];
char command_string[128];
if (PWMDevice::isPeriodSet) // the period has been set
{
// set the pulse
args[0] = pwm_channel;
args[1] = pwm_pulseWidth;
strcpy(command_string, "initMatch");
//robo_host->robocaller(command_string,false,args,2);
robo_host->robocaller(command_string,args,2);
// additional needed commands
args[0] = (unsigned int)LPC_PWM1;
args[1] = pwm_channel;
args[2] = ENABLE;
strcpy(command_string, "PWM_ChannelCmd");
//robo_host->robocaller(command_string,false,args,3);
robo_host->robocaller(command_string,args,3);
}
else
{
// set the period of all pwm devices
args[0] = 0;
args[1] = pwm_period;// 20000us = 20ms = 50Hz
strcpy(command_string, "initMatch");
//robo_host->robocaller(command_string,false,args,2);
robo_host->robocaller(command_string,args,2);
// set the pulse
args[0] = pwm_channel;
args[1] = pwm_pulseWidth;
strcpy(command_string, "initMatch");
//robo_host->robocaller(command_string,false,args,2);
robo_host->robocaller(command_string,args,2);
// additional needed commands
args[0] = (unsigned int)LPC_PWM1;
args[1] = pwm_channel;
args[2] = ENABLE;
strcpy(command_string, "PWM_ChannelCmd");
//robo_host->robocaller(command_string,false,args,3);
robo_host->robocaller(command_string,args,3);
// only perform these actions the first time the PWM periferal is used
args[0] = (unsigned int)LPC_PWM1;
strcpy(command_string, "PWM_ResetCounter");
//robo_host->robocaller(command_string,false,args,1);
robo_host->robocaller(command_string,args,1);
args[1] = ENABLE;
strcpy(command_string, "PWM_CounterCmd");
//robo_host->robocaller(command_string,false,args,2);
robo_host->robocaller(command_string,args,2);
strcpy(command_string, "PWM_Cmd");
//robo_host->robocaller(command_string,false,args,2);
robo_host->robocaller(command_string,args,2);
isPeriodSet = true;
}
}
// create PWMDevice connected to Robovero host with the given channel
PWMDevice::PWMDevice(Robovero *host, unsigned int channel)
{
// set the period and pulse width to nominal values
pwm_period = 20000; // 20000us = 20ms = 50Hz
pwm_pulseWidth = 1520; // 1520us = 1.5ms
robo_host = host;
pwm_channel = channel;
// initialize the device
initPWM();
}
// create PWMDevice connected to Robovero host with given, channel, period and pulseWidth
PWMDevice::PWMDevice(Robovero *host, unsigned int channel, unsigned int period, unsigned int pulse)
{
pwm_period = period;
pwm_pulseWidth = pulse;
robo_host = host;
pwm_channel = channel;
// initialize the device
initPWM();
}
// destructor ... doesn't have to do anything in this case
PWMDevice::~PWMDevice()
{
}