Author Topic: Yet another digital setting circles project: ESP-DSC  (Read 39 times)

ciomasbure

  • Active Astronomer
  • ***
  • Posts: 125
  • Activity:
    20.67%
  • Reputation: +0/-0
    • View Profile
Yet another digital setting circles project: ESP-DSC
« on: December 28, 2017, 04:22:27 AM »
So a few years ago I created TeensyDSC, a Teensy3.1/RX-VN 171 based digital setting circles solution. I've been using that for a while now with great results, but I've always felt that it was too expensive (about $100 in parts). The ESP8266 has been around for a little while now and seems to have stabilized (originally it was quite buggy) so I decided to see if I could get this cheap microprocessor + WiFi adapter to work and shave off over $60 in the process.

The result of that is ESP-DSC. I've just finished bench testing it with a pair of 2.5k CPR encoders (10k after quadrature decoding) and it's run like a champ using SkySafari using the "Basic Encoder System" protocol at a 10Hz refresh rate. The next step is mounting it on my scope and trying it out with my 10k CPR (40k quadrature) encoders. Honestly, I'm not sure if the ESP-12E (or 12F) will have enough horsepower to keep up, but even still, I'd be pretty happy with it since it should only costs about $25-40 in parts.

More information can be found on my website here.

The next steps I have planned are:
Test on my scope and 10K encoders
Investigate adding a webserver for configuration
Clean up the PCB design and make it smaller
Design an enclosure that can be 3D printed



Larry Hopkins

  • Active Astronomer
  • ***
  • Posts: 121
  • Activity:
    16.67%
  • Reputation: +0/-0
    • View Profile
Re: Yet another digital setting circles project: ESP-DSC
« Reply #1 on: December 28, 2017, 10:39:39 PM »
Awesome! Thank you for sharing the progress, Aaron.

Edit: I didn't find any information for 2.5k CPR encoders you have tested on the website. Where I can get them?

proxbiovana

  • Active Astronomer
  • ***
  • Posts: 117
  • Activity:
    17.33%
  • Reputation: +0/-0
    • View Profile
Re: Yet another digital setting circles project: ESP-DSC
« Reply #2 on: January 03, 2018, 09:04:28 AM »
They're standard US Digital S2-2500-250-I-D-D encoders you've no doubt seen a few dozen times. Everyone calls them 10K encoders (due to the quadrature decoding), but natively they're just 2500 CPR (clicks per revolution) which is kinda confusing.

Chris Goldsby

  • Active Astronomer
  • ***
  • Posts: 128
  • Activity:
    20%
  • Reputation: +0/-0
    • View Profile
Re: Yet another digital setting circles project: ESP-DSC
« Reply #3 on: January 04, 2018, 03:15:12 AM »
So I went to bed and as I'm trying to fall asleep I realize I can optimize some of the interrupt handling code and make it faster. This morning I fed the cats, made coffee and updated my code (in that order). That's when I notice I should be able to optimize the code even further or add some error detection for interrupts which weren't handled properly. Since the original code was very strict in trying to handle the interrupts, I decided to opt for error detection rather then optimization.

TL;DR; My updated code now will tell me if I "drop" an interrupt which would introduce some error. Much more accurate then the testing I've done so far.

The good news is that no problems reading a single encoder moving. The bad news is that occasional problems when both encoders are moving at the same time. It's not a lot, but it's enough to cause problems.

So the updated next steps are:
 0. Fix interrupt handling

dsepinumer

  • Active Astronomer
  • ***
  • Posts: 112
  • Activity:
    22.67%
  • Reputation: +0/-0
    • View Profile
Re: Yet another digital setting circles project: ESP-DSC
« Reply #4 on: January 09, 2018, 12:21:25 AM »
So I spent most of the day optimizing the code and realistically speaking not making any significant improvements. At this point I'm going to say that the ESP8266 can't keep up with two 2500 CPR encoders using the Arduino programming environment. Maybe it would work using the native SDK, but so far I haven't found any evidence for that and so I haven't bothered to test that theory yet.

The good news is that there are a number of other solutions and I've documented them here on my blog:https://synfin.net/s...-works-not-well

foarehortalp

  • Active Astronomer
  • ***
  • Posts: 110
  • Activity:
    12.67%
  • Reputation: +0/-0
    • View Profile
