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
1990abba
Commit
1990abba
authored
2 months ago
by
mhby1g21
Browse files
Options
Downloads
Patches
Plain Diff
better fist animation
parent
d11520bb
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
wifi-test/wifi-with-ui.ino
+101
-30
101 additions, 30 deletions
wifi-test/wifi-with-ui.ino
with
101 additions
and
30 deletions
wifi-test/wifi-with-ui.ino
+
101
−
30
View file @
1990abba
...
@@ -35,7 +35,7 @@ int pinkyTarget = 90;
...
@@ -35,7 +35,7 @@ int pinkyTarget = 90;
// Movement parameters
// Movement parameters
const
int
STEP_DELAY
=
5
;
// Delay between steps (ms)
const
int
STEP_DELAY
=
5
;
// Delay between steps (ms)
const
int
STEP_SIZE
=
15
;
// Size of each step in degrees
const
int
STEP_SIZE
=
15
;
// Size of each step in degrees
void
setup
()
{
void
setup
()
{
Serial
.
begin
(
9600
);
Serial
.
begin
(
9600
);
...
@@ -72,6 +72,49 @@ void setup() {
...
@@ -72,6 +72,49 @@ void setup() {
server
.
begin
();
server
.
begin
();
}
}
// Move all servos together for fist motion
void
moveAllServosSmooth
(
int
targetPos
)
{
int
maxSteps
=
0
;
// Calculate the maximum number of steps needed
maxSteps
=
max
(
maxSteps
,
abs
(
indexTarget
-
indexPos
));
maxSteps
=
max
(
maxSteps
,
abs
(
middleTarget
-
middlePos
));
maxSteps
=
max
(
maxSteps
,
abs
(
ringTarget
-
ringPos
));
maxSteps
=
max
(
maxSteps
,
abs
(
pinkyTarget
-
pinkyPos
));
if
(
maxSteps
>
0
)
{
// Calculate step size for each finger
float
indexStep
=
(
float
)(
indexTarget
-
indexPos
)
/
maxSteps
;
float
middleStep
=
(
float
)(
middleTarget
-
middlePos
)
/
maxSteps
;
float
ringStep
=
(
float
)(
ringTarget
-
ringPos
)
/
maxSteps
;
float
pinkyStep
=
(
float
)(
pinkyTarget
-
pinkyPos
)
/
maxSteps
;
// Move all fingers together
for
(
int
i
=
1
;
i
<=
maxSteps
;
i
++
)
{
// Calculate next position for each finger
int
newIndexPos
=
indexPos
+
round
(
indexStep
*
i
);
int
newMiddlePos
=
middlePos
+
round
(
middleStep
*
i
);
int
newRingPos
=
ringPos
+
round
(
ringStep
*
i
);
int
newPinkyPos
=
pinkyPos
+
round
(
pinkyStep
*
i
);
// Move all servos
index_f
.
write
(
newIndexPos
);
middle
.
write
(
newMiddlePos
);
ring
.
write
(
newRingPos
);
pinky
.
write
(
newPinkyPos
);
delay
(
STEP_DELAY
);
}
// Update the current positions
indexPos
=
indexTarget
;
middlePos
=
middleTarget
;
ringPos
=
ringTarget
;
pinkyPos
=
pinkyTarget
;
}
}
// Individual servo movement for single finger control
void
moveServoSmooth
(
Servo
&
servo
,
int
&
currentPos
,
int
targetPos
)
{
void
moveServoSmooth
(
Servo
&
servo
,
int
&
currentPos
,
int
targetPos
)
{
if
(
currentPos
<
targetPos
)
{
if
(
currentPos
<
targetPos
)
{
for
(
int
pos
=
currentPos
;
pos
<=
targetPos
;
pos
+=
STEP_SIZE
)
{
for
(
int
pos
=
currentPos
;
pos
<=
targetPos
;
pos
+=
STEP_SIZE
)
{
...
@@ -89,11 +132,29 @@ void moveServoSmooth(Servo &servo, int ¤tPos, int targetPos) {
...
@@ -89,11 +132,29 @@ void moveServoSmooth(Servo &servo, int ¤tPos, int targetPos) {
}
}
void
loop
()
{
void
loop
()
{
// Handle smooth servo movements
// Check if any finger needs to move to its target position
if
(
indexPos
!=
indexTarget
)
moveServoSmooth
(
index_f
,
indexPos
,
indexTarget
);
bool
needToMove
=
(
indexPos
!=
indexTarget
)
||
if
(
middlePos
!=
middleTarget
)
moveServoSmooth
(
middle
,
middlePos
,
middleTarget
);
(
middlePos
!=
middleTarget
)
||
if
(
ringPos
!=
ringTarget
)
moveServoSmooth
(
ring
,
ringPos
,
ringTarget
);
(
ringPos
!=
ringTarget
)
||
if
(
pinkyPos
!=
pinkyTarget
)
moveServoSmooth
(
pinky
,
pinkyPos
,
pinkyTarget
);
(
pinkyPos
!=
pinkyTarget
);
// If all fingers have the same target (fist motion), move them simultaneously
bool
sameFistMotion
=
(
indexTarget
==
middleTarget
&&
middleTarget
==
ringTarget
&&
ringTarget
==
pinkyTarget
);
if
(
needToMove
)
{
if
(
sameFistMotion
)
{
// Move all fingers together
moveAllServosSmooth
(
indexTarget
);
}
else
{
// Move individual fingers
if
(
indexPos
!=
indexTarget
)
moveServoSmooth
(
index_f
,
indexPos
,
indexTarget
);
if
(
middlePos
!=
middleTarget
)
moveServoSmooth
(
middle
,
middlePos
,
middleTarget
);
if
(
ringPos
!=
ringTarget
)
moveServoSmooth
(
ring
,
ringPos
,
ringTarget
);
if
(
pinkyPos
!=
pinkyTarget
)
moveServoSmooth
(
pinky
,
pinkyPos
,
pinkyTarget
);
}
}
WiFiClient
client
=
server
.
available
();
WiFiClient
client
=
server
.
available
();
...
@@ -127,20 +188,31 @@ void loop() {
...
@@ -127,20 +188,31 @@ void loop() {
client
.
println
(
".button:hover { background: #45a049; }"
);
client
.
println
(
".button:hover { background: #45a049; }"
);
client
.
println
(
".preset { background: #2196F3; }"
);
client
.
println
(
".preset { background: #2196F3; }"
);
client
.
println
(
".reset { background: #f44336; }"
);
client
.
println
(
".reset { background: #f44336; }"
);
client
.
println
(
".
wave
{ background: #
ff980
0; }"
);
client
.
println
(
".
fist
{ background: #
9C27B
0; }"
);
client
.
println
(
"</style>"
);
client
.
println
(
"</style>"
);
client
.
println
(
"</head>"
);
client
.
println
(
"</head>"
);
client
.
println
(
"<body>"
);
client
.
println
(
"<body>"
);
client
.
println
(
"<h1>Robotic Hand Control Panel</h1>"
);
client
.
println
(
"<h1>Robotic Hand Control Panel</h1>"
);
// Fist motion controls (added at top for prominence)
client
.
println
(
"<div class='control-group'>"
);
client
.
println
(
"<h2>Fist Motion Controls</h2>"
);
client
.
println
(
"<button class='button fist' onclick='allFingers(0)'>Close Fist</button>"
);
client
.
println
(
"<button class='button fist' onclick='allFingers(180)'>Open Hand</button>"
);
client
.
println
(
"<button class='button fist' onclick='fistGrasp()'>Grasp Motion</button>"
);
client
.
println
(
"</div>"
);
// Individual finger controls
// Individual finger controls
String
fingers
[]
=
{
"Index"
,
"Middle"
,
"Ring"
,
"Pinky"
};
String
fingers
[]
=
{
"Index"
,
"Middle"
,
"Ring"
,
"Pinky"
};
int
positions
[]
=
{
indexPos
,
middlePos
,
ringPos
,
pinkyPos
};
int
positions
[]
=
{
indexPos
,
middlePos
,
ringPos
,
pinkyPos
};
String
endpoints
[]
=
{
"index"
,
"middle"
,
"ring"
,
"pinky"
};
String
endpoints
[]
=
{
"index"
,
"middle"
,
"ring"
,
"pinky"
};
client
.
println
(
"<div class='control-group'>"
);
client
.
println
(
"<h2>Individual Finger Controls</h2>"
);
for
(
int
i
=
0
;
i
<
4
;
i
++
)
{
for
(
int
i
=
0
;
i
<
4
;
i
++
)
{
client
.
println
(
"<div
class='control-group
'>"
);
client
.
println
(
"<div
style='margin-bottom:10px;
'>"
);
client
.
println
(
"<h3>"
+
fingers
[
i
]
+
" Finger</h3>"
);
client
.
println
(
"<h3>"
+
fingers
[
i
]
+
" Finger</h3>"
);
client
.
println
(
"<input type='range' class='slider' min='0' max='180' value='"
+
String
(
positions
[
i
])
+
"' "
);
client
.
println
(
"<input type='range' class='slider' min='0' max='180' value='"
+
String
(
positions
[
i
])
+
"' "
);
client
.
println
(
"oninput='updateFinger(
\"
"
+
endpoints
[
i
]
+
"
\"
, this.value)'>"
);
client
.
println
(
"oninput='updateFinger(
\"
"
+
endpoints
[
i
]
+
"
\"
, this.value)'>"
);
...
@@ -150,19 +222,12 @@ void loop() {
...
@@ -150,19 +222,12 @@ void loop() {
client
.
println
(
"<button class='button' onclick='updateFinger(
\"
"
+
endpoints
[
i
]
+
"
\"
, 180)'>Open</button>"
);
client
.
println
(
"<button class='button' onclick='updateFinger(
\"
"
+
endpoints
[
i
]
+
"
\"
, 180)'>Open</button>"
);
client
.
println
(
"</div>"
);
client
.
println
(
"</div>"
);
}
}
// Preset controls
client
.
println
(
"<div class='control-group'>"
);
client
.
println
(
"<h3>Preset Controls</h3>"
);
client
.
println
(
"<button class='button preset' onclick='allFingers(0)'>Close All</button>"
);
client
.
println
(
"<button class='button preset' onclick='allFingers(180)'>Open All</button>"
);
client
.
println
(
"<button class='button reset' onclick='resetPositions()'>Reset to 90°</button>"
);
client
.
println
(
"</div>"
);
client
.
println
(
"</div>"
);
//
Wave motion
controls
//
Additional preset
controls
client
.
println
(
"<div class='control-group'>"
);
client
.
println
(
"<div class='control-group'>"
);
client
.
println
(
"<h3>
Wave Motion
</h3>"
);
client
.
println
(
"<h3>
Other Controls
</h3>"
);
client
.
println
(
"<button class='button
wave
' onclick='
startWave()'>Start Wave
</button>"
);
client
.
println
(
"<button class='button
reset
' onclick='
resetPositions()'>Reset to 90°
</button>"
);
client
.
println
(
"</div>"
);
client
.
println
(
"</div>"
);
// JavaScript
// JavaScript
...
@@ -172,23 +237,20 @@ void loop() {
...
@@ -172,23 +237,20 @@ void loop() {
client
.
println
(
"}"
);
client
.
println
(
"}"
);
client
.
println
(
"function allFingers(angle) {"
);
client
.
println
(
"function allFingers(angle) {"
);
client
.
println
(
" ['index', 'middle', 'ring', 'pinky'].forEach(finger => {"
);
client
.
println
(
" fetch('/fist/' + angle);"
);
client
.
println
(
" updateFinger(finger, angle);"
);
client
.
println
(
" });"
);
client
.
println
(
"}"
);
client
.
println
(
"}"
);
client
.
println
(
"function resetPositions() {"
);
client
.
println
(
"function resetPositions() {"
);
client
.
println
(
" allFingers(90);"
);
client
.
println
(
" allFingers(90);"
);
client
.
println
(
"}"
);
client
.
println
(
"}"
);
client
.
println
(
"function startWave() {"
);
client
.
println
(
"function fistGrasp() {"
);
client
.
println
(
" let fingers = ['index', 'middle', 'ring', 'pinky'];"
);
client
.
println
(
" // Open hand first"
);
client
.
println
(
" let delay = 0;"
);
client
.
println
(
" allFingers(180);"
);
client
.
println
(
" fingers.forEach(finger => {"
);
client
.
println
(
" // Close after a delay"
);
client
.
println
(
" setTimeout(() => updateFinger(finger, 180), delay);"
);
client
.
println
(
" setTimeout(() => allFingers(0), 1500);"
);
client
.
println
(
" setTimeout(() => updateFinger(finger, 0), delay + 1000);"
);
client
.
println
(
" // Open again after another delay"
);
client
.
println
(
" delay += 500;"
);
client
.
println
(
" setTimeout(() => allFingers(180), 3000);"
);
client
.
println
(
" });"
);
client
.
println
(
"}"
);
client
.
println
(
"}"
);
client
.
println
(
"</script>"
);
client
.
println
(
"</script>"
);
...
@@ -223,7 +285,16 @@ void processRequest(String request) {
...
@@ -223,7 +285,16 @@ void processRequest(String request) {
Serial
.
println
(
"Command: "
+
command
);
Serial
.
println
(
"Command: "
+
command
);
// Parse command and set target positions
// Parse command and set target positions
if
(
command
.
startsWith
(
"/index/"
))
{
if
(
command
.
startsWith
(
"/fist/"
))
{
// Set all fingers to same position
int
angle
=
constrain
(
command
.
substring
(
6
).
toInt
(),
0
,
180
);
indexTarget
=
angle
;
middleTarget
=
angle
;
ringTarget
=
angle
;
pinkyTarget
=
angle
;
Serial
.
println
(
"Moving all fingers to "
+
String
(
angle
));
}
else
if
(
command
.
startsWith
(
"/index/"
))
{
indexTarget
=
constrain
(
command
.
substring
(
7
).
toInt
(),
0
,
180
);
indexTarget
=
constrain
(
command
.
substring
(
7
).
toInt
(),
0
,
180
);
Serial
.
println
(
"Moving index to "
+
String
(
indexTarget
));
Serial
.
println
(
"Moving index to "
+
String
(
indexTarget
));
}
}
...
...
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