Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
MechaRaptor - Arduino Controller
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
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
Aerospace and Mechanical Engineering
tlambert
MechaRaptor - Arduino Controller
Commits
2aeeb284
Verified
Commit
2aeeb284
authored
2 years ago
by
Thomas Lambert
Browse files
Options
Downloads
Patches
Plain Diff
fix(wip): proper angle reading
parent
8a05f0ae
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
controller/controller.ino
+34
-38
34 additions, 38 deletions
controller/controller.ino
controller/mechaRaptor.json
+1
-1
1 addition, 1 deletion
controller/mechaRaptor.json
with
35 additions
and
39 deletions
controller/controller.ino
+
34
−
38
View file @
2aeeb284
...
...
@@ -48,7 +48,7 @@ const int gMIN_ESC_VAL = 40; // Min value for ESC to start motor (trial-error
Servo
gEsc1
;
// ESC for front motor
Servo
gEsc2
;
// ESC for aft motor
int
gEsc_val
=
0
;
// ESC angle (to use with the Servo library) [0-180]
int
gEsc_val
=
0
;
// ESC angle (to use with the Servo library) [0-180]
String
gMode
;
// Mode of operation
...
...
@@ -56,7 +56,8 @@ String gMode; // Mode of operation
ARDUINO SETUP
*******************************************************************************/
void
setup
()
{
Serial
.
begin
(
9600
);
//115200 for proper angle measurements, 9600 otherwise
//Serial.begin(9600); //[DEBUG]: For debugging only
Serial
.
begin
(
115200
);
// Need for good precision
Serial
.
print
(
"[INFO]: Attaching ESCs ... "
);
delay
(
5000
);
// Prevents motor from starting automatically with serial
...
...
@@ -66,13 +67,15 @@ void setup() {
pinMode
(
SWITCH_PIN
,
INPUT
);
for
(
int
i
=
0
;
i
<
sizeof
(
gCSN_PINS
);
i
++
)
{
pinMode
(
gCSN_PINS
[
i
],
OUTPUT
);
digitalWrite
(
gCSN_PINS
[
i
],
HIGH
);
}
pinMode
(
CLK_PIN
,
OUTPUT
);
pinMode
(
DO_PIN
,
INPUT
);
digitalWrite
(
CLK_PIN
,
HIGH
);
// [DEBUG] Comment all that as long as PCB not properly soldered
//for (int i = 0; i < sizeof(gCSN_PINS); i++) {
// pinMode(gCSN_PINS[i], OUTPUT);
// digitalWrite(gCSN_PINS[i], HIGH);
//}
//pinMode(CLK_PIN, OUTPUT);
//pinMode(DO_PIN, INPUT);
//digitalWrite(CLK_PIN, HIGH);
// Initialize modes
int
switch_state
=
digitalRead
(
SWITCH_PIN
);
...
...
@@ -107,37 +110,28 @@ void loop() {
int
potent
=
analogRead
(
A0
);
// Potentiometer value
unsigned
int
wing_angles
[
4
];
// Wing angles (1L, 1R, 2L, 2R)
if
(
gMode
==
"SERIAL"
){
/* If nothing new, just continue
* If serial input, change value
* If serial is 0, stop
*/
if
(
Serial
.
available
()){
int
old
=
gEsc_val
;
gEsc_val
=
Serial
.
parseFloat
();
if
(
gEsc_val
==
0
){
StopMotor
(
old
);
delay
(
2000
);
exit
(
0
);
}
}
if
(
gMode
==
"SERIAL"
and
Serial
.
available
()){
//[FIXME] Get value properly
gEsc_val
=
40
;
float
input
=
Serial
.
parseFloat
();
if
(
input
==
0
){
StopMotor
(
gEsc_val
);
}
else
{
gEsc_val
=
FreqToEsc
(
input
);
}
}
else
if
(
gMode
==
"MANUAL"
){
gEsc_val
=
map
(
potent
,
0
,
1023
,
0
,
180
);
}
// Write value to ESCs
gEsc1
.
write
(
gEsc_val
);
gEsc2
.
write
(
gEsc_val
);
// [FIXME] Use readsensors
wing_angles
[
0
]
=
(
unsigned
int
)
0
;
wing_angles
[
1
]
=
(
unsigned
int
)
1
;
wing_angles
[
2
]
=
(
unsigned
int
)
2
;
wing_angles
[
3
]
=
(
unsigned
int
)
3
;
// Read value from rotary encoders
for
(
int
i
=
0
;
i
<
sizeof
(
gCSN_PINS
);
i
++
){
wing_angles
[
i
]
=
ReadSensor
(
gCSN_PINS
[
i
]);
}
SerialPrint
(
gMode
,
potent
,
gEsc_val
,
wing_angles
);
}
...
...
@@ -147,7 +141,6 @@ void loop() {
OTHER FUNCTIONS
*******************************************************************************/
// Setup serial
// Get the initial value for the ESCs in serial mode
float
SetupSerial
()
{
...
...
@@ -162,9 +155,9 @@ float SetupSerial() {
Serial
.
print
(
max_freq
);
Serial
.
println
(
" [Hz]"
);
while
(
input
>
m
in
_freq
or
input
<
m
ax
_freq
){
while
(
input
>
m
ax
_freq
or
input
<
m
in
_freq
){
// Loop indefinitely while waiting for serial input
while
(
Serial
.
available
()
==
0
)
{}
while
(
Serial
.
available
()
==
0
)
{
}
input
=
Serial
.
parseFloat
();
if
(
input
>
max_freq
or
input
<
min_freq
)
{
...
...
@@ -192,7 +185,10 @@ int FreqToEsc(float freq) {
if
(
esc_val
<
gMIN_ESC_VAL
)
{
esc_val
=
gMIN_ESC_VAL
;
}
else
if
(
esc_val
>
gMAX_ESC_VAL
)
{
esc_val
=
gMAX_ESC_VAL
;
}
return
esc_val
;
}
...
...
@@ -224,9 +220,9 @@ void StopMotor(int old_esc_val){
}
//
[TODO] FIX THAT
//
Read 12-bit rotary encoder (RobinL modified by linarism on forum.arduino.cc)
unsigned
int
ReadSensor
(
uint8_t
csn
){
unsigned
int
dataOu
t
=
0
;
unsigned
int
re
t
=
0
;
digitalWrite
(
csn
,
LOW
);
delayMicroseconds
(
1
);
//Waiting for Tclkfe
...
...
@@ -237,11 +233,11 @@ unsigned int ReadSensor(uint8_t csn){
delayMicroseconds
(
1
);
//Tclk/2
digitalWrite
(
CLK_PIN
,
HIGH
);
delayMicroseconds
(
1
);
//Tdo valid, like Tclk/2
dataOut
=
(
dataOu
t
<<
1
)
|
digitalRead
(
DO_PIN
);
//shift all the entering data to the left and past the pin state to it. 1e bit is MSB
ret
=
(
re
t
<<
1
)
|
digitalRead
(
DO_PIN
);
//shift all the entering data to the left and past the pin state to it. 1e bit is MSB
}
digitalWrite
(
csn
,
HIGH
);
//deselects the encoder from reading
return
dataOu
t
;
return
re
t
;
}
...
...
@@ -258,7 +254,7 @@ void SerialPrint(String mode, int potent, int esc_val, unsigned int wing_angles[
Serial
.
print
(
","
);
Serial
.
print
(
esc_val
);
Serial
.
print
(
","
);
Serial
.
print
(
freq
);
Serial
.
print
(
freq
);
// [FIXME] Is it useful?
for
(
int
i
=
0
;
i
<
sizeof
(
gCSN_PINS
);
i
++
){
Serial
.
print
(
","
);
Serial
.
print
(
wing_angles
[
i
]);
...
...
This diff is collapsed.
Click to expand it.
controller/mechaRaptor.json
+
1
−
1
View file @
2aeeb284
...
...
@@ -75,7 +75,7 @@
"log"
:
false
,
"max"
:
15
,
"min"
:
0
,
"title"
:
"
Flap.
freq."
,
"title"
:
"
Desired
freq."
,
"units"
:
"Hz"
,
"value"
:
"%5"
,
"widget"
:
"gauge"
...
...
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