Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PowerSensor2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ResearchAndDevelopment
PowerSensor2
Commits
047fe715
Commit
047fe715
authored
7 years ago
by
Bram Veenboer
Browse files
Options
Downloads
Patches
Plain Diff
Update Arduino directory
parent
aaad2c75
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
Arduino/Makefile
+2
-7
2 additions, 7 deletions
Arduino/Makefile
Arduino/PowerSensor.ino
+87
-86
87 additions, 86 deletions
Arduino/PowerSensor.ino
Semaphore.h
+46
-0
46 additions, 0 deletions
Semaphore.h
with
135 additions
and
93 deletions
Arduino/Makefile
+
2
−
7
View file @
047fe715
#NO_CORE= 1
#MCU= m328p
#F_CPU= 16000000
#VARIANT= standard
TARGET
=
PowerSensor
BOARD_TAG
=
uno
#ARDUINO_PORT= /dev/ttyACM0
ARDUINO_PORT
=
/dev/ttyUSB0
BOARD_TAG
=
leonardo
ARDUINO_PORT
=
/dev/ttyACM0
include
/usr/share/arduino/Arduino.mk
This diff is collapsed.
Click to expand it.
Arduino/PowerSensor.ino
+
87
−
86
View file @
047fe715
...
...
@@ -32,79 +32,75 @@ struct EEPROM
struct
Sensor
{
double
volt
;
double
type
;
int16_t
nullLevel
s
;
double
nullLevel
;
}
sensors
[
MAX_SENSORS
];
}
eeprom
__attribute__
((
section
(
".eeprom"
)));
int64_t
accumulated_values
[
MAX_SENSORS
];
int16_t
levels
[
MAX_SENSORS
];
long
previous_time
;
double
weights
[
MAX_SENSORS
];
int16_t
nullLevels
[
MAX_SENSORS
];
bool
inUse
[
MAX_SENSORS
];
struct
Sensor
{
bool
inUse
;
uint16_t
count
;
int32_t
total
;
double
weight
;
double
nullLevel
;
double
power
()
const
{
return
weight
*
total
/
count
-
nullLevel
;
}
int64_t
total_accumulated_value
()
{
int64_t
sum
=
0
;
}
sensors
[
MAX_SENSORS
];
for
(
uint8_t
sensor
=
0
;
sensor
<
MAX_SENSORS
;
sensor
++
)
if
(
inUse
[
sensor
])
sum
+=
weights
[
sensor
]
*
accumulated_values
[
sensor
];
return
sum
;
}
bool
streamValues
=
false
;
inline
uint8_t
next
_s
ensor
(
uint8_t
current
_s
ensor
)
inline
uint8_t
next
S
ensor
(
uint8_t
current
S
ensor
)
{
do
if
(
++
current_sensor
==
MAX_SENSORS
)
current_sensor
=
0
;
while
(
!
inUse
[
current_sensor
]);
if
(
++
currentSensor
==
MAX_SENSORS
)
currentSensor
=
0
;
while
(
!
sensors
[
currentSensor
].
inUse
);
return
currentSensor
;
}
return
current_sensor
;
inline
void
setADMUX
(
uint8_t
sensor
)
{
#if defined __AVR_ATmega32U4__
ADMUX
=
_BV
(
REFS0
)
|
((
sensor
<=
2
?
6
:
4
)
-
sensor
);
#else
ADMUX
=
_BV
(
REFS0
)
|
(
sensor
+
1
);
// ADC0 reads the LCD buttons; skip it
#endif
}
ISR
(
ADC_vect
)
{
static
uint8_t
current
_s
ensor
=
0
;
static
uint8_t
current
S
ensor
=
0
;
uint8_t
low
=
ADCL
,
high
=
ADCH
;
// read in this order
levels
[
current_sensor
]
=
(
high
<<
8
)
|
low
;
// start next ADC ASAP.
uint8_t
previous
_s
ensor
=
current
_s
ensor
;
current
_s
ensor
=
next
_s
ensor
(
current
_s
ensor
);
uint8_t
previous
S
ensor
=
current
S
ensor
;
current
S
ensor
=
next
S
ensor
(
current
S
ensor
);
ADMUX
=
_BV
(
REFS0
)
|
(
current
_s
ensor
+
1
);
// ADC0 reads the LCD buttons; skip it
set
ADMUX
(
current
S
ensor
);
ADCSRA
|=
_BV
(
ADSC
);
// start ADC conversion
// handle measured value
if
(
current_sensor
<=
previous_sensor
)
{
// we had one round of all sensors. now handle them in one go
long
measured_time
=
micros
();
long
time_difference
=
measured_time
-
previous_time
;
previous_time
=
measured_time
;
int16_t
level
=
(
high
<<
8
)
|
low
;
sensors
[
previousSensor
].
total
+=
level
-
512
;
sensors
[
previousSensor
].
count
++
;
for
(
uint8_t
sensor
=
0
;
sensor
<
MAX_SENSORS
;
sensor
++
)
// compiler will generate 16x16 bit multiply producing 32-bit result
accumulated_values
[
sensor
]
+=
(
long
)
(
int
)
time_difference
*
(
levels
[
sensor
]
-
nullLevels
[
sensor
]);
}
}
if
(
streamValues
)
{
Serial
.
write
((
previousSensor
<<
5
)
|
(
level
>>
5
));
Serial
.
write
(
0xE0
|
(
level
&
0x1F
));
#if defined __AVR_ATmega32U4__
Serial
.
flush
();
#endif
void
readState
()
{
noInterrupts
();
int32_t
total
=
total_accumulated_value
()
>>
16
;
Serial
.
write
((
const
uint8_t
*
)
&
total
,
sizeof
total
);
Serial
.
write
((
const
uint8_t
*
)
&
previous_time
,
sizeof
previous_time
);
interrupts
();
//for (int i = 2; i < 16; i ++)
//Serial.write(0);
}
}
...
...
@@ -114,9 +110,9 @@ void configureFromEEPROM()
eeprom_read_block
(
&
copy
,
&
eeprom
,
sizeof
copy
);
for
(
unsigned
i
=
0
;
i
<
MAX_SENSORS
;
i
++
)
{
inUse
[
i
]
=
copy
.
sensors
[
i
].
volt
!=
0
;
weight
s
[
i
]
=
inUse
[
i
]
?
copy
.
sensors
[
i
].
volt
*
2.5
/
copy
.
sensors
[
i
].
type
:
0
;
nullLevel
s
[
i
]
=
copy
.
sensors
[
i
].
nullLevel
s
;
sensors
[
i
].
inUse
=
copy
.
sensors
[
i
].
volt
!=
0
;
sensors
[
i
].
weight
=
sensor
s
[
i
]
.
inUse
?
(
2.5
/
512
)
*
(
copy
.
sensors
[
i
].
volt
/
copy
.
sensors
[
i
].
type
)
:
0
;
sensors
[
i
].
nullLevel
=
copy
.
sensors
[
i
].
nullLevel
;
}
}
...
...
@@ -131,7 +127,10 @@ void readConfig()
void
writeConfig
()
{
#if !defined __AVR_ATmega32U4__
Serial
.
begin
(
115200
);
// serial comm from host to Arduino seems less reliable --> reduce baud rate
#endif
EEPROM
copy
;
for
(
unsigned
i
=
0
;
i
<
sizeof
copy
;
i
++
)
{
...
...
@@ -141,31 +140,32 @@ void writeConfig()
((
uint8_t
*
)
&
copy
)[
i
]
=
Serial
.
read
();
}
Serial
.
begin
(
2000000
);
// probably only neecssary on Leonardo, that does not automatically reset after creating a new connection
#if !defined __AVR_ATmega32U4__
Serial
.
begin
(
2000000
);
#endif
eeprom_update_block
(
&
copy
,
&
eeprom
,
sizeof
copy
);
configureFromEEPROM
();
}
void
readLevels
()
{
Serial
.
write
((
const
uint8_t
*
)
levels
,
sizeof
levels
);
}
void
serialEventRun
()
{
switch
(
Serial
.
read
())
{
case
'
l
'
:
read
Levels
();
case
'
r
'
:
read
Config
();
break
;
case
'
r
'
:
read
Config
();
case
'
w
'
:
write
Config
();
break
;
case
'
s
'
:
rea
dState
()
;
case
'
S
'
:
st
rea
mValues
=
true
;
break
;
case
'w'
:
writeConfig
();
case
'T'
:
streamValues
=
false
;
break
;
case
'X'
:
streamValues
=
false
;
Serial
.
write
((
const
uint8_t
[])
{
0xFF
,
0xE0
},
2
);
break
;
}
}
...
...
@@ -182,7 +182,7 @@ void setup()
Serial
.
begin
(
2000000
);
ADMUX
=
_BV
(
REFS0
)
+
1
;
set
ADMUX
(
0
)
;
ADCSRA
|=
_BV
(
ADIE
);
// enable ADC interrupts
ADCSRA
|=
_BV
(
ADSC
);
// start ADC conversion
}
...
...
@@ -190,51 +190,52 @@ void setup()
void
loop
()
{
static
unsigned
long
previous_print_time
;
static
int64_t
previous_value_of_next_sensor
,
previous_value_total
;
static
uint8_t
count
,
current_sensor
=
next_sensor
(
MAX_SENSORS
-
1
),
current_line
;
noInterrupts
();
static
unsigned
long
previousPrintTime
;
static
uint8_t
count
,
currentSensor
=
nextSensor
(
MAX_SENSORS
-
1
),
currentLine
;
u
nsigned
long
current
_t
ime
=
mi
cro
s
(),
time
_d
ifference
=
current
_t
ime
-
previous
_p
rint
_t
ime
;
u
int16_t
current
T
ime
=
mi
lli
s
(),
time
D
ifference
=
current
T
ime
-
previous
P
rint
T
ime
;
if
(
time
_d
ifference
>
333
333
)
{
if
(
time
D
ifference
>
333
)
{
double
power
;
if
(
current
_l
ine
==
0
)
{
if
(
current
L
ine
==
0
)
{
// print top line; rotate among the used sensors
int64_t
total
=
weights
[
current_sensor
]
*
accumulated_values
[
current_sensor
];
if
((
++
count
&
7
)
==
0
)
current_sensor
=
next_sensor
(
current_sensor
);
power
=
(
double
)
(
total
-
previous_value_of_next_sensor
)
/
(
512
*
time_difference
);
previous_value_of_next_sensor
=
weights
[
current_sensor
]
*
accumulated_values
[
current_sensor
];
noInterrupts
();
power
=
sensors
[
currentSensor
].
power
();
interrupts
();
if
((
++
count
&
7
)
==
0
)
currentSensor
=
nextSensor
(
currentSensor
);
if
((
count
&
7
)
==
1
)
{
lcd
.
setCursor
(
7
,
0
);
lcd
.
print
((
char
)
(
'0'
+
current
_s
ensor
));
lcd
.
print
((
char
)
(
'0'
+
current
S
ensor
));
}
}
else
{
// current
_l
ine == 1
}
else
{
// current
L
ine == 1
// print bottom line; the sum of all sensors
int64_t
total
=
total_accumulated_value
();
power
=
0
;
noInterrupts
();
for
(
uint8_t
sensor
=
0
;
sensor
<
MAX_SENSORS
;
sensor
++
)
{
if
(
sensors
[
sensor
].
inUse
)
{
power
+=
sensors
[
sensor
].
power
();
sensors
[
sensor
].
total
=
0
;
sensors
[
sensor
].
count
=
0
;
}
}
interrupts
();
power
=
(
double
)
(
total
-
previous_value_total
)
/
(
512
*
time_difference
);
previous_value_total
=
total
;
previous_print_time
=
current_time
;
previousPrintTime
=
currentTime
;
}
char
buffer
[
16
];
dtostrf
(
power
,
6
,
1
,
buffer
);
lcd
.
setCursor
(
9
,
current
_l
ine
);
lcd
.
setCursor
(
9
,
current
L
ine
);
lcd
.
print
(
buffer
);
current
_l
ine
^=
1
;
current
L
ine
^=
1
;
}
interrupts
();
}
This diff is collapsed.
Click to expand it.
Semaphore.h
0 → 100644
+
46
−
0
View file @
047fe715
#if !defined SEMAPHORE_H
#define SEMAPHORE_H
#include
<condition_variable>
#include
<mutex>
namespace
PowerSensor
{
class
Semaphore
{
public:
Semaphore
(
unsigned
initialLevel
=
0
)
:
level
(
initialLevel
)
{
}
void
up
(
unsigned
count
=
1
)
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex
);
level
+=
count
;
if
(
count
==
1
)
cv
.
notify_one
();
else
cv
.
notify_all
();
}
void
down
(
unsigned
count
=
1
)
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex
);
cv
.
wait
(
lock
,
[
this
,
count
]
{
return
level
>=
count
;
});
level
-=
count
;
}
private
:
std
::
mutex
mutex
;
std
::
condition_variable
cv
;
unsigned
level
;
};
}
#endif
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment