Hi, I've got the code below which is in C++. How do I convert it to Arduino? I'm mainly troubled by the serial parts - converting byte[] command = new byte[5] etc..
I suspect both 4th Order Ignorance and 2nd Order Ignorance. You are about to be gravely disappointed.
Structured exceptions do not exist in the Arduino world. That line of Java has no equivalent. You will not be able to perform a line-by-line conversion. (There are other troublesome lines in your snippet.)
If you want that code to run on an Arduino you will have to rewrite it.
I just changed that to Serial.println("Error comm..."). Can you explain why a run-time error is a problem. I'm new to Java and in Arduino I haven't really handled run time errors..
hillp16:
Can you explain why a run-time error is a problem. I'm new to Java and in Arduino I haven't really handled run time errors..
Java is pretty much an interpreted language. The JVM is in charge and can can spot runtime errors and cause error events to happen which can be trapped within your Java code.
There is no equivalent to the JVM on an Arduino. If your code does something stupid there is nothing to warn you - you have to plan for problems yourself (such as division by zero).
That code uses a whole lot of external methods. You'd be much better off starting from scratch rather than trying to "convert" this code.
Start with a small program and slowly add the functionality that you want. You could try to understand the Java code to help you implement an Arduino version, but most of the code above is calls to external functions, so it would all be different on the Arduino. Starting from scratch is a better option.
So far I have got the code below together. The values I am printing out give crazy, incorrect values (it thinks the voltage is 639V??), and they are mixed with the error message in the serial terminal.
Please pick out any errors - the code I have so far:
/* CONNECTIONS
CONTROLLER +5V --> SERIAL LINK +5V
CONTROLLER SIGNAL --> SERIAL LINK SIGNAL
CONTROLLER GND --> SERIAL LINK GND
SERIAL LINK +BUS --> ARDUINO +5V
SERIAL LINK TX --> ARDUINO RX
SERIAL LINK RX --> ARDUINO TX
SERIAL LINK GND --> ARDUINO GND
*/
/* DEFINITIONS */
/* FLOATS */
byte command[5];
byte response[3];
/* INTEGERS */
int deviceId = 0x17;
int throttleOut = 0;
int voltage = 0;
int rippleVoltage = 0;
int current = 0;
int power = 0;
int throttle = 0;
int pwm = 0;
int rpm = 0;
int temp = 0;
int becVoltage = 0;
int safeState = 0;
int linkLiveEnabled = 0;
int eStopStatus = 0;
int rawNTC = 0;
/* SETUP */
void setup() {
Serial1.begin(115200);
Serial.begin(115200);
}
void loop() {
flushPort();
ReadWriteRegister(128, throttleOut, true);//_throttleOut is 0[0%] to 65535[100%]
voltage = ReadWriteRegister(0, 0, false) / 2042.0 / 0.05;
rippleVoltage = ReadWriteRegister(1, 0, false) / 2042 / 0.25;
current = ReadWriteRegister(2, 0, false) / 2042.0 / 0.02;
power = voltage * current;
throttle = (ReadWriteRegister(3, 0, false) / 2042.0 / 1.0);
pwm = ReadWriteRegister(4, 0, false) / 2042.0 / 3.996735;
rpm = ReadWriteRegister(5, 0, false) / 2042.0 / 4.89796E-5;
double poleCount = 4;//Motor pole count
rpm = rpm / (poleCount / 2);
temp = ReadWriteRegister(6, 0, false) / 2042.0 * 30.0;
becVoltage = ReadWriteRegister(7, 0, false) / 2042 / 0.25;
safeState = ReadWriteRegister(26, 0, false);
linkLiveEnabled = ReadWriteRegister(25, 0, false);
eStopStatus = ReadWriteRegister(27, 0, false) == 0 ? false : true;
rawNTC = ReadWriteRegister(9, 0, false) / 2042.0 / 0.01567091;
rawNTC = 1.0 / (log(rawNTC * 10200.0 / (255.0 - rawNTC) / 10000.0) / 3455.0 + 1.0 / 298.0) - 273.0;
Serial.print("Voltage: ");
Serial.println(voltage);
Serial.print("Ripple Voltage: ");
Serial.println(rippleVoltage);
Serial.print("Current: ");
Serial.println(current);
}
void flushPort() {
command[0] = command[1] = command[2] = command[3] = command[4] = 0;
Serial1.write(command, 5);
while (Serial1.available() > 0) {
Serial1.read();
}
}
uint16_t ReadWriteRegister(int reg, int value, bool writeMode) {
// Send read command
command[0] = (byte)(0x80 | deviceId);
command[1] = (byte)reg;
command[2] = (byte)((value >> 8) & 0xFF);
command[3] = (byte)(value & 0xFF);
command[4] = (byte)(0 - command[0] - command[1] - command[2] - command[3]);
Serial1.write(command, 5);
// Read response
response[0] = (byte)Serial1.read();
response[1] = (byte)Serial1.read();
response[2] = (byte)Serial1.read();
if ((byte)(response[0] + response[1] + response[2]) == 0)
{
return (double)((response[0] << 8) + (response[1]));
}
else
{
Serial.println("Error communicating with device!");
}
}