Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
MicroCART
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
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
Distributed Autonomous Networked Control Lab
MicroCART
Commits
c7bbe22a
Commit
c7bbe22a
authored
2 years ago
by
cpryan
Browse files
Options
Downloads
Patches
Plain Diff
Added serial interface for test stand firmware
parent
94aa44b1
No related branches found
No related tags found
2 merge requests
!109
Final sdmay24-32 merge to master
,
!102
Adds TestStand class to r/w cmds for testStand
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
test_stand_firmware/test_stand_firmware.ino
+164
-109
164 additions, 109 deletions
test_stand_firmware/test_stand_firmware.ino
with
164 additions
and
109 deletions
test_stand_firmware/test_stand_firmware.ino
+
164
−
109
View file @
c7bbe22a
#include
<movingAvg.h>
const
int
DATA_PIN
=
A7
;
const
int
BUTTON_PIN
=
2
;
int
buttonState
=
0
;
const
boolean
NO_HANDSHAKE
=
false
;
const
int
SEND_CODE
=
65
;
int
incomingByte
=
0
;
int
val
=
0
;
const
double
TEN_BIT_SCALAR
=
0.3516
;
// 360/1024
int
homePosition
;
// Rate variables
long
lastTime
=
0
;
long
currTime
=
0
;
long
lastReading
=
0
;
long
currReading
=
0
;
int
prevRate
=
0
;
long
deltaTime
=
0
;
long
deltaReading
=
0
;
double
rate
;
// Long Press Variables
long
buttonTimer
=
0
;
const
long
LONG_PRESS_MILLIS
=
250
;
boolean
buttonActive
=
false
;
boolean
longPressActive
=
false
;
boolean
isInPositionMode
=
true
;
movingAvg
mySensor
(
10
);
void
serialFlush
()
{
while
(
Serial
.
available
()
>
0
)
{
char
t
=
Serial
.
read
();
}
}
void
setup
()
{
// put your setup code here, to run once:
Serial
.
begin
(
9600
);
pinMode
(
BUTTON_PIN
,
INPUT
);
pinMode
(
LED_BUILTIN
,
OUTPUT
);
homePosition
=
analogRead
(
DATA_PIN
);
homePosition
+=
512
;
lastTime
=
millis
();
lastReading
=
analogRead
(
DATA_PIN
);
mySensor
.
begin
();
}
void
loop
()
{
currTime
=
millis
();
currReading
=
analogRead
(
DATA_PIN
);
if
(
digitalRead
(
BUTTON_PIN
)
==
HIGH
)
{
if
(
buttonActive
==
false
)
{
// Button was not pressed last cycle
buttonActive
=
true
;
buttonTimer
=
millis
();
}
if
(((
millis
()
-
buttonTimer
)
>
LONG_PRESS_MILLIS
)
&&
(
longPressActive
==
false
))
{
// Detected a long press, swap modes
longPressActive
=
true
;
isInPositionMode
=
!
isInPositionMode
;
}
}
else
{
if
(
buttonActive
==
true
)
{
// Button was being pressed last cycle
if
(
longPressActive
==
true
)
{
// Button was being long pressed last cycle, user has released the long press
longPressActive
=
false
;
}
else
{
// Short press occured, grab current position regardless of mode
homePosition
=
analogRead
(
DATA_PIN
);
homePosition
+=
512
;
}
buttonActive
=
false
;
}
}
// LED on if we're in position mode, off if we're in rate mode
digitalWrite
(
LED_BUILTIN
,
isInPositionMode
);
if
(
isInPositionMode
==
true
)
{
double
absoluteDegrees
=
TEN_BIT_SCALAR
*
(
double
)
currReading
;
double
relativeDegrees
=
TEN_BIT_SCALAR
*
(
double
)
homePosition
;
double
displayPosition
=
absoluteDegrees
-
relativeDegrees
;
displayPosition
+=
(
displayPosition
<
0
)
?
(
360
)
:
(
0
);
Serial
.
println
((
displayPosition
-
180
));
delay
(
100
);
}
else
{
// We're in rate mode
deltaTime
=
currTime
-
lastTime
;
deltaReading
=
currReading
-
lastReading
;
rate
=
1000
*
((
TEN_BIT_SCALAR
*
deltaReading
)
/
(
deltaTime
));
// Thanks josh for the fix
if
(
abs
(
rate
-
prevRate
)
>
360
)
{
}
else
{
mySensor
.
reading
(
rate
);
Serial
.
println
(
mySensor
.
getAvg
()
*-
1
);
delay
(
50
);
}
prevRate
=
rate
;
}
// Reset vars for next cycle
lastTime
=
currTime
;
lastReading
=
currReading
;
}
#include
<movingAvg.h>
unsigned
long
test_time
=
0
;
const
int
DATA_PIN
=
A7
;
const
int
BUTTON_PIN
=
2
;
boolean
isStreaming
=
true
;
int
f
=
200
;
int
w
=
0
;
unsigned
long
time_step
=
5000
;
const
double
TEN_BIT_SCALAR
=
0.3516
;
// 360/1024
int
homePosition
;
double
displayPosition
;
// Rate variables
unsigned
long
lastTime
=
0
;
unsigned
long
currTime
=
0
;
int
lastReading
=
0
;
int
currReading
=
0
;
double
deltaTime
=
1
;
double
deltaReading
=
0
;
unsigned
long
buttonTimer
=
0
;
int
i
=
0
;
double
rate
;
// Long Press Variables
const
long
LONG_PRESS
=
250
;
boolean
isInPositionMode
=
true
;
movingAvg
myRate
(
10
);
void
rateMode
(){
deltaTime
=
currTime
-
lastTime
;
// Find change in time
deltaReading
=
currReading
-
lastReading
;
// Find change in position
deltaReading
+=
(
deltaReading
<
-
512
)
?
(
1024
)
:
(
0
);
deltaReading
-=
(
deltaReading
>
512
)
?
(
1024
)
:
(
0
);
// Rate is change in position divided by change in time (multiply by 10^6 to convert microseconds to seconds)
rate
=
TEN_BIT_SCALAR
*
deltaReading
*
1000000
/
deltaTime
;
myRate
.
reading
(
rate
);
rate
=
myRate
.
getAvg
();
if
(
isStreaming
&&
!
(
isInPositionMode
)){
Serial
.
println
(
rate
);
}
}
void
positionMode
(){
displayPosition
=
TEN_BIT_SCALAR
*
(
double
)(
currReading
);
displayPosition
+=
(
displayPosition
<
0
)
?
(
360
)
:
(
0
);
displayPosition
-=
180
;
if
(
isStreaming
&&
isInPositionMode
){
Serial
.
println
(
displayPosition
);
}
}
void
handleButton
(){
// If button was pressed
if
(
digitalRead
(
BUTTON_PIN
)
==
HIGH
){
buttonTimer
=
millis
();
while
(
digitalRead
(
BUTTON_PIN
)
==
HIGH
){
}
if
(
millis
()
-
buttonTimer
>
LONG_PRESS
){
// Detected a long press, swap modes
isInPositionMode
=
!
isInPositionMode
;
}
else
{
// Short press occured, grab current position to reset home position regardless of mode
homePosition
=
analogRead
(
DATA_PIN
);
homePosition
+=
512
;
}
// LED on if we're in position mode, off if we're in rate mode
digitalWrite
(
LED_BUILTIN
,
isInPositionMode
);
}
}
void
handleSerial
(){
if
(
Serial
.
available
()
>
1
&&
Serial
.
find
(
'@'
)){
char
readChar
=
Serial
.
read
();
switch
(
readChar
){
case
'h'
:
// Set current position to be zero
homePosition
=
analogRead
(
DATA_PIN
);
homePosition
+=
512
;
break
;
case
'w'
:
// Change wait time between cycles (in microseconds)
w
=
Serial
.
readStringUntil
(
'!'
).
toInt
();
if
(
w
>
0
&&
w
<
1000000
){
time_step
=
w
;
}
break
;
case
't'
:
// Toggle mode
isInPositionMode
=
!
isInPositionMode
;
// LED on if we're in position mode, off if we're in rate mode
digitalWrite
(
LED_BUILTIN
,
isInPositionMode
);
break
;
case
'p'
:
// Set to position mode
isInPositionMode
=
true
;
// LED on if we're in position mode, off if we're in rate mode
digitalWrite
(
LED_BUILTIN
,
isInPositionMode
);
break
;
case
'r'
:
// Set to rate mode
isInPositionMode
=
false
;
// LED on if we're in position mode, off if we're in rate mode
digitalWrite
(
LED_BUILTIN
,
isInPositionMode
);
break
;
case
'P'
:
// Print position
Serial
.
println
(
displayPosition
);
break
;
case
'R'
:
// Print rate
Serial
.
println
(
rate
);
break
;
case
'v'
:
// Print value based on mode
if
(
isInPositionMode
){
Serial
.
println
(
displayPosition
);
}
else
{
Serial
.
println
(
rate
);
}
break
;
case
's'
:
// Enable Streaming
isStreaming
=
true
;
break
;
case
'd'
:
// Disable Streaming
isStreaming
=
false
;
break
;
case
'x'
:
// Print raw value
Serial
.
println
(
analogRead
(
DATA_PIN
));
break
;
}
}
}
void
setup
()
{
// put your setup code here, to run once:
Serial
.
begin
(
9600
);
pinMode
(
BUTTON_PIN
,
INPUT
);
pinMode
(
DATA_PIN
,
INPUT
);
pinMode
(
LED_BUILTIN
,
OUTPUT
);
homePosition
=
analogRead
(
DATA_PIN
);
// Capture home position
homePosition
+=
512
;
// Offset home position to middle value
lastTime
=
micros
();
lastReading
=
analogRead
(
DATA_PIN
);
// LED on if we're in position mode, off if we're in rate mode
digitalWrite
(
LED_BUILTIN
,
isInPositionMode
);
myRate
.
begin
();
}
void
loop
()
{
currTime
=
micros
();
currReading
=
(
analogRead
(
DATA_PIN
)
-
homePosition
+
1024
)
%
1024
;
// Switch mode after long button press, zero position after short button press
handleButton
();
// Find the position
positionMode
();
// Find the rate
rateMode
();
// Handle commands from the PC
handleSerial
();
// Reset vars for next cycle
lastTime
=
currTime
;
lastReading
=
currReading
;
delayMicroseconds
(
time_step
);
}
This diff is collapsed.
Click to expand it.
Cole Hunt
@crhunt
mentioned in issue
#145 (closed)
·
2 years ago
mentioned in issue
#145 (closed)
mentioned in issue #145
Toggle commit list
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