# Closed Loop drives motor in random directions even with motor.target=0

**URL:** <https://community.simplefoc.com/t/closed-loop-drives-motor-in-random-directions-even-with-motor-target-0/2833>\
**Category:** applications\
**Tags:** esp32, drv8305\
**Created:** [January 8, 2023, 10:19pm UTC](https://community.simplefoc.com/t/closed-loop-drives-motor-in-random-directions-even-with-motor-target-0/2833 "2023-01-08T22:19:32Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![juniorgary](https://community.simplefoc.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@juniorgary](https://community.simplefoc.com/u/juniorgary)\
**Post date:** [January 8, 2023, 10:19pm UTC](https://community.simplefoc.com/t/closed-loop-drives-motor-in-random-directions-even-with-motor-target-0/2833/1 "2023-01-08T22:19:32Z")

</div>

Hi,  
I’m trying to run a linear motor (PMLSM) using the SimpleFOC library. I use an ESP32 and a DRV8305. The angle is determined by two hall sensors offset by 90°. Since the SimpleFOC library does not support this sensor setup, I implemented the calculation as GenericSensor. The result is a very clean angle signal (in radiants). The sensor initialisation also provides reasonable values (zero\_electric\_angle = 0.12; sensor\_direction = 1; motor\_status = 4;). In open loop, the setup works perfectly. Changed to “MotionControlType::velocity”, the motor moves in a random direction as soon as I activate the GateEnable signal to the driver (manually, to be on the safe side) with random velocity far above the velocity\_limit. Playing around with the PID\_velocity settings does not seem to have any impact on the behaviour. Since I am not getting anywhere at this point, I kindly ask you for helpful hints.

---

<div class="post-metadata">

**Author:** ![juniorgary](https://community.simplefoc.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@juniorgary](https://community.simplefoc.com/u/juniorgary)\
**Post date:** [January 8, 2023, 10:26pm UTC](https://community.simplefoc.com/t/closed-loop-drives-motor-in-random-directions-even-with-motor-target-0/2833/2 "2023-01-08T22:26:02Z")

</div>

![motor-shaft_velocity_bei_Tf0k1___MotionControlTypeVelocity_](https://community.simplefoc.com/uploads/default/original/2X/9/90095d5f605a8f98486aaa48f77eeb28d6ed5382.jpeg)

---

<div class="post-metadata">

**Author:** ![juniorgary](https://community.simplefoc.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@juniorgary](https://community.simplefoc.com/u/juniorgary)\
**Post date:** [January 8, 2023, 10:42pm UTC](https://community.simplefoc.com/t/closed-loop-drives-motor-in-random-directions-even-with-motor-target-0/2833/3 "2023-01-08T22:42:35Z")

</div>

The graph shows following signals:  
value1 & value2 are the analogRead values of the position sensors (scaled by 0.01 for display purposes),  
value3 shows sensor.getAngle(),  
value4 shows motor.shaft\_velocity.  
LPF\_velocity.Tf has been set to 0.1 (for testing purposes)  
Whenever the GateEnable signal to the driver was activated, the motor started moving, as shown in the graph.

---

<div class="post-metadata">

**Author:** ![runger](https://community.simplefoc.com/user_avatar/community.simplefoc.com/runger/32/395_2.png) [@runger](https://community.simplefoc.com/u/runger)\
**Post date:** [January 8, 2023, 11:53pm UTC](https://community.simplefoc.com/t/closed-loop-drives-motor-in-random-directions-even-with-motor-target-0/2833/4 "2023-01-08T23:53:46Z")

</div>

So cool that you’re trying this! I’ve never used such a motor.

Is it driven by exactly the same kind of commutation as a PMSM motor? I guess so, if you say open loop is working!

What determines the number of “pole pairs” for such a motor? It’s important to get this number right, at least BLDCs don’t work if you don’t get this right.

You should try torque-voltage mode before velocity. In this mode, you just set the voltage and the motor goes at whatever speed is normal for that voltage. It uses the sensor to set the commutation optimally based on the motor’s position (FOC).  
Your motor isn’t really rotating, it is translating… so I don’t know if this makes a difference in our algorithm. Perhaps you can account for it in your sensor implementation.

Once torque-voltage is working, only then should you try velocity. I think you can try setting the P, I, D values to zero, and raising P just a little to see how sensitive the motor is. Luckily ESP32s are very fast MCUs, so hopefully you should be able to get the motor under control 🙂

---

<div class="post-metadata">

**Author:** ![juniorgary](https://community.simplefoc.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@juniorgary](https://community.simplefoc.com/u/juniorgary)\
**Post date:** [January 18, 2023, 11:49pm UTC](https://community.simplefoc.com/t/closed-loop-drives-motor-in-random-directions-even-with-motor-target-0/2833/5 "2023-01-18T23:49:02Z")

</div>

Hello runger,

thanks for your answer (and sorry for my late reply)!  
Unfortunately, I don’t have the motor available at the moment, as it is currently needed for assembly tests. I will therefore have to wait a few weeks before I can continue with the software development.

I think that I had actually set the number of pole pairs wrong, because this was noted incorrectly in the manufacturer’s description. On the now available winding diagram (see following picture) I clearly identify 2 pole pairs - please confirm or correct my interpretation 🙂

 ![Ohne Titel](https://community.simplefoc.com/uploads/default/original/2X/5/5f8cb84116e743d6a0a0b6bfa2d84ab4d18f317f.jpeg)

I probably also made a mistake when setting up the controller. I understood the SimpleFOC description to mean that you have to define the torque controller in any case, so I defined both:  
`motor.torque_controller = TorqueControlType::voltage;`  
`motor.controller = MotionControlType::velocity;`  
However, as you describe it, only one of the two definitions may be specified. Is that correct? It now appears that the specification of torque control has disabled all settings relating to velocity control. Does that make sense?

Thank you very much for your help, it has already been a step forward for me. As soon as I have further results, I will report them here.

kind regards

---

<div class="post-metadata">

**Author:** ![Valentine](https://community.simplefoc.com/user_avatar/community.simplefoc.com/valentine/32/1024_2.png) [@Valentine](https://community.simplefoc.com/u/Valentine)\
**Post date:** [January 19, 2023, 12:06am UTC](https://community.simplefoc.com/t/closed-loop-drives-motor-in-random-directions-even-with-motor-target-0/2833/6 "2023-01-19T00:06:15Z")

</div>

> [@juniorgary](#):
>
> I don’t have the motor available at the moment,

Could you please post a link to the motor documentation? This is very interesting. I’ve never seen a linear motor commutated with SimpleFOC. It would be very educations.

Cheers,  
Valentine

---

<div class="post-metadata">

**Author:** ![juniorgary](https://community.simplefoc.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@juniorgary](https://community.simplefoc.com/u/juniorgary)\
**Post date:** [January 19, 2023, 12:47am UTC](https://community.simplefoc.com/t/closed-loop-drives-motor-in-random-directions-even-with-motor-target-0/2833/7 "2023-01-19T00:47:42Z")

</div>

Hi Valentine,

as the motor is a customised single unit, there is no documentation available on the internet. However, there is actually not much difference to a rotary motor. The construction is quite clear from the picture in my previous post. The angle is determined by two hall sensors offset by 90°. Everything else is actually just application-specific dimensioning.

kind regards

---

<div class="post-metadata">

**Author:** ![Valentine](https://community.simplefoc.com/user_avatar/community.simplefoc.com/valentine/32/1024_2.png) [@Valentine](https://community.simplefoc.com/u/Valentine)\
**Post date:** [January 19, 2023, 2:47am UTC](https://community.simplefoc.com/t/closed-loop-drives-motor-in-random-directions-even-with-motor-target-0/2833/8 "2023-01-19T02:47:25Z")

</div>

> [@juniorgary](#):
>
> not much difference to a rotary motor

I’m waiting for a picture.

Cheers,  
Valentine

---

<div class="post-metadata">

**Author:** ![juniorgary](https://community.simplefoc.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@juniorgary](https://community.simplefoc.com/u/juniorgary)\
**Post date:** [January 19, 2023, 11:23am UTC](https://community.simplefoc.com/t/closed-loop-drives-motor-in-random-directions-even-with-motor-target-0/2833/9 "2023-01-19T11:23:10Z")

</div>

![PMLSM](https://community.simplefoc.com/uploads/default/original/2X/3/31b68d54cb796e4557a74395594da9d55a9c8a13.jpeg)  
In principle it looks like a rotary motor that has been cut open and rolled out. The permanent magnets (mounted on a ferromagnetic body) are covered by the stainless steel plate at the bottom.  
See also the description here: [Linear motor - Wikipedia](https://en.wikipedia.org/wiki/Linear_motor#/media/File:Linearmotorprinzip.png)

best regards

---

<div class="post-metadata">

**Author:** ![Valentine](https://community.simplefoc.com/user_avatar/community.simplefoc.com/valentine/32/1024_2.png) [@Valentine](https://community.simplefoc.com/u/Valentine)\
**Post date:** [January 19, 2023, 2:24pm UTC](https://community.simplefoc.com/t/closed-loop-drives-motor-in-random-directions-even-with-motor-target-0/2833/10 "2023-01-19T14:24:42Z")

</div>

> [@juniorgary](#):
>
> looks like a rotary motor that has been cut open

Is yours running in a circle? I don’t see curvature.

Cheers,  
Valentine

---

<div class="post-metadata">

**Author:** ![juniorgary](https://community.simplefoc.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@juniorgary](https://community.simplefoc.com/u/juniorgary)\
**Post date:** [January 19, 2023, 7:10pm UTC](https://community.simplefoc.com/t/closed-loop-drives-motor-in-random-directions-even-with-motor-target-0/2833/11 "2023-01-19T19:10:26Z")

</div>

There is no curvature. It runs straight ahead or in other words “linear” 😉

---

<div class="post-metadata">

**Author:** ![Valentine](https://community.simplefoc.com/user_avatar/community.simplefoc.com/valentine/32/1024_2.png) [@Valentine](https://community.simplefoc.com/u/Valentine)\
**Post date:** [January 19, 2023, 10:20pm UTC](https://community.simplefoc.com/t/closed-loop-drives-motor-in-random-directions-even-with-motor-target-0/2833/12 "2023-01-19T22:20:49Z")

</div>

> [@juniorgary](#):
>
> runs straight ahead

Haha.

My question is what are the boundaries of the run and how you control it. Obviously it doesn’t go from + to - infinity.

Cheers,  
Valentine

---

<div class="post-metadata">

**Author:** ![juniorgary](https://community.simplefoc.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@juniorgary](https://community.simplefoc.com/u/juniorgary)\
**Post date:** [January 20, 2023, 7:10am UTC](https://community.simplefoc.com/t/closed-loop-drives-motor-in-random-directions-even-with-motor-target-0/2833/13 "2023-01-20T07:10:53Z")

</div>

Oh, you wanted to know what limits the run. There are mechanical stops on both sides that should be approached completely. I would initially detect and save the end positions by means of a calibration run. During operation, a certain speed curve is then run depending on sensor.getAngle(). In addition, obstacle (and end position) detection via current measurement is also being considered, but that is still up in the air. 😉

---

<div class="post-metadata">

**Author:** ![Valentine](https://community.simplefoc.com/user_avatar/community.simplefoc.com/valentine/32/1024_2.png) [@Valentine](https://community.simplefoc.com/u/Valentine)\
**Post date:** [January 20, 2023, 2:06pm UTC](https://community.simplefoc.com/t/closed-loop-drives-motor-in-random-directions-even-with-motor-target-0/2833/14 "2023-01-20T14:06:07Z")

</div>

> [@juniorgary](#):
>
> depending on sensor.getAngle().

How do you get angle in a linear motion?
