Send CAN message help

User avatar
Peter
Posts: 310
Joined: Fri Dec 14, 2018 9:07 pm
Location: North West Lancs, UK
Been thanked: 8 times

Send CAN message help

Post by Peter »

Hi Forum. I am struggling with sending a CAN message to my inverter via Arduino and CAN shield. I can receive ok and the data I get is correct. However I just can’t get the send to work. Any help much appreciated as I have read the Wiki so many times and tried so many permutations my brain hurts :-) Thanks in advance to anyone who can help. Peter.

Code: Select all

#include “mcp_can.h”
MCP_CAN CAN(10);
unsigned char Boost1[8] = {0x40,0x00,0x20,0x01,0x80,0x0C,0,0}; // 100 decimal
void setup()
{while (CAN_OK != CAN.begin(CAN_1000KBPS)){delay(1);}
}
void loop()
{ if(CAN_MSGAVAIL == CAN.checkReceive())
{CAN.sendMsgBuf(20,0,16,Boost1);} // if set as 20,0,16,1 and RX in spot values
}
User avatar
mdrobnak
Posts: 692
Joined: Thu Mar 05, 2020 5:08 pm
Location: Colorado, United States
Has thanked: 1 time
Been thanked: 5 times

Re: Send CAN message help

Post by mdrobnak »

You shouldn't have a loop in setup(), it's probably getting stuck there.

-Matt
Isaac96
Posts: 656
Joined: Sat Oct 05, 2019 6:50 pm
Location: Northern California, USA
Been thanked: 1 time
Contact:

Re: Send CAN message help

Post by Isaac96 »

Yup, maybe add some serial port debugging so that you can see whether you're actually hitting the loop and sending any data. If you have something to read CAN you can also make sure that it's running at the right datarate (sometimes the CAN shield has a different crystal).
-Isaac
User avatar
bexander
Posts: 835
Joined: Tue Jun 16, 2020 6:00 pm
Location: Gothenburg, Sweden
Has thanked: 63 times
Been thanked: 89 times

Re: Send CAN message help

Post by bexander »

I have not tested sending CAN-messages to the OpenInverter specifically so my answer is a general.

There is proably nothing wrong with the While loop in setup as this is a common way to setup the mcp_can.
In my code for other projects I normaly use this line in the while loop:
while (CAN_OK != CAN.begin(MCP_STDEXT, CAN_250KBPS, MCP_8MHZ))
Specifies both standard or extended frames (to get can filtering to work), CAN-speed, crystal speed for the mcp2515.

However when sending data the third value (16) is the data length and your array of data is only consists of 8 bytes.
Try "CAN.sendMsgBuf(20,0,8,Boost1)".

May I ask why you are checking if data is available before sending data?
User avatar
Peter
Posts: 310
Joined: Fri Dec 14, 2018 9:07 pm
Location: North West Lancs, UK
Been thanked: 8 times

Re: Send CAN message help

Post by Peter »

Thank you to all for your replies. The send message is part of a larger programme which does already read messages so that part is working fine. I will add the extra data to the CAN_OK line, thank you for that bexander. Data rate is correct in both inverter and programme so that can be eliminated. Checking for data available is for the reading of CAN, I will move that line below the send message to see if that helps. Will update my results this evening. Thanks again. Peter
User avatar
bexander
Posts: 835
Joined: Tue Jun 16, 2020 6:00 pm
Location: Gothenburg, Sweden
Has thanked: 63 times
Been thanked: 89 times

Re: Send CAN message help

Post by bexander »

One other thing. It might be a good idea to use some sort of time delay on the CAN.sendMsgBuf otherwise you will fill the receiver buffer very fast.
Sending every 100ms or so is usually enough.
User avatar
bexander
Posts: 835
Joined: Tue Jun 16, 2020 6:00 pm
Location: Gothenburg, Sweden
Has thanked: 63 times
Been thanked: 89 times

Re: Send CAN message help

Post by bexander »

I've got curious and as I will be using CAN-communication for my own project I did some testing.
I got it to work with this setup:

I set the following parameters in the openinverter:
potmin 0
potmax 1023
potmode CAN
canspeed 250k (should match the speed set in the arduino code)
Then I set under "Spot Values", pot 100 0 16 32, and pushed RX and then hit "refresh" to verify.

I'm using a "Blue Pill" for this testing so connected it to a MCP2561 CAN tranciver with a 120ohm termination resistor.

