Skip to content

Commit c83706b

Browse files
authored
Merge pull request #125 from mcgill-robotics/power_commenting
Added comments to power_custom & power_ros1_main.cpp
2 parents aafce8f + d810878 commit c83706b

File tree

8 files changed

+135
-12
lines changed

8 files changed

+135
-12
lines changed

pio_workspace/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
.vscode/c_cpp_properties.json
44
.vscode/launch.json
55
.vscode/ipch
6+
.DS_Store
7+
lib/.DS_Store
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,39 @@
1+
/*
2+
Revision 1.0
3+
Commenter: Dorothy Ma
4+
5+
TEMPERATURE SENSOR CONTROLS:
6+
0) indicate pin number and ref (Vcc) voltage when creating TMP36 object
7+
1) call "begin()" to setup the pin with microcontroller
8+
2) call "readTemperature()" to retrieve updated measurements
9+
*/
10+
111
#include "TMP36.h"
212

13+
// Arguments: pin number, reference voltage (Vcc)
314
TMP36::TMP36(int pin, float ref) {
415
sensorPin = pin;
516
refVoltage = ref;
17+
// Initializes volts and temperature variables to 0
618
volts = 0;
719
temperature = 0;
820
}
921

22+
// Initializes the temp sensor by setting it up with the right pin
1023
void TMP36::begin() {
1124
pinMode(sensorPin, INPUT);
1225
}
1326

27+
28+
// Returns reading of the temperature sensor
1429
float TMP36::readTemperature() {
30+
// calculates the temperature based on formula and reference voltage from the setup
31+
// code source: https://botland.store/content/147-Read-temperature-with-an-Arduino-and-sensor-TMP36GT9Z
32+
1533
int reading = analogRead(sensorPin);
1634
volts = (reading * refVoltage) / 1024.0;
1735
temperature = (volts - 0.5) * 100;
1836

1937
return temperature;
38+
2039
}
Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
1+
/*
2+
Revision 1.0
3+
Commenter: Dorothy Ma
4+
5+
HEADER FILE FOR TEMPERATURE SENSOR (TMP36GT9Z)
6+
*/
7+
18
#ifndef TMP36_H
29
#define TMP36_H
310

411
#include "Arduino.h"
512

613
class TMP36 {
714
public:
15+
// Constructor for TMP36 class
816
TMP36(int pin, float ref);
917

18+
// Initialize TMP36 class
1019
void begin();
20+
21+
// Performs one reading of the sensor
1122
float readTemperature();
1223

1324
private:
14-
int sensorPin;
15-
float refVoltage;
16-
float volts;
17-
float temperature;
25+
int sensorPin; // Pin number to which the sensor is connected
26+
float refVoltage; // Reference voltage for voltage conversion (Vcc)
27+
float volts; // Voltage from reading
28+
float temperature; // Temperature from reading
1829
};
1930

2031
#endif

pio_workspace/lib/power_custom/ThrusterControl.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
/*
2+
Revision 1.0
3+
4+
FUNCTIONS TO CONTROL THRUSTERS:
5+
1) call "initThrusters()" to attach servos and initialize all thrusters to off state
6+
2) call "updateThrusters(const int16_t microseconds[8])" to update PWM signals based on the microseconds[8] array
7+
*/
8+
19
#ifdef POWER_H
210

311
#include "ThrusterControl.h"

pio_workspace/lib/power_custom/ThrusterControl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/*
2+
Revision 1.0
3+
4+
HEADER FILE FOR THRUSTER CONTROL
5+
*/
6+
17
#ifdef POWER_H
28

39
#ifndef THRUSTER_CONTROL_H

pio_workspace/lib/power_custom/adc_sensors.cpp

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1-
#include "adc_sensors.h"
1+
/*
2+
Revision 1.0
3+
Commenter: Dorothy Ma
4+
5+
FUNCTIONS TO USE VOLTAGE- AND CURRENT-SENSING WITH ADCS:
6+
1) call "begin()" to initialize the ADCs and indicate whether to enableVoltage- and or enableCurrent-sensing
7+
2) call "senseVoltage()" and "senseCurrent()" to retrieve updated measurements
8+
*/
29

10+
#include "adc_sensors.h"
11+
/*
12+
Constructor for empty ADCSensor object:
13+
1) voltageEnabled and currentEnabled flags are set to false
14+
2) the arrays for holding sensor values are initialized to 0
15+
*/
316
ADCSensors::ADCSensors() {
417
voltageEnabled = false;
518
currentEnabled = false;
@@ -17,6 +30,11 @@ ADCSensors::ADCSensors() {
1730
}
1831
}
1932

