Notifications
Clear all

REVISIT: Using SD Cards with Arduino - Record Servo Motor Movements

58 Posts
6 Users
7 Reactions
4,383 Views
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2544
 

@inq @hinobot

I understand what you're saying and if the devices were extremely fast, then I'd agree. But we're talking cheap servos which are slow. If we imagine your example of multiple heads, consider the difficulty of simulating simultaneity of turning them while adjusting lips, jaw openings, eye rolls and so on.

Even with reading and interpreting one line at a time it'll still be slow, but at least the cumulative effects of all movements will be correct every "tick" microseconds. "tick" will, of course, depend on the number of servos, their speed and the relative distance each has to move.

A superior (although much more difficult) approach would be to manage each pot to collect and store (for each servo) an angular displacement and an elapsed time for that movement. The playback would then read that combination (ID, change in angle, time for change) and then update the position of the servo by moving change/time x loop time. That would be beneficial in keeping all of the movement relatively in synch and cut down on data storage as well. But it'd be hell to implement 🙂

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2544
 
Posted by: @inq
  1. I'd just wrap it up in a simple C++ class for one servo.  From then on you simply add servos to your heart's delight and no changes to file format or to the main sketch's logic.

But as I mentioned... there are as many opinions here as there are people.  It is totally what makes the most sense to @hinobot and his daughters.  Some people think procedurally.  Fewer people (I have to admit) thing Object Oriented.

I agree completely about handling it (either way) with a class.

But, as I understand it, hinobot and his daughters aren't even C programmers (yet :)) and I think this project will be more difficult than they imagined in the first place. I don't want to scare them off !

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2045
 

@hinobot 

I don't have any attachment to the code posted.  The original code posted by DroneBot Workshop 2019 works perfect for my needs, one to record, the other to play back.  Its just fine, I just need to add the ability to capture 4 pots and 4 servos, and play them back at the same rate recorded.

I have ordered the sd card module so when it arrives I will give it a go and maybe in the meantime figure out how to modify Bill's code to use 4 servos but it may take time and you may have is all working before then 🙂

 

 


   
HinoBot reacted
ReplyQuote
HinoBot
(@hinobot)
Member
Joined: 2 years ago
Posts: 6
Topic starter  

@robotbuilder Thank you, I got all the servos working with modifying the original code, the rub is when we try to record each channel on the SD card, and then as @bill mentions, there is no time code during the recording, so it unceremoniously goes through the entire movements very fast (on one channel). 


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2045
 

@hinobot 

You can use millis() for timing.

For play you can read the card and update the next positions in fixed intervals of time.

For record you can read the pots at the same fixed time intervals and save the positions on the card.

 

unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 1000;  //the value is a number of milliseconds

void setup()
{
  Serial.begin(115200);  //start Serial in case we need to print debugging info
  // other setup stuff here
  startMillis = millis();  //initial start time
}

void loop()
{
  currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  if (currentMillis - startMillis >= period)  //test whether the period has elapsed
  {
    // ======================     DO YOU THING HERE  ================================
    //                        READ and UPDATE POSITIONS
    // ============================================================================
    startMillis = currentMillis;  //IMPORTANT to save the start time.
  }
}

 

 


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2544
 

@robotbuilder @hinobot

It would probably be better to use a button to signify that you're finished adjusting all the pots (especially when you add more). That would allow you plenty of time to carefully adjust each pot (and therefore servo) to just the right place. When the button is pushed, those positions can be collected, converted to a string and then written out to the SD card very quickly.

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2045
 

@hinobot

@will
It would probably be better to use a button to signify that you're finished adjusting all the pots (especially when you add more).

Ways to synchronize movements (a kinetic melody) I think is a major consideration when recording them. Also the ability to edit the movements. Usually with animatronics you have synchronized speech or other lights and sounds.

I probably wouldn't record movements rather generate them from code as my interest is robotics. This could also have sensors to react to anyone that comes into the room to make those eyes and head follow people. Much spookier if the machine reacts to stimuli 🙂

Here is an example from James Button for smoother movements.

It would help to own a 3d printer and be able to design your own parts.

 


   
Inq reacted
ReplyQuote
Inq
 Inq
(@inq)
Member
Joined: 2 years ago
Posts: 1900
 

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, WiFi Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@zander

Posted by: @zander

@will Ok, I checked the original sketch from Bill. If they want to enlarge that to 3 or 4, isn't that a  good place to use a class? (can't believe I just said that)

Me either, LOL!

Looking forward to an example to suit!

Cheers


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7195
 

@frogandtoad Not likely you will ever see anything written by me, I am barely able to read the code. Maybe modify something very similar but that's about it.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2544
 
