# State machine initialization in XOD 0.18.1

**URL:** <https://forum.xod.io/t/state-machine-initialization-in-xod-0-18-1/499>\
**Category:** Uncategorized\
**Created:** [February 12, 2018, 11:35pm UTC](https://forum.xod.io/t/state-machine-initialization-in-xod-0-18-1/499 "2018-02-12T23:35:31Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![gweimer](https://avatars.discourse-cdn.com/v4/letter/g/a9adbd/32.png) [@gweimer](https://forum.xod.io/u/gweimer)\
**Post date:** [February 12, 2018, 11:35pm UTC](https://forum.xod.io/t/state-machine-initialization-in-xod-0-18-1/499/1 "2018-02-12T23:35:31Z")

</div>

I have obstacle avoidance code that was working in 0.17.1, but in 0.18.1 during initialization, the motors & servo go spastic trying to process multiple values in quick succession. Once initialization is complete, the code seems to execute normally. The motor & servo nodes should not activate if a pulse is not fed into them and all was quiet during initialization in 0.17.1 XOD.

Here is the code I tested with:  
[obstacle-avoidance.xodball](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/1X/0e69270faddb18ddb6d4f010e751d87eafaacefe.xodball) (57.4 KB)

The good news: This program had \<100 Bytes of dynamic memory free in 0.17.1 & I could not add LCD nodes. In 0.18.1, I can add my LCD-init library to display forward distance on line1 & left/right distance on line2 and still have 939 Bytes free!

---

<div class="post-metadata">

**Author:** ![evgenykochetkov](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/evgenykochetkov/32/14_2.png) [@evgenykochetkov](https://forum.xod.io/u/evgenykochetkov)\
**Post date:** [February 13, 2018, 6:59am UTC](https://forum.xod.io/t/state-machine-initialization-in-xod-0-18-1/499/2 "2018-02-13T06:59:52Z")

</div>

Thanks for the report, I will investigate.

---

<div class="post-metadata">

**Author:** ![gweimer](https://avatars.discourse-cdn.com/v4/letter/g/a9adbd/32.png) [@gweimer](https://forum.xod.io/u/gweimer)\
**Post date:** [March 2, 2018, 6:55pm UTC](https://forum.xod.io/t/state-machine-initialization-in-xod-0-18-1/499/3 "2018-03-02T18:55:57Z")

</div>

Any update on this? I see 0.19.0 has same issue. I assume available time was dedicated to fixing variadic nodes…

I just tried from the browser IDE, copied code into Arduino IDE & downloaded…same result.

---

<div class="post-metadata">

**Author:** ![nkrkv](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/nkrkv/32/8_2.png) [@nkrkv](https://forum.xod.io/u/nkrkv)\
**Post date:** [March 2, 2018, 9:53pm UTC](https://forum.xod.io/t/state-machine-initialization-in-xod-0-18-1/499/4 "2018-03-02T21:53:11Z")

</div>

Yeah, sorry for the delay. I was on vacation and the guys were very focused on 0.19.0. We’ll try to find the problem at the beginning of next week, before starting a new development sprint.

---

<div class="post-metadata">

**Author:** ![gweimer](https://avatars.discourse-cdn.com/v4/letter/g/a9adbd/32.png) [@gweimer](https://forum.xod.io/u/gweimer)\
**Post date:** [March 2, 2018, 11:50pm UTC](https://forum.xod.io/t/state-machine-initialization-in-xod-0-18-1/499/5 "2018-03-02T23:50:38Z")

</div>

No problem, I’m getting more than my money’s worth for support 🙂

Mostly I wanted to make sure it hadn’t just been forgotten. I’d really like to use the latest version for my class, but they are already having trouble understanding code; they don’t need a bug like this to confuse them more :-(. The down-side of using beta software to teach a class… Still an awesome product that is much easier for them to understand than straight Arduino IDE!

---

<div class="post-metadata">

**Author:** ![nkrkv](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/nkrkv/32/8_2.png) [@nkrkv](https://forum.xod.io/u/nkrkv)\
**Post date:** [March 5, 2018, 1:17pm UTC](https://forum.xod.io/t/state-machine-initialization-in-xod-0-18-1/499/6 "2018-03-05T13:17:25Z")

</div>

@gweimer I’ve carefully compared node evaluation order between 0.17 and 0.19 and found a critical bug introduced while switching to the optimized code generator:

 ![patch](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/1X/8ce9063e05eca473c61e3f34f398ba576b36421f.png)

Since 0.18 any `defer-pulse` node emits an extraneous pulse on boot. I think that is the problem you’ve stumbled upon.

Could you verify it is the root cause? To check it:

- Generate C++ for your `main-0` patch
- Find `evaluate` function definition in the `xod __core__ defer_pulse` namespace
- Apply a change to the single line:

```diff
diff --git a/workspace/ __lib__ /xod/core/defer-pulse/patch.cpp b/workspace/ __lib__ /xod/core/defer-pulse/patch.cpp
index 62341a59..1ed690e7 100644
--- a/workspace/ __lib__ /xod/core/defer-pulse/patch.cpp
+++ b/workspace/ __lib__ /xod/core/defer-pulse/patch.cpp
@@ -6,7 +6,7 @@ struct State {
 void evaluate(Context ctx) {
     if (isInputDirty<input_IN>(ctx)) { // This happens only when all nodes are evaluated
         setTimeout(ctx, 0);
- } else {
+ } else if (isTimedOut(ctx)) {
         emitValue<output_OUT>(ctx, true);
     }
 }

```

---

<div class="post-metadata">

**Author:** ![gweimer](https://avatars.discourse-cdn.com/v4/letter/g/a9adbd/32.png) [@gweimer](https://forum.xod.io/u/gweimer)\
**Post date:** [March 5, 2018, 2:04pm UTC](https://forum.xod.io/t/state-machine-initialization-in-xod-0-18-1/499/7 "2018-03-05T14:04:55Z")

</div>

That seems to be the issue. Motors & servo were quite during initialization with this change.

---

<div class="post-metadata">

**Author:** ![nkrkv](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/nkrkv/32/8_2.png) [@nkrkv](https://forum.xod.io/u/nkrkv)\
**Post date:** [March 12, 2018, 2:51pm UTC](https://forum.xod.io/t/state-machine-initialization-in-xod-0-18-1/499/8 "2018-03-12T14:51:02Z")

</div>

Fixed in [0.19.2](https://github.com/xodio/xod/releases/tag/v0.19.2)

---

<div class="post-metadata">

**Author:** ![gweimer](https://avatars.discourse-cdn.com/v4/letter/g/a9adbd/32.png) [@gweimer](https://forum.xod.io/u/gweimer)\
**Post date:** [March 12, 2018, 3:49pm UTC](https://forum.xod.io/t/state-machine-initialization-in-xod-0-18-1/499/9 "2018-03-12T15:49:49Z")

</div>

So sad…fix not really included…

```auto
void evaluate(Context ctx) {
    if (isInputDirty<input_IN>(ctx)) { // This happens only when all nodes are evaluated
        setTimeout(ctx, 0);
    } else {
        emitValue<output_OUT>(ctx, true);
    }
}

} // namespace xod __core__ defer_pulse

```

---

<div class="post-metadata">

**Author:** ![nkrkv](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/nkrkv/32/8_2.png) [@nkrkv](https://forum.xod.io/u/nkrkv)\
**Post date:** [March 14, 2018, 8:40am UTC](https://forum.xod.io/t/state-machine-initialization-in-xod-0-18-1/499/10 "2018-03-14T08:40:34Z")

</div>

It [should](https://github.com/xodio/xod/blob/v0.19.2/workspace/ __lib__ /xod/core/defer-pulse/patch.cpp). I guess you have forked the `xod/core` unintentionally. Take a look at your workspace libraries (`~/xod/ __lib__ `) if the `xod/core` is here, just remove it and the system would use the core library bundled with IDE itself.

---

<div class="post-metadata">

**Author:** ![gweimer](https://avatars.discourse-cdn.com/v4/letter/g/a9adbd/32.png) [@gweimer](https://forum.xod.io/u/gweimer)\
**Post date:** [March 14, 2018, 2:30pm UTC](https://forum.xod.io/t/state-machine-initialization-in-xod-0-18-1/499/11 "2018-03-14T14:30:20Z")

</div>

Good call. That was the issue. I had updated range-finder code to return a big number if no ping returned (nothing in front of us) instead of the default to not change the output on error.

Startup is clean now. I’ll have to change my code to work with core or create a new library instead of changing core.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/1X/eb7348baa04192c2ef932d1db54d44f09f8a7888.png) [@system](https://forum.xod.io/u/system)\
**Post date:** [March 16, 2018, 2:30pm UTC](https://forum.xod.io/t/state-machine-initialization-in-xod-0-18-1/499/12 "2018-03-16T14:30:21Z")

</div>

Closed on March 16, 2018, 2:30pm UTC

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
