[SOLVED] FEZ Mini with Existing Car motor

Have you any source code online? Just to check it and compare some stuff?

No, the robot project is not yet online, but below the source DC motor.

I’ve adjusted GHI example, I wanted only 10 speed steps which make a difference hence the PWM table. Each engine type, the value can be determined.
The “move ramp” function starts a thread to handle so the calling program is not waiting. The “move ramp” function only need the motors are strong.

/*
Copyright 2010 GHI Electronics LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 
*/

using System;
using System.Threading;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.Hardware;

namespace GHIElectronics.NETMF.FEZ
{
    public static partial class FEZ_Components
    {
        public class Motor : IDisposable
        {
            OutputPort _dirLeft;
            PWM _pwmLeft;
            OutputPort _dirRight;
            PWM _pwmRight;
            private sbyte _last_speedLeft, _last_speedRight;
            private sbyte[] _pwmSpeedTab;

            public void Dispose()
            {
                _dirLeft.Dispose();
                _pwmLeft.Dispose();
                _dirRight.Dispose();
                _pwmRight.Dispose();
            }

            public Motor(FEZ_Pin.Digital dirL, FEZ_Pin.PWM pwmL, FEZ_Pin.Digital dirR, FEZ_Pin.PWM pwmR)
            {
                _dirLeft = new OutputPort((Cpu.Pin)dirL, false);
                _pwmLeft = new PWM((PWM.Pin)pwmL);
                _dirRight = new OutputPort((Cpu.Pin)dirR, false);
                _pwmRight = new PWM((PWM.Pin)pwmR);
                //                           0   1   2   3   4   5   6   7   8   9   10 
                _pwmSpeedTab = new sbyte[] { 0, 26, 30, 34, 38, 42, 48, 59, 81, 92, 100 };
            }

            public void Move(sbyte left, sbyte right, byte ramp)
            {
                sbyte tL = _pwmSpeedTab[(sbyte)System.Math.Abs(left)];
                sbyte tR = _pwmSpeedTab[(sbyte)System.Math.Abs(right)];

                if (left < 0) tL *= -1;
                if (right < 0) tR *= -1;
                MoveRamp(tL, tR, ramp);
            }

            public void Move(sbyte leftSpeed, sbyte rightSpeed)
            {
                if (leftSpeed > 10 || leftSpeed < -10 || rightSpeed > 10 || rightSpeed < -10)
                    new ArgumentException();
                sbyte tL = _pwmSpeedTab[(sbyte)System.Math.Abs(leftSpeed)];
                sbyte tR = _pwmSpeedTab[(sbyte)System.Math.Abs(rightSpeed)];

                if (leftSpeed < 0) tL *= -1;
                if (rightSpeed < 0) tR *= -1;
                MoveRaw(tL, tR);
            }

            public void MoveRaw(sbyte speedL, sbyte speedR)
            {
                if (speedL > 100 || speedL < -100 || speedR > 100 || speedR < -100)
                    new ArgumentException();
                _last_speedLeft = speedL;
                _last_speedRight = speedR;
                if (speedL < 0)
                {
                    _dirLeft.Write(true);
                    _pwmLeft.Set(1000, (byte)(100 - Math.Abs(speedL)));
                }
                else
                {
                    _dirLeft.Write(false);
                    _pwmLeft.Set(1000, (byte)(speedL));
                }
                if (speedR < 0)
                {
                    _dirRight.Write(true);
                    _pwmRight.Set(1000, (byte)(100 - Math.Abs(speedR)));
                }
                else
                {
                    _dirRight.Write(false);
                    _pwmRight.Set(1000, (byte)(speedR));
                }
            }

            public void MoveRamp(sbyte speedL, sbyte speedR, byte ramping_delay)
            {
                if ((speedL != _last_speedLeft) || (speedR != _last_speedRight))
                {
                    new Thread(() => rampProcessing(speedL, speedR, ramping_delay)).Start();
                }
            }

            public void rampProcessing(sbyte speed1, sbyte speed2, byte ramping_delay)
            {
                Thread.CurrentThread.Priority = ThreadPriority.Normal;
                sbyte temp_speed1 = _last_speedLeft;
                sbyte temp_speed2 = _last_speedRight;
                while ((speed1 != temp_speed1) || (speed2 != temp_speed2))
                {
                    if (temp_speed1 > speed1)
                        temp_speed1--;
                    if (temp_speed1 < speed1)
                        temp_speed1++;
                    if (temp_speed2 > speed2)
                        temp_speed2--;
                    if (temp_speed2 < speed2)
                        temp_speed2++;
                    MoveRaw(temp_speed1, temp_speed2);
                    Thread.Sleep(ramping_delay);
                }
            }
        }
    }
}

Removed the ramping stuff. Also working like a charm ;D
Update the TinyCLR code…

Just mounted a Ultrasonic Sensor, programming now…

Good news. Thanks for the update.

Yeah, good news. now i am testing the sensor.

Sensor is working, but the following code isn’t working as expected :frowning:

Debugging debugging debugging :wink:

Will post some video’s soon!

Changed the code many times :wink:
Front Sensor working okay (other sensors not connected yet, one thing at a time).

What it does now:

  1. When Front Distance < 100cm, then stop, steer left, go reverse for some seconds, stop, steer forward, move forward again. and loop again.
  2. When Front Distance is changed with a difference of 50cm, someone just walked in front of the car, so we expect he/she will go away, so, stop, and when distance is back to > 100 then move forward again.

The second code (2), isn’t working correctly yet, it only stops, and waits for ever, even when the object is gone.

Please do not respond to this, cause i want to debug myself :wink: if you do, thanks :stuck_out_tongue:

[Edit]

Found the problem… By bad :-[
First have to recharge the batteries of the RC Car, can’t do more tests when battery is empty :o lol