33+
/*
34+
Initializes the I2C buses for sensors using their respective addresses for current and voltage sensing
35+
Arguments: flags for voltage sensing, current sensing, and wire
36+
Returns: true if the sensors are successfully initialized
37+
*/
2038
bool ADCSensors::begin(bool enableVoltage, bool enableCurrent, TwoWire* wire) {
2139
voltageEnabled = enableVoltage;
2240
currentEnabled = enableCurrent;
@@ -35,6 +53,10 @@ bool ADCSensors::begin(bool enableVoltage, bool enableCurrent, TwoWire* wire) {
3553
return success;
3654
}
3755

56+
/*
57+
Returns meaningful voltage values into an array [2]
58+
Will be -ve if voltageEnabled = false
59+
*/
3860
float* ADCSensors::senseVoltage() {
3961
if (voltageEnabled) {
4062
refreshVoltage();
@@ -47,6 +69,10 @@ float* ADCSensors::senseVoltage() {
4769
return voltageValues;
4870
}
4971

72+
/*
73+
Returns meaningful current values into an array [8]
74+
Will be -ve if currentEnabled = false
75+
*/
5076
float* ADCSensors::senseCurrent() {
5177
if (currentEnabled) {
5278
refreshCurrent();
@@ -61,6 +87,10 @@ float* ADCSensors::senseCurrent() {
6187
return currentValues;
6288
}
6389

90+
/*
91+
Performs one reading of the ADC for voltage
92+
Read and stores raw ADC value from channels 0-1 and computed voltage values into arrays
93+
*/
6494
void ADCSensors::refreshVoltage() {
6595
rawADCVoltage[0] = adcVoltage.readADC_SingleEnded(0);
6696
rawADCVoltage[1] = adcVoltage.readADC_SingleEnded(1);
@@ -69,6 +99,11 @@ void ADCSensors::refreshVoltage() {
6999
computedADCVoltage[1] = adcVoltage.computeVolts(rawADCVoltage[1]);
70100
}
71101

102+
/*
103+
Performs one reading of the ADC for current
104+
Read and stores raw ADC value for channels 0-3 for both adcCurrent objects
105+
Stores adcCurrent1 values into 0-3 and adcCurrent2 values into 4-7
106+
*/
72107
void ADCSensors::refreshCurrent() {
73108
for (int i = 0; i < 4; i++) {
74109
rawADCCurrent[i] = adcCurrent1.readADC_SingleEnded(i);
@@ -81,10 +116,22 @@ void ADCSensors::refreshCurrent() {
81116
}
82117
}
83118

119+
// Convert ADC values into meaningful real-life voltage values based on circuit components
84120
float ADCSensors::convertVoltage(float adcVoltage) {
121+
/*
122+
1+R41/R42 = 3
123+
VBAT LOW: 12.8 | ADC INPUT: 0.18
124+
VBAT NOM: 14.8 | ADC INPUT: 1.384
125+
VBAT HIGH: 16.8 | ADC INPUT: 2.586
126+
*/
85127
return (adcVoltage * 2) * (16.8 - 12.8) / (2.586 - 0.180) + 12.5;
86128
}
87129

130+
// Convert ADC values into meaningful real-life current values based on circuit components
88131
float ADCSensors::convertCurrent(float adcCurrent) {
132+
/*
133+
Rs * Imax * Gain = Vomax
134+
Vomax = 0.005 * 10 * 50 = 2.5
135+
*/
89136
return (((adcCurrent * 2 ) / 50 ) / 0.005);
90137
}

pio_workspace/lib/power_custom/adc_sensors.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/*
2+
Revision 1.0
3+
Commenter: Dorothy Ma
4+
5+
HEADER FILE FOR ADC_SENSOR FOR CURRENT & VOLTAGE SENSING
6+
*/
7+
18
#ifndef ADC_SENSORS_H
29
#define ADC_SENSORS_H
310

@@ -11,32 +18,43 @@
1118

1219
class ADCSensors {
1320
public:
21+
// Constructor for ADCSensor class
1422
ADCSensors();
1523

24+
// Initialization of ADCSensor
1625
bool begin(bool enableVoltage, bool enableCurrent, TwoWire* wire = &Wire1);
26+
27+
// Returns an array pointer to current measurements
1728
float* senseVoltage();
1829
float* senseCurrent();
1930

2031
private:
32+
// Flags to indicate which component to measure
2133
bool voltageEnabled;
2234
bool currentEnabled;
2335

36+
// Create Adafruit_ADS1115 sensor drivers
2437
Adafruit_ADS1115 adcCurrent1;
2538
Adafruit_ADS1115 adcCurrent2;
2639
Adafruit_ADS1115 adcVoltage;
2740

41+
// Arrays for raw measurements from the ADC
2842
int16_t rawADCVoltage[2];
2943
float computedADCVoltage[2];
3044

45+
// Arrays for measurements from ADC converted by the ADS1X15 computeVolts method
3146
int16_t rawADCCurrent[8];
3247
float computedADCCurrent[8];
3348

49+
// Arrays for V and I values calculated using real-world components with meaningful values
3450
float voltageValues[2];
3551
float currentValues[8];
3652

53+
// Reads the ADC values and uses computeVolts and stores into arrays
3754
void refreshVoltage();
3855
void refreshCurrent();
3956

57+
// Converts computed values to real-world values and stores into arrays
4058
float convertVoltage(float adcVoltage);
4159
float convertCurrent(float adcCurrent);
4260
};

pio_workspace/src/power_ros1_main.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
/*
2+
Revision 1.0
3+
4+
SETUP FOR ROS COMMUNICATION WITH THE POWER BOARD
5+
1) Initializes and connects thrusters, adc sensors, and temperature sensors
6+
2) Sets up ros nodes for advertising and subscribing
7+
3) Polls all sensors, publishes data, and updates thrusters every 10 ms
8+
*/
9+
110
#ifdef POWER_ROS1_H
211

312
#include "power_ros1_main.h"
@@ -29,10 +38,11 @@
2938
#define ENABLE_VOLTAGE_SENSE true
3039
#define ENABLE_CURRENT_SENSE true
3140

41+
// creates ADCSensors and TMP36 sensor objects
3242
ADCSensors adcSensors;
3343
TMP36 temperatureSensor(23, 3.3);
3444

35-
// defines 8 thursters for ROS subscribing
45+
// defines 8 thrusters for ROS subscribing
3646
const uint8_t BACK_L = auv_msgs::ThrusterMicroseconds::BACK_LEFT;
3747
const uint8_t HEAVE_BACK_L = auv_msgs::ThrusterMicroseconds::HEAVE_BACK_LEFT;
3848
const uint8_t HEAVE_FRONT_L = auv_msgs::ThrusterMicroseconds::HEAVE_FRONT_LEFT;
@@ -46,6 +56,7 @@ const uint8_t BACK_R = auv_msgs::ThrusterMicroseconds::BACK_RIGHT;
4656
std_msgs::Float32 batt1_voltage_msg;
4757
std_msgs::Float32 batt2_voltage_msg;
4858

59+
// defines 8 thruster current sensing for ROS advertising
4960
std_msgs::Float32 thruster1_current_msg;
5061
std_msgs::Float32 thruster2_current_msg;
5162
std_msgs::Float32 thruster3_current_msg;
@@ -55,6 +66,7 @@ std_msgs::Float32 thruster6_current_msg;
5566
std_msgs::Float32 thruster7_current_msg;
5667
std_msgs::Float32 thruster8_current_msg;
5768

69+
// defines board and teensy temperature sensing for ROS advertising
5870
std_msgs::Float32 board_temperature_msg;
5971
std_msgs::Float32 teensy_temperature_msg;
6072

@@ -65,9 +77,8 @@ Servo thrusters[8];
6577
uint16_t microseconds[] = {1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500};
6678
const uint16_t offCommand[] = {1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500};
6779

68-
// creates array for 2 battery voltage sensing
69-
float Bvoltages[2];
70-
float Tcurrents[8];
80+
float Bvoltages[2]; // array for 2 battery voltage sensing
81+
float Tcurrents[8]; // array for 8 thrusters current sensing
7182
float boardTemperature;
7283
float teensyTemperature;
7384

@@ -118,7 +129,7 @@ ros::Publisher thruster8_current("/power/thrusters/current/8", &thruster8_curren
118129
ros::Publisher board_temperature("/power/board/temperature", &board_temperature_msg);
119130
ros::Publisher teensy_temperature("/power/teensy/temperature", &teensy_temperature_msg);
120131

121-
// senses the voltages of the 2 batteries
132+
// senses the battery voltages, thruster currents, and board and teensy temperatures
122133
void senseData() {
123134
float* voltagePtr = adcSensors.senseVoltage();
124135
float* currentPtr = adcSensors.senseCurrent();
@@ -171,9 +182,10 @@ void publishData() {
171182
teensy_temperature.publish(&teensy_temperature_msg);
172183
}
173184

185+
// setup all thrusters and sensors and setup node handler for subscribing and advertising
174186
void power_ros1_setup() {
175187
pinMode(LED_PIN, OUTPUT);
176-
digitalWrite(LED_PIN, HIGH);
188+
digitalWrite(LED_PIN, HIGH); // turn on LED_PIN
177189

178190
initThrusters();
179191

@@ -197,7 +209,7 @@ void power_ros1_setup() {
197209
}
198210

199211
void power_ros1_loop() {
200-
updateThrusters(microseconds);
212+
updateThrusters(microseconds);
201213

202214
publishData();
203215

0 commit comments

Comments
 (0)