Re: Yet another digital setting circles project: ESP-DSC
« Reply #5 on: January 09, 2018, 07:45:46 PM »
Do you have any feeling for what resolution the encoders wouldn't  have a problem ? I have built a similar unit using an ESP12E module which cost under $10 It has a built in USB port so no need for the separateFTDI board for programming and one can directly wire the encoder to the module for easy construction. I'm using 8192 encoders. A couple of my club members have built them and we haven't seen any issues with dropped interrupts or encoder count errors.

        - Dave


Tim Jauregui

  • Active Astronomer
  • ***
  • Posts: 116
  • Activity:
    16%
  • Reputation: +0/-0
    • View Profile
Re: Yet another digital setting circles project: ESP-DSC
« Reply #6 on: January 10, 2018, 10:31:56 PM »
2500 CPR/10K encoders almost work. I bet they would work great if you had a go to scope since they don't move fast. But someone with a push-to dob who moves both axis quickly would see a few dropped interrupts. You may not even notice it during alignment, but over the course of the night things would get progressively worse. I don't see any dropped interrupts if you move only one encoder.

As I'm sure you know, if only one encoder is moving, the interrupts happen sequentially and never occur at the same time. Once you add a second encoder, the chances of an interrupt collision goes from 0 to greater then 0. According to the info I've read, the ESP should queue up the interrupt if it is in an interrupt handler when another pin detects a voltage change. However that queue is limited and if you overrun it, then you'll start dropping interrupts. Maybe tonight I'll plug the board into my 10K CPR encoders on my scope and see just how bad it is. For a single encoder it might work.

Looks like you've got a NodeMCU? I'd be curious to compare your code vs. mine if you don't mind sharing. All my code is posted up on github:https://github.com/s...ter/src/esp_dsc To get the code to work, copy config.h.sample to config.h and edit to taste. You'll probably also need to edit esp_dsc.h to assign the encoder pins.

The code that is published currently only will detect a missed interrupt on the DEC encoder's B channel. The rest of the error detection has been optimized away.

Jeff Swan

  • Active Astronomer
  • ***
  • Posts: 120
  • Activity:
    18.67%
  • Reputation: +0/-0
    • View Profile
Re: Yet another digital setting circles project: ESP-DSC
« Reply #7 on: January 16, 2018, 12:11:59 AM »
According to the info I've read, the ESP should queue up the interrupt if it is in an interrupt handler when another pin detects a voltage change.

I wonder if ISR's for RISING and FALLING edge instead of just CHANGE would help? It would require using four pins per encoder I guess. Might make for slightly faster ISRs too since you know the pin state going in.

plicoptorol

  • Active Astronomer
  • ***
  • Posts: 104
  • Activity:
    17.33%
  • Reputation: +0/-0
    • View Profile
Re: Yet another digital setting circles project: ESP-DSC
« Reply #8 on: January 18, 2018, 04:07:41 AM »
Hi Howard,

I suppose I could do that, but that won't reduce the total number of interrupts which I believe to be a bigger problem then the amount of work done in the ISR. Or in other words, I believe the ESP has to spend significant clock cycles setting up/tearing down the ISR (modifying the stack, etc) and that's the root cause of the issue.

As it is, my code avoids digitalRead() on 3 of the 4 pins- instead I just flip state. This works fine as long as we don't drop interrupts. On the 4th pin, I do a digitalRead() and compare it to the previous state. If it is the same as the previous state, then I know I lost an interrupt somewhere. Of course it's possible that digitalRead() is really really slow... I don't have a signal generator to do a proper bench test, but maybe I can hack something. Have to think about that.

Honestly, I suspect I need to find almost an order of magnitude improvement in performance to prevent lost interrupts at this point. Remember, my goal isn't to support these encoders, but rather the 40K (10K CPR) encoders on my scope. Right now, I don't see any way that the ESP8266 can do that on it's own based on my results so far.

globleferep

  • Active Astronomer
  • ***
  • Posts: 114
  • Activity:
    20.67%
  • Reputation: +0/-0
    • View Profile
Re: Yet another digital setting circles project: ESP-DSC
« Reply #9 on: January 25, 2018, 07:17:52 PM »
I suppose I could disable WiFi for the next test. That would give a good indication if it's too busy elsewhere.

The thing is that when you enter an ISR, interrupts are disabled globally- not just for that pin. The fact that I don't see any dropped interrupts when only one encoder is being rotated tells me that the ESP is capable of handling the CHANGE interrupt on a single pin. Hence I don't think distributing the interrupts to more pins is going to help.

