Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
mattdr
supermileage_2018
Commits
00015f86
Commit
00015f86
authored
Jan 30, 2019
by
mattdr
Browse files
added some changes to scale the pot and keep it above a certain periood
parent
999f364a
Changes
3
Hide whitespace changes
Inline
Side-by-side
source_code/Teensy32/src/motor_ctrl.cpp
View file @
00015f86
...
...
@@ -16,9 +16,22 @@
const
int
led
=
LED_BUILTIN
;
//This scale factor scales the potentiometer
//
This scale factor scales the potentiometer
int
const
SCALE_FACTOR
=
20
;
// This is the PWM frequency and it is very important
int
FREQ
=
8000
;
//8000;
// This is the lower limit of PWM frequency
// its important for not breaking gate drivers
float
PERIOD
=
1
/
((
float
)
FREQ
);
// this helps in calculatations of the lower limit of pwm
float
PERIOD_OVER_255
=
PERIOD
/
255.0
;
void
turn_all_off
();
int
one_to_scaleFactor
(
int
x
);
void
set_phase
(
int
high1
,
int
high2
,
int
high3
,
int
low1
,
int
low2
,
int
low3
);
...
...
@@ -33,7 +46,7 @@ void motor_butt_control_precalculated(void);
void
motor_setup
(
void
)
{
// put your setup code here, to run once:
float
freq
=
8000.00
;
//8000;
pinMode
(
L1
,
OUTPUT
);
pinMode
(
L2
,
OUTPUT
);
...
...
@@ -44,29 +57,32 @@ void motor_setup(void) {
//PWM setup
analogWriteFrequency
(
H1
,
freq
);
analogWriteFrequency
(
H2
,
freq
);
analogWriteFrequency
(
H3
,
freq
);
analogWriteFrequency
(
H1
,
FREQ
);
analogWriteFrequency
(
H2
,
FREQ
);
analogWriteFrequency
(
H3
,
FREQ
);
turn_all_off
();
}
void
turn_all_off
()
{
analogWrite
(
H1
,
0
);
analogWrite
(
H2
,
0
);
analogWrite
(
H3
,
0
);
digitalWrite
(
L1
,
LOW
);
digitalWrite
(
L2
,
LOW
);
digitalWrite
(
L3
,
LOW
);
analogWrite
(
H1
,
0
);
analogWrite
(
H2
,
0
);
analogWrite
(
H3
,
0
);
}
// If this function recives a 1 it will check the potentiometer and
// return a int 0-255 of the value on the pot (scaled)
// if it recives a 0 it will return 0
int
one_to_scaleFactor
(
int
x
)
{
if
(
x
==
1
)
{
// NOTE: The scale factor of is the max value we want out of
// the potentiometer
return
get_pot_0_scaled
(
SCALE_FACTOR
);
return
get_pot_0_scaled
(
SCALE_FACTOR
,
PERIOD_OVER_255
);
}
else
{
return
0
;
...
...
@@ -131,7 +147,7 @@ void set_phase_float(float f1, float f2, float f3)
//deal with the potentiometer first
// NOTE: The scale factor of is the max value we want out of
// the potentiometer
int
potval
=
get_pot_0_scaled
(
SCALE_FACTOR
);
int
potval
=
get_pot_0_scaled
(
SCALE_FACTOR
,
PERIOD_OVER_255
);
f1
=
(
f1
*
potval
);
f2
=
(
f2
*
potval
);
...
...
source_code/Teensy32/src/utils.cpp
View file @
00015f86
...
...
@@ -34,11 +34,22 @@ int get_pot_1_inv(void)
//reads potentiometer on pin A0 and scales by scale_factor
//NOTE: scale factor should only be 0-255!
int
get_pot_0_scaled
(
int
scaleFactor
)
// TODO: make sure this code is not run too often it will take time
int
get_pot_0_scaled
(
int
scaleFactor
,
float
period_over_255
)
{
volatile
float
potval
=
analogRead
(
A0
);
volatile
float
raw_potval
=
analogRead
(
A0
);
float
potval
=
raw_potval
;
potval
=
potval
/
1024
;
potval
=
potval
*
scaleFactor
;
potval
-=
3.0
;
// this just disables the first three pot ticks
// This code is used to make sure that the pwm nver has a period
// lower than 250 ns (as this may be problematic for gate drivrs)
if
((
period_over_255
*
potval
)
<
250.0E-9
){
potval
=
0.0
;
}
return
(
int
)
potval
;
}
...
...
source_code/Teensy32/src/utils.h
View file @
00015f86
...
...
@@ -4,7 +4,7 @@
void
block_until_spacebar
(
void
);
void
do_hard_coded_offset
(
void
);
int
get_pot_1_inv
(
void
);
int
get_pot_0_scaled
(
int
scaleFactor
);
int
get_pot_0_scaled
(
int
scaleFactor
,
float
period_over_255
);
void
locate_state_positions
(
void
);
void
direct_position_control
(
void
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment