Page 1 of 2
Send CAN message help
Posted: Sat Jul 18, 2020 9:31 pm
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
}
Re: Send CAN message help
Posted: Sun Jul 19, 2020 1:44 am
by mdrobnak
You shouldn't have a loop in setup(), it's probably getting stuck there.
-Matt
Re: Send CAN message help
Posted: Sun Jul 19, 2020 1:54 am
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
Re: Send CAN message help
Posted: Sun Jul 19, 2020 5:29 am
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?
Re: Send CAN message help
Posted: Sun Jul 19, 2020 9:54 am
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
Re: Send CAN message help
Posted: Sun Jul 19, 2020 10:20 am
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.
Re: Send CAN message help
Posted: Sun Jul 19, 2020 12:16 pm
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
}
}
Re: Send CAN message help
Posted: Sun Jul 19, 2020 2:02 pm
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
Re: Send CAN message help
Posted: Sun Jul 19, 2020 2:30 pm
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.
Re: Send CAN message help
Posted: Mon Jul 20, 2020 9:18 am
by Peter
Hi bexander. Thank you for your help. I can now send data to my inverter, two bytes which works fine too. Peter
Re: Send CAN message help
Posted: Mon Jul 20, 2020 1:05 pm
by bexander
No problem, glad to hear it is working!
Re: Send CAN message help
Posted: Tue Jul 21, 2020 8:48 am
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.
Re: Send CAN message help
Posted: Tue Jul 21, 2020 9:39 am
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.
Re: Send CAN message help
Posted: Tue Jul 21, 2020 9:48 am
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 ?
Re: Send CAN message help
Posted: Tue Jul 21, 2020 11:13 am
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?
Re: Send CAN message help
Posted: Tue Jul 21, 2020 6:18 pm
by bexander
I got the above to work. Boost was at the top, so position 0.
Re: Send CAN message help
Posted: Wed Aug 19, 2020 12:47 am
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?
Re: Send CAN message help
Posted: Wed Aug 19, 2020 6:26 am
by johu
No the hidden flag does not affect the index.
Re: Send CAN message help
Posted: Wed Aug 19, 2020 9:46 am
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.
Re: Send CAN message help
Posted: Thu Aug 20, 2020 1:03 am
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);
Re: Send CAN message help
Posted: Mon Sep 07, 2020 8:24 pm
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
Re: Send CAN message help
Posted: Mon Sep 07, 2020 8:40 pm
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.
Re: Send CAN message help
Posted: Mon Sep 07, 2020 9:08 pm
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.
Re: Send CAN message help
Posted: Tue Sep 08, 2020 6:21 pm
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.
Re: Send CAN message help
Posted: Fri Sep 18, 2020 1:31 am
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?