Anyways, I should definitely give a single 10K encoder a try tonight. I'm curious if the ESP can keep up- it may only be a problem when two interrupts occur simultaneously. So I just did the math and at 80Mhz, 40K would work out to 2000 instructions per interrupt. That seems like plenty considering what is happening. Of course there is additional overhead for WiFi, etc.

That said, IIRC, there is a way to run the ESP at 160Mhz. Eats more power, but my design has a beefy 3.3V regulator so it should be no problem to try that. So I should try that as well.

outatnoha

  • Active Astronomer
  • ***
  • Posts: 141
  • Activity:
    19.33%
  • Reputation: +0/-0
    • View Profile
Re: Yet another digital setting circles project: ESP-DSC
« Reply #10 on: January 25, 2018, 10:56:18 PM »
The timing requirements are more critical with two encoders, each has to (potentially) wait for the other + anything else that might disable interrupts for a moment here and there. With a single encoder it never waits for itself, either A or B never both.

IDK if this will help, but I wouldn't be surprised if it did. I also wouldn't be surprised if there was some obscure problem with having eight pin interrupts enabled at the same time on this thing.

guisamcipen

  • Active Astronomer
  • ***
  • Posts: 136
  • Activity:
    22%
  • Reputation: +0/-0
    • View Profile
Re: Yet another digital setting circles project: ESP-DSC
« Reply #11 on: January 31, 2018, 05:56:07 AM »
I think digitalRead has a lot of overhead
http://forum.arduino...ic,46896.0.html
Mconsidine

ropnolini

  • Active Astronomer
  • ***
  • Posts: 140
  • Activity:
    25.33%
  • Reputation: +0/-0
    • View Profile
Re: Yet another digital setting circles project: ESP-DSC
« Reply #12 on: February 02, 2018, 10:26:35 PM »
That link is talking about the Arduino AVR based boards (Uno, Mega, etc) which are slow. This page gives the numbers:

http://syncchannel.b...t-shootout.html

If you scroll down to the comments, the ESP8266 @ 160Mhz digitalRead() is 0.3us vs 5.4us for the Arduino Uno.

Anyways, I bumped the speed up to 160Mhz and ran my tests. Still dropping interrupts when spinning both encoders quickly. I also tried one 40K encoder and it's able to keep up as long as I don't try to move it very quickly (spin the encoder between your thumb and finger). Normal deliberate moves are ok. I've glued a toothpick to the encoder and unless I spin it very fast it keeps up fine.

Honestly, I'd be hard pressed to say that 160Mhz is any better then 80Mhz. The 40K encoder all by its own can do roughly a constant 1 rotation/sec (maybe more?) before things break.

Edit: edited the code to disable WiFi and ran it at 160Mhz to see if that helps things. I'm going to subjectively say "yes", but only a little.

Ralph Gleason

  • Active Astronomer
  • ***
  • Posts: 121
  • Activity:
    22.67%
  • Reputation: +0/-0
    • View Profile
Re: Yet another digital setting circles project: ESP-DSC
« Reply #13 on: February 08, 2018, 03:28:45 AM »
How do things look if you strip out the code that enables multiple clients to connect to the esp? Ie write it for only one client...
mconsidine

monsresiwor

  • Active Astronomer
  • ***
  • Posts: 131
  • Activity:
    22.67%
  • Reputation: +0/-0
    • View Profile
Re: Yet another digital setting circles project: ESP-DSC
« Reply #14 on: February 08, 2018, 07:56:09 PM »
Well I disabled all the WiFi/client handling code and it made no significant difference. Without a proper signal generator, I don't have a means of actual measuring changes like that.

Doing some research shows that I should be able to come up with a poor mans signal generator using an Arduino/etc to generate two PWM signals 90deg out of phase. I probably wouldn't be able to change the frequency very well, but could easily emulate a constant rotation speed of the encoder. Not sure how easy to do this is yet. Would be nice to be able to to get an accurate measure of accuracy without relying on a digitalRead() (which I believe to be fast, but still a lot slower then flipping a boolean).

The other thing I'm looking into is using something like a pair of ATTiny84 to process the encoders. I think one should be able to keep up with a single 10K encoder.  I have some old Arduino Uno's which are basically the same chip, but just have more pins so I could do a test. Nice thing about them is that they're only $1/ea vs the $5 US Digital wants for their encoder counter chips.

Or use two ESP8266's and each one only handles a single encoder and they communicate via serial. That should work as long as one of them still has enough power to handle WiFi.

Or wait a while for the new ESP32 which is a more powerful dual-core wifi/bluetooth solution to stabilize.