Using an Arduino connected to a MCP2515 CAN-module with a 120ohm termination resistor.

My code for the Arduino:

Code: Select all

// Include libraries
#include "mcp_can.h"
#include "SPI.h"

// Constant declaration
// Timing constants
const uint32_t timePeriod100ms = 100; // delay, 100ms

// I/O-PINS
const uint8_t SPI_CS_PIN = 10; // D10, CS PIN for MCP2515 CAN module

// Function declaration
void sendCAN();

MCP_CAN CAN(SPI_CS_PIN);	// Set CS pin


/********
* SETUP *
********/
void setup()
{
  while (CAN_OK != CAN.begin(MCP_STDEXT, CAN_250KBPS, MCP_8MHZ))		// init can bus : baudrate = 250k
  {
    delay(100);
  }

  CAN.setMode(MCP_NORMAL);  // Set operation mode to normal so the MCP2515 sends acks to received data.
}


/*******
* LOOP *
*******/
void loop()
{
	static uint32_t lastMillisCAN = millis();
  
  if (millis() - lastMillisCAN >= timePeriod100ms) // test if the period has elapsed
  {
    lastMillisCAN = millis(); // reset timer
    
    uint8_t dataToSend[8];
    
    //uint16_t pot1 = analogRead(A3); // Read accelerator pot1
    uint16_t pot1 = 576; // Read accelerator pot1
        
    dataToSend[0] = lowByte(pot1); // Pot1 value to send
    dataToSend[1] = highByte(pot1); // Pot1 value to send
    dataToSend[2] = 0x00; // Empty
    dataToSend[3] = 0x00; // Empty
    dataToSend[4] = 0x00; // Empty
    dataToSend[5] = 0x00; // Empty
    dataToSend[6] = 0x00; // Empty
    dataToSend[7] = 0x00; // Empty
          
    CAN.sendMsgBuf(100, 0, 8, dataToSend); // Send data
  }
}
User avatar
Peter
Posts: 310
Joined: Fri Dec 14, 2018 9:07 pm
Location: North West Lancs, UK
Been thanked: 8 times

Re: Send CAN message help

Post by Peter »

Hi bexander. Thanks for the help here. Just as a matter of interest could we have just two bytes [0] and [1] ? (100, 0, 2, dataToSend) ?? So therefore setting Spot Value as pot 100 0 2 32 ? I can see an error I made, I have not set the last parameter to 32, need to do that later. Thanks. Peter
User avatar
bexander
Posts: 835
Joined: Tue Jun 16, 2020 6:00 pm
Location: Gothenburg, Sweden
Has thanked: 63 times
Been thanked: 89 times

Re: Send CAN message help

Post by bexander »

Yes, it is possible to send only two bytes using (100, 0, 2, dataToSend).
The question is what the OpenInverter expects to receive and if it can handle receiving only two bytes?
As I understand it the Spot Value should still be 100 0 16 32. The 0 is the start bit of the data and 16 is the number of bits to read. 16 bits equals 2 bytes.
User avatar
Peter
Posts: 310
Joined: Fri Dec 14, 2018 9:07 pm
Location: North West Lancs, UK
Been thanked: 8 times

Re: Send CAN message help

Post by Peter »

Hi bexander. Thank you for your help. I can now send data to my inverter, two bytes which works fine too. Peter
User avatar
bexander
Posts: 835
Joined: Tue Jun 16, 2020 6:00 pm
Location: Gothenburg, Sweden
Has thanked: 63 times
Been thanked: 89 times

Re: Send CAN message help

Post by bexander »

No problem, glad to hear it is working!
User avatar
Peter
Posts: 310
Joined: Fri Dec 14, 2018 9:07 pm
Location: North West Lancs, UK
Been thanked: 8 times

Re: Send CAN message help

Post by Peter »

Hi bexander. I checked boostcalc in spot values on Auto Repeat whilst constantly sending the CAN message and it switches between the value I am sending and the fixed value in parameters repeatedly. Dont know if that is actually happening under power yet.
User avatar
bexander
Posts: 835
Joined: Tue Jun 16, 2020 6:00 pm
Location: Gothenburg, Sweden
Has thanked: 63 times
Been thanked: 89 times

Re: Send CAN message help

Post by bexander »