Posted by: @inq
Posted by: @will

I understand what you're saying and if the devices were extremely fast, then I'd agree. But we're talking cheap servos which are slow.

I'm not sure I understand your point.  In my opinion, them being slow REQUIRES you to have a timing value for each for when you read the analog pin connected to each potentiometer and writing it to a file and then writing to the servo to actually move it.  If you use one timestamp for potentially dozens of pots/servos, by the time you write the last one it might be significant fractions of a second.  When played back they will not be in sync visually.

That was my point. Somebody mentioned Claymation earlier in the thread. There, the creator adjusts all of the parts of the animation that need to move and then takes a snapshot and then moves on to the next frame.

Here, you can set all the servos to their next position and then take a snapshot of them. But during playback you have to wait for all of them to be moved. So the timing has to be based on the playback, and the servo settings have to be set according to the distance they'll have to travel during that time.

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
Inq
 Inq
(@inq)
Member
Joined: 2 years ago
Posts: 1900
 
Posted by: @will

That was my point. Somebody mentioned Claymation earlier in the thread. There, the creator adjusts all of the parts of the animation that need to move and then takes a snapshot and then moves on to the next frame.

Here, you can set all the servos to their next position and then take a snapshot of them. But during playback you have to wait for all of them to be moved. So the timing has to be based on the playback, and the servo settings have to be set according to the distance they'll have to travel during that time.

Now, I see our differences... I completely agree the sketches I provided will serve poorly in that case.  I was the one that made that allusion to Claymation type choreography, but @hinobot came back with this:

Posted by: @hinobot

We are building a pair of eyes that go in a painting for our haunted house.  The eyes remain closed for some time, open, move around for a bit, stare off for a moment, and then close.  The idea is that the pattern repeat over and over again.  Think of a tape recorder that captures information in real time and then plays it back again.

... that he wants to record in real-time (not step by step like I had guessed).  I'm now guessing he and his daughters work together to drive the eyes to their desired choreography.  His scenario actually makes the coding problem quite simple.  Both of the sketches (Recorder/Playback Sketches).  Keeping them in two Sketches also make it easy to study and digest what is going on in the coding.  A wise choice on his part.  The programs are less than 70 lines if you take out all of the comments and they should be pretty easy to digest if he studies the comments and my logic summary - Logic Summary

Although I only tested with two servos, four should not be a problem and they could make a whole animatronic body with dozens of servos with the same code.  With some good practice with coding, they could come up with ways of combining files... say one controlling eyes blinking occasionally and another controlling head movements into one script.  

I thought it a fun project in my insomnia last night.  I'll probably use it in something someday.  Maybe they or someone else will improve on it.  😎

VBR,

Inq 

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, WiFi Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2544
 
Posted by: @inq

 but @hinobot came back with this:

Posted by: @hinobot

We are building a pair of eyes that go in a painting for our haunted house.  The eyes remain closed for some time, open, move around for a bit, stare off for a moment, and then close.  The idea is that the pattern repeat over and over again.  Think of a tape recorder that captures information in real time and then plays it back again.

In that specific case, I think @zander had the best idea. They should adjust the pots to get the desired servo readings with a desired elapsed time, make a note of the time and the ones that changed and so on. However, instead of recording the movements on an SD card, they should just write a sketch that exhibits pauses and executes servo.write commands according to the notes made. That would be totally editable to adjust timing and movement, but it would take up too much space for very long sequences.

Personally, I'm skeptical that three people can cope with 4 pots being manipulated "in real time", I feel certain that somebody will over- or under- adjust their pot and mess up that "take". But then, I'm a life-long cynic so take this with a handful of salt 🙂

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
HinoBot
(@hinobot)
Member
Joined: 2 years ago
Posts: 6
Topic starter  

@inq Thank you so much for the code and layout, we just purchased the ACEIRMC ESP8266 module from Amazon, we will upload your code and see if it works for our needs... thanks!


   
ReplyQuote
Inq
 Inq
(@inq)
Member
Joined: 2 years ago
Posts: 1900
 
Posted by: @hinobot

@inq Thank you so much for the code and layout, we just purchased the ACEIRMC ESP8266 module from Amazon, we will upload your code and see if it works for our needs... thanks!

I sure hope it works out for you.  If it doesn't, you know where tech support is. 😉 

Years ago I bought a bunch of the shields for the ESP8266 WeMos that I like to use.  I put them in a drawer and forgot about them.  I just sorted through them and one of them is a SD card shield.  I'm using your code sample from your OP as a guide.  I'll revise the code and re-post it.

VBR,

Inq

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, WiFi Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
ReplyQuote
Page 3 / 4