Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
Arduino Codes
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
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
Medics
Arduino Codes
Commits
e8749b7f
"verif/git@git.soton.ac.uk:soclabs/nanosoc_tech.git" did not exist on "6e12d2aaf45fba981f2f03fa9f95c3206ce9f06f"
Commit
e8749b7f
authored
2 months ago
by
mhby1g21
Browse files
Options
Downloads
Patches
Plain Diff
fixed imu but forward backward reversed
parent
83d9d019
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
wifi-integrated.ino
+96
-75
96 additions, 75 deletions
wifi-integrated.ino
with
96 additions
and
75 deletions
wifi-integrated.ino
+
96
−
75
View file @
e8749b7f
...
...
@@ -43,9 +43,9 @@ enum MovementState {
MovementState
currentState
=
STATE_STOP
;
unsigned
long
lastCommandTime
=
0
;
const
unsigned
long
commandTimeout
=
0
;
// Disabled b
y setting to 0
const
unsigned
long
commandTimeout
=
0
;
// Disabled b
ecause not working
// PID con
trol settings
// PID con
stants (tune these) - Matching Medical_V2.ino
float
Kp
=
1.5
;
// Proportional gain
float
Ki
=
0.1
;
// Integral gain
float
Kd
=
0.05
;
// Derivative gain
...
...
@@ -57,7 +57,7 @@ int leftSpeed = 0;
int
rightSpeed
=
0
;
int
baseSpeed
=
200
;
// Base motor speed (0-255)
//
IMU setup
//
BMI270 IMU object
BMI270
imu
;
uint8_t
i2cAddress
=
BMI2_I2C_PRIM_ADDR
;
// 0x68
bool
imuConnected
=
false
;
...
...
@@ -84,7 +84,7 @@ IPAddress dns(8, 8, 8, 8); // Google DNS
void
setup
()
{
// Initialize serial
Serial
.
begin
(
9600
);
Serial
.
println
(
"EMG-controlled
ca
r with IMU stabilization starting up..."
);
Serial
.
println
(
"EMG-controlled
wheelchai
r with IMU stabilization starting up..."
);
// Initialize motor pins
pinMode
(
ENA
,
OUTPUT
);
...
...
@@ -156,20 +156,99 @@ void loop() {
}
}
// Apply movement control based on current state
switch
(
currentState
)
{
case
STATE_FORWARD
:
moveForward
();
break
;
case
STATE_BACKWARD
:
moveBackward
();
break
;
case
STATE_LEFT
:
turnLeft
();
break
;
case
STATE_RIGHT
:
turnRight
();
break
;
case
STATE_STOP
:
// Motors already stopped
break
;
}
// Check for command timeout (safety feature)
if
(
commandTimeout
>
0
&&
currentState
!=
STATE_STOP
&&
(
currentMillis
-
lastCommandTime
>=
commandTimeout
))
{
Serial
.
println
(
"Command timeout - stopping motors for safety"
);
executeStop
();
}
// Apply IMU-based PID control if moving and IMU is enabled
if
(
imuConnected
&&
useIMU
&&
(
currentState
==
STATE_FORWARD
||
currentState
==
STATE_BACKWARD
))
{
applyIMUControl
();
}
// Move Forward - Using the exact structure from Medical_V2.ino
void
moveForward
()
{
// Set direction pins first
digitalWrite
(
IN2
,
HIGH
);
digitalWrite
(
IN1
,
LOW
);
digitalWrite
(
IN4
,
HIGH
);
digitalWrite
(
IN3
,
LOW
);
Direction
=
1
;
// Then apply IMU control if available
if
(
imuConnected
&&
useIMU
)
{
IMU_Control
();
}
else
{
// Without IMU, just use basic speed control
analogWrite
(
ENA
,
SPEED
);
analogWrite
(
ENB
,
SPEED
);
}
}
void
applyIMUControl
()
{
// Move Backwards - Using the exact structure from Medical_V2.ino
void
moveBackward
()
{
// Set direction pins first
digitalWrite
(
IN2
,
LOW
);
digitalWrite
(
IN1
,
HIGH
);
digitalWrite
(
IN4
,
LOW
);
digitalWrite
(
IN3
,
HIGH
);
Direction
=
-
1
;
// Then apply IMU control if available
if
(
imuConnected
&&
useIMU
)
{
IMU_Control
();
}
else
{
// Without IMU, just use basic speed control
analogWrite
(
ENA
,
SPEED
);
analogWrite
(
ENB
,
SPEED
);
}
}
// Turn Left - Using the exact structure from Medical_V2.ino
void
turnLeft
()
{
digitalWrite
(
IN2
,
HIGH
);
digitalWrite
(
IN1
,
LOW
);
digitalWrite
(
IN4
,
LOW
);
digitalWrite
(
IN3
,
LOW
);
Direction
=
0
;
// No IMU control for turning
analogWrite
(
ENA
,
SPEED
);
analogWrite
(
ENB
,
0
);
}
// Turn Right - Using the exact structure from Medical_V2.ino
void
turnRight
()
{
digitalWrite
(
IN2
,
LOW
);
digitalWrite
(
IN1
,
LOW
);
digitalWrite
(
IN4
,
HIGH
);
digitalWrite
(
IN3
,
LOW
);
Direction
=
0
;
// No IMU control for turning
analogWrite
(
ENA
,
0
);
analogWrite
(
ENB
,
SPEED
);
}
// Using the exact IMU_Control from Medical_V2.ino
void
IMU_Control
()
{
// Update IMU data
imu
.
getSensorData
();
...
...
@@ -189,13 +268,12 @@ void applyIMUControl() {
// PID output
float
output
=
Kp
*
error
+
Ki
*
integral
+
Kd
*
derivative
;
// Adjust motor speeds based on current state
if
(
currentState
==
STATE_FORWARD
)
{
Direction
=
1
;
// Adjust motor speeds based on direction
if
(
Direction
==
1
){
//Moving Forward
leftSpeed
=
baseSpeed
-
output
;
rightSpeed
=
baseSpeed
+
output
;
}
else
if
(
currentState
==
STATE_BACKWARD
)
{
Direction
=
-
1
;
}
else
if
(
Direction
=
=
-
1
){
//Moving Backward
leftSpeed
=
baseSpeed
+
output
;
rightSpeed
=
baseSpeed
-
output
;
}
...
...
@@ -305,7 +383,7 @@ bool connectToWiFi() {
printWiFiStatus
();
server
.
begin
();
Serial
.
println
(
"EMG-controlled
ca
r server started"
);
Serial
.
println
(
"EMG-controlled
wheelchai
r server started"
);
return
true
;
}
else
{
Serial
.
println
(
"
\n
Failed to connect to WiFi"
);
...
...
@@ -496,18 +574,6 @@ void executeForward() {
lastError
=
0
;
integral
=
0
;
// Motor A forward
digitalWrite
(
IN1
,
HIGH
);
digitalWrite
(
IN2
,
LOW
);
// Motor B forward
digitalWrite
(
IN3
,
HIGH
);
digitalWrite
(
IN4
,
LOW
);
// Set initial motor speeds
analogWrite
(
ENA
,
SPEED
);
analogWrite
(
ENB
,
SPEED
);
currentState
=
STATE_FORWARD
;
digitalWrite
(
MOVING_LED
,
HIGH
);
}
...
...
@@ -520,74 +586,31 @@ void executeBackward() {
lastError
=
0
;
integral
=
0
;
// Motor A backward
digitalWrite
(
IN1
,
LOW
);
digitalWrite
(
IN2
,
HIGH
);
// Motor B backward
digitalWrite
(
IN3
,
LOW
);
digitalWrite
(
IN4
,
HIGH
);
// Set initial motor speeds
analogWrite
(
ENA
,
SPEED
);
analogWrite
(
ENB
,
SPEED
);
currentState
=
STATE_BACKWARD
;
digitalWrite
(
MOVING_LED
,
HIGH
);
}
void
executeLeft
()
{
Serial
.
println
(
"Turning left"
);
// Motor A forward
digitalWrite
(
IN1
,
HIGH
);
digitalWrite
(
IN2
,
LOW
);
analogWrite
(
ENA
,
SPEED
);
// Motor B backward for sharp turn
digitalWrite
(
IN3
,
LOW
);
digitalWrite
(
IN4
,
HIGH
);
analogWrite
(
ENB
,
TURN_SPEED
);
currentState
=
STATE_LEFT
;
digitalWrite
(
MOVING_LED
,
HIGH
);
}
void
executeRight
()
{
Serial
.
println
(
"Turning right"
);
// Motor A backward for sharp turn
digitalWrite
(
IN1
,
LOW
);
digitalWrite
(
IN2
,
HIGH
);
analogWrite
(
ENA
,
TURN_SPEED
);
// Motor B forward
digitalWrite
(
IN3
,
HIGH
);
digitalWrite
(
IN4
,
LOW
);
analogWrite
(
ENB
,
SPEED
);
currentState
=
STATE_RIGHT
;
digitalWrite
(
MOVING_LED
,
HIGH
);
}
void
executeStop
()
{
Serial
.
println
(
"Stopping motors"
);
// Stop both motors
digitalWrite
(
IN1
,
LOW
);
digitalWrite
(
IN2
,
LOW
);
analogWrite
(
ENA
,
0
);
digitalWrite
(
IN3
,
LOW
);
digitalWrite
(
IN4
,
LOW
);
analogWrite
(
ENB
,
0
);
stopMotors
();
currentState
=
STATE_STOP
;
digitalWrite
(
MOVING_LED
,
LOW
);
}
void
stopMotors
()
{
// Stop both motors immediately
(for initialization or emergencies)
// Stop both motors immediately
digitalWrite
(
IN1
,
LOW
);
digitalWrite
(
IN2
,
LOW
);
analogWrite
(
ENA
,
0
);
...
...
@@ -595,8 +618,6 @@ void stopMotors() {
digitalWrite
(
IN3
,
LOW
);
digitalWrite
(
IN4
,
LOW
);
analogWrite
(
ENB
,
0
);
currentState
=
STATE_STOP
;
}
void
blinkLED
(
int
pin
,
int
times
,
int
delayMs
)
{
...
...
@@ -621,7 +642,7 @@ void printWiFiStatus() {
Serial
.
print
(
rssi
);
Serial
.
println
(
" dBm"
);
Serial
.
println
(
"To control this
ca
r, send HTTP requests to:"
);
Serial
.
println
(
"To control this
wheelchai
r, send HTTP requests to:"
);
Serial
.
print
(
"http://"
);
Serial
.
print
(
ip
);
Serial
.
println
(
"/command"
);
...
...
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