I'm not sure I fully understand.
Are you sending values of "boostcalc" or "boost" via CAN?
My interpretation is that "boostcalc" value is calculated from parameter "boost" and the DC link voltage "udc".
I think you should send values of boost to the inverter in order to change boostcalc.
User avatar
Peter
Posts: 310
Joined: Fri Dec 14, 2018 9:07 pm
Location: North West Lancs, UK
Been thanked: 8 times

Re: Send CAN message help

Post by Peter »

In the spot value section there is only boostcalc (unless I am missing something). Not sure how to actually input data to the Boost itself. Udc is set to 0 too. I have tried using fweakcalc and that does the same. Was wondering if I need to set 0 or try not setting anything in the Boost parameter section to see if that tells the inverter to look only at the CAN value ?
User avatar
bexander
Posts: 835
Joined: Tue Jun 16, 2020 6:00 pm
Location: Gothenburg, Sweden
Has thanked: 63 times
Been thanked: 89 times

Re: Send CAN message help

Post by bexander »

Look under the section "Setting and reading parameters via SDO" on this page
https://openinverter.org/wiki/CAN_commu ... rs_via_SDO

I have not tested this but if you start by doing a list-command in the web interface and then calculates the position, from the top, of boost. Also note that the top position is 0.

Then on CAN:
Id: 0x601
Message: (0x40, 0x00, 0x20, 0x00, 0x80, 0x0C, 0, 0) Set "boost" to 100 (0xC80=3200 because scaled by 32)
0x00 should be replaced with the number derived from the list command, in hex, or mabye dec will work as well?
User avatar
bexander
Posts: 835
Joined: Tue Jun 16, 2020 6:00 pm
Location: Gothenburg, Sweden
Has thanked: 63 times
Been thanked: 89 times

Re: Send CAN message help

Post by bexander »

I got the above to work. Boost was at the top, so position 0.
jon volk
Posts: 572
Joined: Wed Apr 10, 2019 7:47 pm
Location: Connecticut
Been thanked: 2 times

Re: Send CAN message help

Post by jon volk »

Question regarding parameter "count" for CAN messages

For round numbers say you have 5 parameters, then flag one hidden. Is the hidden parameter is now exempt from the count?

i.e. If you flag parameter 2 as hidden, does parameter 4 then become parameter 3?
Formerly 92 E30 BMW Cabrio with Tesla power
User avatar
johu
Site Admin
Posts: 5778
Joined: Thu Nov 08, 2018 10:52 pm
Location: Kassel/Germany
Has thanked: 157 times
Been thanked: 1018 times
Contact:

Re: Send CAN message help

Post by johu »

No the hidden flag does not affect the index.
Support R/D and forum on Patreon: https://patreon.com/openinverter - Subscribe on odysee: https://odysee.com/@openinverter:9
jon volk
Posts: 572
Joined: Wed Apr 10, 2019 7:47 pm
Location: Connecticut
Been thanked: 2 times

Re: Send CAN message help

Post by jon volk »

Hmmm, I’ll mess with it some more then. I have no problems modifying boost and fweak over CAN, but seemingly went off the rails moving down the list to throtramp.
Formerly 92 E30 BMW Cabrio with Tesla power
jon volk
Posts: 572
Joined: Wed Apr 10, 2019 7:47 pm
Location: Connecticut
Been thanked: 2 times

Re: Send CAN message help

Post by jon volk »

So, I managed to get my throtramp value changing over CAN, however, I can't make any sense of the index.

Based on the below by entering command "list", throtramp should have an index value of 31. (index values added in excel)

Code: Select all

Ind    Param
0	boost	
1	fweak [Hz]	
2	fconst [Hz]	
3	udcnom [V]	
4	fslipmin [Hz]	
5	fslipmax [Hz]	
6	fslipconstmax [Hz]	
7	fmin [Hz]	
8	fmax [Hz]	
9	numimp [ppr]	
10	ocurlim [A]	
11	minpulse [dig]	
12	udcofs [dig]	
13	udclim [V]	
14	udcmin [V]	
15	udcmax [V]	
16	iacmax [A]	
17	idcmax [A]	
18	idcmin [A]	
19	tmphsmax [°C]	
20	tmpmmax [°C]	
21	throtmax [%]	
22	throtmin [%]	
23	ifltrise [dig]	
24	ifltfall [dig]	
25	chargeki [dig]	
26	potmin [dig]	
27	potmax [dig]	
28	pot2min [dig]	
29	pot2max [dig]	
30	potmode [0=SingleRegen, 1=DualChannel, 2=CAN]	
31	throtramp [%/10ms]	
However, when I send the following CAN message to this index, it changed iacmax, which should be index of 16.

