Motor Control
Navigates to RoboticsConnection.com Home RoboticsConnection.com HomePage
RoboticsConnection User Forum
Home       Members    Calendar    Who's On
Welcome Guest ( Login | Register )
        



Motor Control Expand / Collapse
Author
Message
Posted Sunday, August 03, 2008 1:19 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: Forum Members
Last Login: Tuesday, August 26, 2008 1:05 AM
Posts: 4, Visits: 51

[quote]Hi,
I’m beginner in C# and wrote this code below in Microsoft Visual C# 2008 Express in Console application to control DC motors. Actually I want to control DC motors from the keyboard, but the problem is I have to press enter after press key.
How can I solve this problem? and How can i change Com port?
Thank you.
using RoboticsConnection.Serializer;
using RoboticsConnection.Serializer.Ids;
using RoboticsConnection.Serializer.Sensors;
using RoboticsConnection.Serializer.Components;
using RoboticsConnection.Serializer.Controllers;
using System;
using System.Threading;
 
namespace test
{
    class Program
    {
        static Serializer serializer;
        static PwmDCMotorController LeftMotor;
        static PwmDCMotorController RightMotor;
 
        static void Main(string[] args)
        {
            serializer = new Serializer();
 
            LeftMotor = new PwmDCMotorController(serializer);
            RightMotor = new PwmDCMotorController(serializer);
 
            LeftMotor.DCMotorId = DCMotorId.DCMotor2;
            RightMotor.DCMotorId = DCMotorId.DCMotor1;
 
            serializer.StartCommunication();
            while (true)
            {
                serializer.PumpEvents();
 
             
                Console.Write("Enter Key:");
                string s = Console.ReadLine();
 
                if (s == "w")
                {
                    LeftMotor.Speed = 100;
                    RightMotor.Speed = 100;
                }
                if (s == "a")
                {
                    LeftMotor.Speed = -100;
                    RightMotor.Speed = 100;
                }
                if (s == "d")
                {
                    LeftMotor.Speed = 100;
                    RightMotor.Speed = -100;
                }
                if (s == "x")
                {
                    LeftMotor.Speed = -100;
                    RightMotor.Speed = -100;
                }
                if (s == "s")
                {
                    LeftMotor.Speed = 0;
                    RightMotor.Speed = 0;
                }
            }
        }
    }
}

Post #606
Posted Friday, August 08, 2008 8:41 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: Forum Members
Last Login: Today @ 6:32 AM
Posts: 1, Visits: 111
I would use the ConsoleKeyInfo struct and Console.ReadKey(). That way you could use the arrow keys.

Something like this. The true in the readkey function blocks the key pressed from being displayed on the screen.

            ConsoleKeyInfo cki;
            while (true)
            {
                cki = Console.ReadKey(true);
                switch (cki.Key)
                {
                    case ConsoleKey.DownArrow:
                        //Do DownArrow key processing here
                        break;
                    case ConsoleKey.UpArrow:
                        //Do UpArrow key processing here
                        break;
                    case ConsoleKey.LeftArrow:
                        //Do LeftArrow key processing here
                        break;
                    case ConsoleKey.RightArrow:
                        //Do RightArrow key processing here
                        break;
                    case ConsoleKey.S:
                        //Do s key processing here
                        break;
                    default:
                        //Do any other key processing here
                        break;
                }

Hope this helps.

Bruno

Post #607
Posted Thursday, August 21, 2008 9:20 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: Forum Members
Last Login: Tuesday, August 26, 2008 1:05 AM
Posts: 4, Visits: 51
Thanks bro it works
and this is the code

using RoboticsConnection.Serializer;
using RoboticsConnection.Serializer.Ids;
using RoboticsConnection.Serializer.Sensors;
using RoboticsConnection.Serializer.Components;
using RoboticsConnection.Serializer.Controllers;
using System;
using System.Threading;

namespace test
{
class Program
{
static Serializer serializer;
static PwmDCMotorController LeftMotor;
static PwmDCMotorController RightMotor;
static void Main(string[] args)
{
serializer = new Serializer();
LeftMotor = new PwmDCMotorController(serializer);
RightMotor = new PwmDCMotorController(serializer);
LeftMotor.DCMotorId = DCMotorId.DCMotor2;
RightMotor.DCMotorId = DCMotorId.DCMotor1;

serializer.StartCommunication();
while (true)
{
serializer.PumpEvents();

switch (Console.ReadKey().Key)
{
case ConsoleKey.UpArrow: // press Up Arrow to move forward
LeftMotor.Speed = 100;
RightMotor.Speed = 100;
break;
case ConsoleKey.LeftArrow: // press Left Arrow to turn Left
LeftMotor.Speed = -100;
RightMotor.Speed = 100;
break;
case ConsoleKey.RightArrow: // press Right arrow to turn Right
LeftMotor.Speed = 100;
RightMotor.Speed = -100;
break;
case ConsoleKey.DownArrow: // press Down Arrow to move backward
LeftMotor.Speed = -100;
RightMotor.Speed = -100;
break;
case ConsoleKey.Spacebar: //Press Spacbar to stop
LeftMotor.Speed = 0;
RightMotor.Speed = 0;
}
}
}
}
}
Post #624
« Prev Topic | Next Topic »


Reading This Topic Expand / Collapse
Active Users: 0 (0 guests, 0 members, 0 anonymous members)
No members currently viewing this topic.
Forum Moderators: jsummerour

Permissions Expand / Collapse

All times are GMT -8:00, Time now is 10:19am

Powered By InstantForum.NET v4.1.4 © 2008
Execution: 0.140. 14 queries. Compression Disabled.