Controlling the HDC2450s controller with PCAN (CAN BUS)

6 years 2 months ago #29529111 by chelsea
I am trying to write the code for controlling the brush motor using the HDC2450 controller using C#.

I have wrote the code to talk to CAN BUS but I don't know what data package to send in to make the motor moves.

So, using the Roboteq Motor Control Utility I went into and set up the controller to use CAN as follow

CAN Mode: RawCAN
Node ID: 100
Bit Rate: 125
Heartbeat (ms) 100

And, in my software, I have something like this to write a command to the controller via CAN BUS
TPCANMsg CANMsg;
            TPCANStatus stsResult;

            CANMsg = new TPCANMsg();
            CANMsg.DATA = new byte[8];

            CANMsg.ID = id;
            CANMsg.LEN = length;
            CANMsg.MSGTYPE = TPCANMessageType.PCAN_MESSAGE_STANDARD;

            CANMsg.DATA[0] = 10;
            CANMsg.DATA[1] = 10;
            CANMsg.DATA[2] = 10;
            CANMsg.DATA[3] = 10;
            CANMsg.DATA[4] = 0;
            CANMsg.DATA[5] = 0;
            CANMsg.DATA[6] = 0;
            CANMsg.DATA[7] = 0;

            stsResult = PCANBasic.Write(PcanHandle, ref CANMsg);

The above code will send some DATA[] to the controller.
But, What I don't know is the DADA[]. What Data should I send to make the motor move?

Any help is appreciated.

Please Log in or Create an account to join the conversation.

6 years 2 months ago #29529112 by Griffin Baker
If the motor is running in open loop, you send it the go command.

setcommand(_G, cc, nn) where cc = motor channel, and nn = command value.

So to set motor 1 command to go to 500, then it would be setcommand(_G, 1, 500).

Please Log in or Create an account to join the conversation.

6 years 2 months ago #29529113 by chelsea
I believe the setcommand() is a microbasic function. How can I send this in a RAW CAN package?

Please Log in or Create an account to join the conversation.

6 years 2 months ago #29529114 by Griffin Baker
I am not sure in terms of can, but you can write the script so that when it receives a certain piece of data that it is compared against, you use the setcommand to tell the motor to move.

So if the can message data = 10, then tell the motor 1 to move.

Please Log in or Create an account to join the conversation.

6 years 2 months ago #29529115 by chelsea
Do I need to write the Microbasic script to be able to control via CAN is the question. Is that the only way of doing this?

Please Log in or Create an account to join the conversation.

6 years 2 months ago #29529118 by roboteq
RawCAN will allow you to capture the raw content of your frames. It is then up to you to give this data a meaning using a script.

Assuming then that the frame you send contains the value 500 (0x1F4) stored in the first two bytes as 0x01 abd 0xF4, the code would look like this (untested)
top:
' check if a new frame has arrived
if (getvalue(_CF, 1) > 0)
   speed = (getvalue(_CAN, 3) << 8) +  getvalue(_CAN, 4)' read first 2 bytes of frame to build 16-bit word
   setcommand(_G, 1, speed)
end if

wait(10) ' 10ms pause
goto top

Please Log in or Create an account to join the conversation.

Moderators: tonysantoni
Time to create page: 0.082 seconds