Code: Select all

msg.id = 0x601; //set parameter ID
        msg.len = 8;
        msg.buf[0] = 0x40; //CMD
        msg.buf[1] = 0x00;
        msg.buf[2] = 0x20; 
        msg.buf[3] = 0x1F;//index:boost=0, count down for index number
        msg.buf[4] = 0xE0;
        msg.buf[5] = 0x01;//value x 32
        msg.buf[6] = 0x00;
        msg.buf[7] = 0x00;
        Can0.write(msg);
        
By some trial and error, I was able to arrive at index 46 for throtramp. Is this simply a likely matter of user error on my part that Im not seeing?
The following works correctly, setting the parameter value to 15.

Code: Select all

msg.id = 0x601; //set parameter ID
    msg.len = 8;
    msg.buf[0] = 0x40; //CMD
    msg.buf[1] = 0x00; 
    msg.buf[2] = 0x20; //
    msg.buf[3] = 0x33;//index:boost=0, count down for index number
    msg.buf[4] = 0xE0; //value x 32 == 480 == 0x01E0
    msg.buf[5] = 0x01;
    msg.buf[6] = 0x00;
    msg.buf[7] = 0x00;
    Can0.write(msg);
    
Formerly 92 E30 BMW Cabrio with Tesla power
User avatar
dima
Posts: 157
Joined: Sun Dec 09, 2018 9:35 pm
Location: Canada

Re: Send CAN message help

Post by dima »

I thought index ids came from /include/param_prj.h

Something like "boost = 1" so something like: 1-1 = 0 (DEC) or 0x00 (HEX)? or is this completely off :oops:
User avatar
johu
Site Admin
Posts: 5778
Joined: Thu Nov 08, 2018 10:52 pm
Location: Kassel/Germany
Has thanked: 157 times
Been thanked: 1018 times
Contact:

Re: Send CAN message help

Post by johu »

Sorry, just bumped into this. Yes that what it is. I think on Jons list some parameters are hidden and he doesn't count them.
param_prj.h has become harder to read in that respect because I divided it up in blocks to remove redundancy between foc and sine.
Index 46 sounds about right, I arrived at 49 in the current git version.

Maybe I should include the parameter index in the json command so that it can be displayed in the web interface.
Support R/D and forum on Patreon: https://patreon.com/openinverter - Subscribe on odysee: https://odysee.com/@openinverter:9
User avatar
johu
Site Admin
Posts: 5778
Joined: Thu Nov 08, 2018 10:52 pm
Location: Kassel/Germany
Has thanked: 157 times
Been thanked: 1018 times
Contact:

Re: Send CAN message help

Post by johu »

Alright, some late night hacking: "json" now produces for parameters

Code: Select all

"manualid": {"unit":"A","value":0.00,"isparam":true,"minimum":-400.00,"maximum":400.00,"default":0.00,"category":"Testing","i":73}
Note the added "i".

Added that index before parameter name. Note the skip from 12 to 17 between motor and inverter parameters because of 5 hidden parameters.

Also handling "flag parameters", pinswap being the only one. As soon as multiple flags are selected the parameter is just displayed and can't be modified. Maybe I should just add check boxes instead. Later.
esp8266 github updated.
Attachments
Bildschirmfoto vom 2020-09-07 23-06-16.png
Support R/D and forum on Patreon: https://patreon.com/openinverter - Subscribe on odysee: https://odysee.com/@openinverter:9
jon volk
Posts: 572
Joined: Wed Apr 10, 2019 7:47 pm
Location: Connecticut
Been thanked: 2 times

Re: Send CAN message help

Post by jon volk »

Excellent! I updated the firmware and saw the index value of throtramp was different than my previous version. Quick change in my VCU code and we're off and running. Great addition.
Formerly 92 E30 BMW Cabrio with Tesla power
User avatar
dima
Posts: 157
Joined: Sun Dec 09, 2018 9:35 pm
Location: Canada

Re: Send CAN message help

Post by dima »

Can I ask a bit shifted question, but parameter related ...When using baud rate 921600 in ESP module JSON parameters comes out fragmented (randomly) Is it possible to customize to use slightly lower (lets say 256000) baud rate?
json_921600.png
Post Reply