I have the Makeblock Ultimate 2.0 kit which uses the Arduino MegaPi board. There is an Arduino PID example. But, there is no explanation of what the commands are and what the numbers are. Is there a manual or tutorial of something? Thanks in advance.
The example program follows:
/**
* \par Copyright (C), 2012-2016, MakeBlock
* #file Me_Megapi_encoder_pid_speed_MAM.ino
* #author MakeBlock
* #version V1.0.0
* #date 11/27/2016
* #brief Description: this file is sample code for Megapi encoder motor device.
*
* Function Call List:
* 1. uint8_t MeEncoderOnBoard::getPortB(void);
* 2. uint8_t MeEncoderOnBoard::getIntNum(void);
* 3. void MeEncoderOnBoard::pulsePosPlus(void);
* 4. void MeEncoderOnBoard::pulsePosMinus(void);
* 5. void MeEncoderOnBoard::setMotorPwm(int pwm);
* 6. double MeEncoderOnBoard::getCurrentSpeed(void);
* 7. void MeEncoderOnBoard::setSpeedPid(float p,float i,float d);
* 8. void MeEncoderOnBoard::setPosPid(float p,float i,float d);
* 7. void MeEncoderOnBoard::setPosPid(float p,float i,float d);
* 8. void MeEncoderOnBoard::setPulse(int16_t pulseValue);
* 9. void MeEncoderOnBoard::setRatio(int16_t RatioValue);
* 10. void MeEncoderOnBoard::runSpeed(float speed);
* 11. void MeEncoderOnBoard::loop(void);
*
* \par History:
* <pre>
* <Author> <Time> <Version> <Descr>
* Mark Yan 2016/07/14 1.0.0 build the new
* MAM 10/27/2016 1.0.0 Renamed Me_Megapi_encoder_pid_speed_MAM.ino
* </pre>
*/
#include <MeMegaPi.h>
MeEncoderOnBoard Encoder_1(SLOT1);
MeEncoderOnBoard Encoder_2(SLOT2);
void isr_process_encoder1(void)
{
if(digitalRead(Encoder_1.getPortB()) == 0)
{
Encoder_1.pulsePosMinus();
}
else
{
Encoder_1.pulsePosPlus();;
}
}
void isr_process_encoder2(void)
{
if(digitalRead(Encoder_2.getPortB()) == 0)
{
Encoder_2.pulsePosMinus();
}
else
{
Encoder_2.pulsePosPlus();
}
}
void setup()
{
attachInterrupt(Encoder_1.getIntNum(), isr_process_encoder1, RISING);
attachInterrupt(Encoder_2.getIntNum(), isr_process_encoder2, RISING);
Serial.begin(115200);
//Set PWM 8KHz
TCCR1A = _BV(WGM10);
TCCR1B = _BV(CS11) | _BV(WGM12);
TCCR2A = _BV(WGM21) | _BV(WGM20);
TCCR2B = _BV(CS21);
Encoder_1.setPulse(7);
Encoder_2.setPulse(7);
Encoder_1.setRatio(26.9);
Encoder_2.setRatio(26.9);
Encoder_1.setPosPid(1.8,0,1.2);
Encoder_2.setPosPid(1.8,0,1.2);
Encoder_1.setSpeedPid(0.18,0,0);
Encoder_2.setSpeedPid(0.18,0,0);
}
void loop()
{
if(Serial.available())
{
char a = Serial.read();
switch(a)
{
case '0':
Encoder_1.runSpeed(0);
Encoder_2.runSpeed(0);
break;
case '1':
Encoder_1.runSpeed(100);
Encoder_2.runSpeed(-100);
break;
case '2':
Encoder_1.runSpeed(200);
Encoder_2.runSpeed(-200);
break;
case '3':
Encoder_1.runSpeed(255);
Encoder_2.runSpeed(-255);
break;
case '4':
Encoder_1.runSpeed(-100);
Encoder_2.runSpeed(100);
break;
case '5':
Encoder_1.runSpeed(-200);
Encoder_2.runSpeed(200);
break;
case '6':
Encoder_1.runSpeed(-255);
Encoder_2.runSpeed(255);
break;
default:
break;
}
}
Encoder_1.loop();
Encoder_2.loop();
Serial.print( "Speed 1:\t");
Serial.print(Encoder_1.getCurrentSpeed());
Serial.print(" \t Speed 2:\t");
Serial.println(Encoder_2.getCurrentSpeed());
}
I tried to quickly search if there was anything online that maybe you had missed and I couldn't find much other than the source code for the MeMegaPi library (https://github.com/Makeblock-official/Makeblock-Libraries/tree/master/makeblock/src)
They also refer you back to the MegaBlocks website for more info which I guess you already did. So I'll explain a bit of what's going on here, though for the future, using the API will require you to read the source and that can be a bit daunting if you aren't familiar with C/C++. Anyway here goes:
MeEncoderOnBoard Encoder_1(SLOT1);
MeEncoderOnBoard Encoder_2(SLOT2);
void isr_process_encoder1(void)
First we have the instantiation of the Encoders to SLOT1 and SLOT2 for MeEncoderOnBoard Encoder_1 and MeEncoderOnBoard Encoder_2 respectively. SLOT1 and SLOT2 are aliases for connection 1 and connection 2 (/src/MePort.h) on your device (some kind of multi-pin cable I'd guess). These encoders also encode actions as we will see soon.
void isr_process_encoder1(void)
{
if(digitalRead(Encoder_1.getPortB()) == 0)
{
Encoder_1.pulsePosMinus();
}
else
{
Encoder_1.pulsePosPlus();;
}
}
void isr_process_encoder2(void)
{
if(digitalRead(Encoder_2.getPortB()) == 0)
{
Encoder_2.pulsePosMinus();
}
else
{
Encoder_2.pulsePosPlus();
}
}
Then we make the interrupt routines isr_process_encoder#s 1 and 2 for these encoders. For both we are reading port B (a pin on the connection slot) and performing an increment/decrement (depending on the value on portB) on what they call the pulse position.
void setup()
{
attachInterrupt(Encoder_1.getIntNum(), isr_process_encoder1, RISING);
attachInterrupt(Encoder_2.getIntNum(), isr_process_encoder2, RISING);
Serial.begin(115200);
In setup(), we attach the interrupt routines to the encoders and tell them to trigger on a rising-signal interrupt. After both encoders are set with interrupt handlers, we initiate serial communication on the arduino device with a baud-rate of 115200.
//Set PWM 8KHz
TCCR1A = _BV(WGM10);
TCCR1B = _BV(CS11) | _BV(WGM12);
TCCR2A = _BV(WGM21) | _BV(WGM20);
TCCR2B = _BV(CS21);
The next 4 lines of code is a bit of magic. The reasoning (as the comment suggests) is to set the PWM rate at 8KHz for what I'm assuming is the motors. The _BV is apparently how arduino sets bit values at low-level (making a big guess here)? Not too sure about this actually but I trust the people making this did their homework.
Encoder_1.setPulse(7);
Encoder_2.setPulse(7);
Encoder_1.setRatio(26.9);
Encoder_2.setRatio(26.9);
Encoder_1.setPosPid(1.8,0,1.2);
Encoder_2.setPosPid(1.8,0,1.2);
Encoder_1.setSpeedPid(0.18,0,0);
Encoder_2.setSpeedPid(0.18,0,0);
}
Now... admittedly, I have no idea what the numbers in setPulse, setRatio, setPosPid, or setSpeedPid represent... I hope that some kind of documentation for your PID motor is available because this is magic to me too. However these function calls are just setting these attributes on the Encoder objects (wish I had more to say :[)...
void loop()
{
if(Serial.available())
{
char a = Serial.read();
switch(a)
{
Next we have the main program loop. Starting off, we check if there is a Serial connection we can listen to. If yes, then we read a byte from the serial connection. if the byte we read corresponds to any case, we set the speeds on the Encoders as appropriate.
Encoder_1.loop();
Encoder_2.loop();
After this switch block, we perform the loop() call on our Encoders. According to the source, this is actually an update step where our settings are actually relayed to the device we are communicating with (I've been assuming it's a pair of motors).
Serial.print( "Speed 1:\t");
Serial.print(Encoder_1.getCurrentSpeed());
Serial.print(" \t Speed 2:\t");
Serial.println(Encoder_2.getCurrentSpeed());
}
Finally, we print out the speeds of the motors and then loop routine continues to do this [Read->Update->Print] cycle
Sorry I can't be more help with this, but this is all I can give you. good luck and I hope you can find documentation! I'm back off to work in my boring non-embedded programming job ;)
Related
I have only put in one 'print command' however I get two print reads.
The programme drives two stepper motors.
The moveSteps value = 48
The motor stops briefly and print's '48' when the programme starts to run and then '48' again when the if is triggered, just before the programme ends.
Only one '48' should be printed. Any ideas why this happens?
/*
Precise movement with stop
Moves the robot 20mm forwards and 20mm backwards
Rob Miles (edited by Dileepa Ranawake)
April 2017
Version 1.0
*/
int motorDelay;
byte left1,left2,left3,left4;
byte right1,right2,right3,right4;
float wheelDiameter = 68.5;
float stepsPerRevolution = 512;
float mmsPerStep = (wheelDiameter * 3.1416) / stepsPerRevolution;
int moveCount;
int moveSteps; // number of steps the motor is to move
void leftForwards()
{
left1=7; left2=6; left3=5; left4=4;
}
void leftReverse()
{
left1=4; left2=5; left3=6; left4=7;
}
void rightForwards()
{
right1=8; right2=9; right3=10; right4=11;
}
void rightReverse()
{
right1=11; right2=10; right3=9; right4=8;
}
int calculateDistanceSteps(float distanceInMM)
{
return distanceInMM / mmsPerStep + 0.5;
}
void setup() {
leftForwards();
rightForwards();
pinMode(left1,OUTPUT);
pinMode(left2,OUTPUT);
pinMode(left3,OUTPUT);
pinMode(left4,OUTPUT);
digitalWrite(left1,HIGH);
pinMode(right1,OUTPUT);
pinMode(right2,OUTPUT);
pinMode(right3,OUTPUT);
pinMode(right4,OUTPUT);
digitalWrite(right1,HIGH);
motorDelay=1200;
moveCount=0;
moveSteps = calculateDistanceSteps(20);
Serial.begin(9800);
}
void loop() {
moveCount = moveCount + 1;
if (moveCount==moveSteps)
{
digitalWrite(left1,LOW);
digitalWrite(right1,LOW);
Serial.println(moveCount);
exit(0);
}
digitalWrite(left2,HIGH);
digitalWrite(right2,HIGH);
delayMicroseconds(motorDelay);
digitalWrite(left1,LOW);
digitalWrite(right1,LOW);
delayMicroseconds(motorDelay);
digitalWrite(left3,HIGH);
digitalWrite(right3,HIGH);
delayMicroseconds(motorDelay);
digitalWrite(left2,LOW);
digitalWrite(right2,LOW);
delayMicroseconds(motorDelay);
digitalWrite(left4,HIGH);
digitalWrite(right4,HIGH);
delayMicroseconds(motorDelay);
digitalWrite(left3,LOW);
digitalWrite(right3,LOW);
delayMicroseconds(motorDelay);
digitalWrite(left1,HIGH);
digitalWrite(right1,HIGH);
delayMicroseconds(motorDelay);
digitalWrite(left4,LOW);
digitalWrite(right4,LOW);
delayMicroseconds(motorDelay);
}
Serial Monitor prints out 4848
I have also noticed that just opening serial monitor makes the stepper motor move!
Using exit() with an Arduino is not standard. It basically disables all interrupts and enters an infinite loop. You could restructure your loop() like this to avoid it:
void loop()
{
// Still moving?
if (moveCount < moveSteps) {
moveCount = moveCount + 1;
// Move complete
if (moveCount == moveSteps)
{
digitalWrite(left1,LOW);
digitalWrite(right1,LOW);
Serial.println(moveCount);
}
else {
digitalWrite(left2,HIGH);
digitalWrite(right2,HIGH);
//etc.....
}
}
}
Also, your loop delays 1200µs 8x. That's only 1200 × 8 = 9600 µs = 9.6 ms. If moveSteps = 48 then the entire loop will only take 460.8 ms. The program is running once before you open the serial monitor then a second time after. What happens if you push the reset button after you've opened the Serial Monitor?
Have you considered using the Arduino's built in Stepper Library?
Lastly, in the future, consider posting questions like this at [arduino.se].
So, I am an amateur programmer at Arduino, and have never used Arduino MKR1000 before. I used an Arduino Uno and wrote the attached code that detects heart beat and temperature using the Grove Ear clip heart beat sensor and the Grove Temperature sensor and then prints them in the console every 20 seconds. Previously, this code was written to show in an Grove OLED screen but later simplified it back to using it by reading on just the console.
Because of the wearable nature of my project I have to switch to using a MKR1000 instead. I know the MKR1000 uses the same Arduino code and should work the same way as it did on my Arduino Uno, but I have been having some issues with using the MKR1000 with the same code.
The issue is that the code only runs once and stops after that. While I am aware of the for loops and how it works to certain extent, I cannot find the exact issue why it stops looping instead of taking the data constantly and publishing it on the console, like it did previously with my Uno.
Just for the heads up, following is how my code reacted to Arduino Uno:
The result in the console shows:
Please be ready
This will now begin
then it prints number 1 to 20 every second followed by sensor readings. After publishing this, it repeats this process again.
Again sorry for the inconvenience and thank you for your help.
I used direct codes from the documentation blog of the sensors (bottom of the page of the linked page):
Grove heart bear sensor
Grove Temperature sensor
#define LED 4//indicator, Grove - LED is connected with D4 of Arduino
boolean led_state = LOW;//state of LED, each time an external interrupt
//will change the state of LED
float tempa;
int tempPin = 0;
unsigned char counter;
unsigned long temp[21];
unsigned long sub;
bool data_effect=true;
unsigned int heart_rate;//the measurement result of heart rate
const int max_heartpluse_duty = 2000;//you can change it follow your system's request.
//2000 meams 2 seconds. System return error
//if the duty overtrip 2 second.
void setup()
{
pinMode(LED, OUTPUT);
Serial.begin(9600);
while (!Serial){
;
}
Serial.println("Please be ready");
delay(5000);
arrayInit();
Serial.println("This will now begin.");
attachInterrupt(0, interrupt, RISING);//set interrupt 0,digital port 2
}
void loop()
{
digitalWrite(LED, led_state);//Update the state of the indicator
}
/*Function: calculate the heart rate*/
void sum()
{
if(data_effect)
{
heart_rate=1200000/(temp[20]-temp[0]);//60*20*1000/20_total_time
Serial.print("Heart_rate_is:\t");
Serial.println(heart_rate);
tempa = analogRead(tempPin);
tempa = tempa * 0.11;
Serial.print("Body Temperature = ");
Serial.print(tempa);
Serial.print("*C");
Serial.println();
delay(1000);
}
data_effect=1;//sign bit
}
/*Function: Interrupt service routine.Get the sigal from the external interrupt*/
void interrupt()
{
temp[counter]=millis();
Serial.println(counter,DEC);
switch(counter)
{
case 0:
sub=temp[counter]-temp[20];
break;
default:
sub=temp[counter]-temp[counter-1];
break;
}
if(sub>max_heartpluse_duty)//set 2 seconds as max heart pluse duty
{
data_effect=0;//sign bit
counter=0;
Serial.println("measurement error,test will restart!" );
arrayInit();
}
else if (counter==20&&data_effect)
{
counter=0;
sum();
}
else if(counter!=20&&data_effect)
{
counter++;
}
else
{
counter=0;
data_effect=1;
}
}
/*Function: Initialization for the array(temp)*/
void arrayInit()
{
for(unsigned char i=0;i < 20;i ++)
{
temp[i]=0;
}
temp[20]=millis();
}
Your problem is the interrupt pin. On the Arduino UNO the digital pin D2 is interrupt pin 0, as you have done in your code. On the Arduino MKR1000 the interrupt pin is tha same as the physical pin number, so if you'r connecting to pin 2, change attachInterrupt(0, interrupt, RISING); to attachInterrupt(2, interrupt, RISING);
If you wanna be sure to use the right pin you can use digitalPinToInterrupt(pin) to get the correct interrupt number on any Arduino board. Like this attachInterrupt(digitalPinToInterrupt(pin), interrupt, RISING); where pin is the physical pin number.
Info
On the Arduino MKR1000 you can use pin 0, 1, 4, 5, 6, 7, 8, 9, A1 and A2 as interrupt
Please bear with me. I am an amateur programmer at Arduino, and have never used Particle photon before.
I used an Arduino Uno and wrote the attached code that detects heart beat and temperature using the Grove Ear clip heart beat sensor and the Grove Temperature sensor and then prints them in the console every 20 seconds. Previously, this code was written to show in an Grove OLED screen but later simplified it back to using it without the OLED screen.
I am now looking into using the same application and function using the same sensor on a Particle Photon (as it is smaller and has WiFi capability). I have never previously used this technology before but I saw online that it, more or less, uses the same code application. I have been through the sample codes for this online available on its website but I have no idea how to convert my code for the Arduino into code for the photon (Photon console compiles the code without any error but does not show any sensor data). Can someone either point me to the right direction/ appropriate online resources / help me change this code here to make it work on the Photon? (I just added the Particle.publish() wherever I was using Arduino's Serial.println() but it still doesn't print anything).
The result in the console shows:
Please be ready
This will now begin
then it prints number 1 to 20 every second followed by sensor readings.
Again sorry for the inconvenience and thank you for your help.
I used direct codes from the documentation blog of the sensors (bottom of the page of the linked page):
Grove heart bear sensor
Grove Temperature sensor
#define LED 4//indicator, Grove - LED is connected with D4 of Arduino
boolean led_state = LOW;//state of LED, each time an external interrupt
//will change the state of LED
float tempa;
int tempPin = 0;
unsigned char counter;
unsigned long temp[21];
unsigned long sub;
bool data_effect=true;
unsigned int heart_rate;//the measurement result of heart rate
const int max_heartpluse_duty = 2000;//you can change it follow your system's request.
//2000 meams 2 seconds. System return error
//if the duty overtrip 2 second.
void setup()
{
pinMode(LED, OUTPUT);
Serial.begin(9600);
while (!Serial){
;
}
Serial.println("Please be ready");
delay(5000);
arrayInit();
Serial.println("This will now begin.");
attachInterrupt(0, interrupt, RISING);//set interrupt 0,digital port 2
}
void loop()
{
digitalWrite(LED, led_state);//Update the state of the indicator
}
/*Function: calculate the heart rate*/
void sum()
{
if(data_effect)
{
heart_rate=1200000/(temp[20]-temp[0]);//60*20*1000/20_total_time
Serial.print("Heart_rate_is:\t");
Serial.println(heart_rate);
tempa = analogRead(tempPin);
tempa = tempa * 0.11;
Serial.print("Body Temperature = ");
Serial.print(tempa);
Serial.print("*C");
Serial.println();
delay(1000);
}
data_effect=1;//sign bit
}
/*Function: Interrupt service routine.Get the sigal from the external interrupt*/
void interrupt()
{
temp[counter]=millis();
Serial.println(counter,DEC);
switch(counter)
{
case 0:
sub=temp[counter]-temp[20];
break;
default:
sub=temp[counter]-temp[counter-1];
break;
}
if(sub>max_heartpluse_duty)//set 2 seconds as max heart pluse duty
{
data_effect=0;//sign bit
counter=0;
Serial.println("measurement error,test will restart!" );
arrayInit();
}
else if (counter==20&&data_effect)
{
counter=0;
sum();
}
else if(counter!=20&&data_effect)
{
counter++;
}
else
{
counter=0;
data_effect=1;
}
}
/*Function: Initialization for the array(temp)*/
void arrayInit()
{
for(unsigned char i=0;i < 20;i ++)
{
temp[i]=0;
}
temp[20]=millis();
}
I'm working on a project that collects data from an Arduino Pro Mini and sends it using SPI to a raspberry Pi for storage.
The Pro Mini will be reading analog input and calculating voltage (once I finish), and passing values to the Pi when prompted using an ISR.
I'm using C/C++ for both platforms to keep it uniform. The slave code is snipped together using Arduino IDE and the master code is based off a BCM2835 SPI library example for the Pi.
Arduino code is meant to calculate a float value and pre-process the float value into an array of 4 bytes/chars (I'm shooting for binary because I think it is the best way to go).
Once prompted by the Pi, each byte is sent and will be recompiled into a float.
Here is what I have now:
Slave
/*************************************************************
ARDUINO BREAKER READ/SPI PRE-PROC/TRANSMIT CASES
****************************************************************/
/***************************************************************
Global Variables
***************************************************************/
byte command = 0; //command from PI
byte bytes[4]; //
int sensorVoltage, sensorCurrent; //eventual live reading vars
float Voltage, Current, RealCurrent, RealVoltage, Power;
/***************************************************************
Set Up
-designate arudino as slave
-turn on interrupts
***************************************************************/
void setup (void)
{
//debugging with serial monitor
Serial.begin(9600);
// Set up arduino as slave
pinMode(MOSI, INPUT);
pinMode(SCK, INPUT);
pinMode(SS, INPUT);
pinMode(MISO, OUTPUT);
// turn on SPI in slave mode
SPCR |= _BV(SPE);
// turn on interrupts
SPCR |= _BV(SPIE);
} // end of setup
/*************************************************************
Interrupt Service Routine
************************************************************/
// SPI interrupt routine
ISR (SPI_STC_vect)
{
delay(500); //for errors
// Create union of shared memory space
union
{
float f_var;
unsigned char bytes[4];
} u;
// Overwrite bytes of union with float variable
u.f_var = RealVoltage;
// Assign bytes to input array
memcpy(bytes, u.bytes, 4);
byte c = SPDR;
command = c;
switch (command)
{
// null command zeroes register
case 0:
SPDR = 0;
break;
// case a - d reserved for voltage
case 'a':
SPDR = bytes[3];
break;
// incoming byte, return byte result
case 'b':
SPDR = bytes[2];
break;
// incoming byte, return byte result
case 'c':
SPDR = bytes[1];
break;
// incoming byte, return byte result
case 'd':
SPDR = bytes[0];
break;
/** // case e -h reserved for current
case 'e':
SPDR = amps.b[0];
break;
// incoming byte, return byte result
case 'f':
SPDR = amps.b[1];
break;
// incoming byte, return byte result
case 'g':
SPDR = amps.b[2];
break;
// incoming byte, return byte result
case 'h':
SPDR = amps.b[3];
break;
// case i - l reserved for wattage
case 'i':
SPDR = watts.b[0];
break;
// incoming byte, return byte result
case 'j':
SPDR = watts.b[1];
break;
// incoming byte, return byte result
case 'k':
SPDR = watts.b[2];
break;
// incoming byte, return byte result
case 'l':
SPDR = watts.b[3];
break;**/
} // end of switch
} // end of interrupt service routine (ISR) SPI_STC_vect
/***************************************************************
Loop until slave is enabled by Pi.
****************************************************************/
void loop (void)
{
/*************************************************************
Read and Calculate
****************************************************************/
/**
sensorVoltage = analogRead(A2);
sensorCurrent = analogRead(A3);
Voltage = sensorVoltage*(5.0/1023.0);
Current = sensorCurrent*(5.0/1023.0);
RealCurrent = Current/0.204545;
RealVoltage = (Voltage/0.022005);
Power = RealVoltage*RealCurrent;
**/
RealVoltage = 1.234;
/*************************************************************
Loop Check for SS activation
****************************************************************/
// if SPI not active, clear current command, else preproc floats and pass to SPI
if (digitalRead (SS) == HIGH){
command = 0;
}
/*************************************************************
Debug with serial monitor
****************************************************************/
/*
Serial.print("Byte 3: ");
Serial.println(bytes[3],BIN);
delay(500);
Serial.print("Byte 2: ");
Serial.println(bytes[2],BIN);
delay(500);
Serial.print("Byte 1: ");
Serial.println(bytes[1],BIN);
delay(500);
Serial.print("Byte 0: ");
Serial.println(bytes[0],BIN);
delay(1000);
Serial.println();*/
}
Master
#include <bcm2835.h>
#include <stdio.h>
void setup()
{
bcm2835_spi_begin();
bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_LSBFIRST); // The default
bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); // The default
bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536); // The default
bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // the default
}
char getByte(const char command){
char read_data = bcm2835_spi_transfer(command);
delay(100);
return read_data;
}
int main(int argc, char **argv)
{
//If you call this, it will not actually access the GPIO
//Use for testing
//bcm2835_set_debug(1);
if (!bcm2835_init())
return 1;
setup();
//Start communication
bcm2835_spi_chipSelect(BCM2835_SPI_CS0);// Enable 0
//voltage 1-4
char read_data = getByte('a');
printf("byte is %02d\n", read_data);
read_data = getByte('b');
printf("byte is %02d\n", read_data);
read_data = getByte('c');
printf("byte is %02d\n", read_data);
read_data = getByte('d');
printf("byte is %02d\n", read_data);
/** voltage = volts.f;
printf("%.6f", voltage);
printf("\n");
**/
delay(1000);
bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, HIGH);
bcm2835_spi_chipSelect(BCM2835_SPI_CS0);// Disable 0
bcm2835_spi_end();
bcm2835_close();
return 0;
}
I'm using a fixed value to debug the code. Sometimes the output from SPI on the Pi is accurate, but otherwise it changes and outputs partially accurate and/or random bytes.
The issue I have been unable to work out is stability on the side of the Pi, so I am looking for help evaluating whether my code is causing inaccuracies or if it's my hardware.
At a guess I would say that this is a timing issue between the sender and receiver. Try looking at the bits of the data that you are receiving and see if they are shifted forward or backward. This will possibly indicate that the pi is waiting too long to begin receiving, or not waiting long enough for all of the data. I think the issues are probably around the delays:
delay(500); //for errors
on the Arduino, and
delay(1000);
on the receiver. Why are you using these? 500ms is a long time to keep the pi waiting for a response to the SPI data.
Just a note, there is also now an SPI kernel driver (spidev) for the pi - this is a much more industry standard approach, and is potentially a more robust method.
There's a good example of this on the raspberry pi github site: https://github.com/raspberrypi/linux/blob/rpi-4.4.y/Documentation/spi/spidev_test.c
As usual, I know how to bypass this problem with some ugly patch work, but I want to make it elegant: I would like to make a small wrapper for motors commanded by an Arduino, which would unfortunately mean writing a distinct interrupt routine for each instance of motor because it has to modify the right step counter (member variable of the motor class) to determine its rate. However those functions would obviously have the same processing...
My question is: how can I determine which counter to modify in a unique interrupt routine?
Here is what I have so far, I'd like to hide interrupts from the user.
class Motor {
volatile int counter;
unsigned long lastUpdate; //Last timestamp update (for calculating the rate)
static const unsigned int RESOLUTION = 1024; //Number of steps in one rev
static const unsigned int RPMS_TO_RPM = 60000; //Convers rev/ms to rpm
public:
Motor() : counter(0)
{
lastUpdate = millis();
}
void encoderInput(bool pinA, bool pinB)
{
counter += (pinA ^ pinB)*(-1)+!(pinA ^ pinB);
}
int getRate() {
int ret = float(counter)/RESOLUTION/(millis() - lastUpdate)*RPMS_TO_RPM;
lastUpdate = millis();
counter = 0;
return ret;
}
};
/* Example:
* Motor motor1;
*
* void motor1_isr(void) {
* motor1.encoderInput(PIN_A, PIN_B);
* }
*
* void setup() {
* attachInterrupt(PIN_I, motor1_isr, CHANGE);
* Serial.begin(9600);
* }
*
* void loop() {
* Serial.println(motor1.getRate());
* delay(1000);
* }
*/
Thanks for your help, I think it would be useful to other people as well once it's done with :)
Regards,
Mister Mystère
You are facing a fundamental problem: an ISR is called with no data. In other words, the ISR function is not provided any data that indicates its source of the interrupt.
The basic approach is that the person writing the code provides the linkage between input and action by hardcoding a function. The tricky approach you are looking for is a method by which the ISR can figure out what the source was.
So you want to have N motors with 2N inputs from quadrature encoders. A single interrupt handler is attached to all the input pins on a CHANGE condition. The same handler gets called for all N motors. It can figure out which Motor to update by comparing the input pins to the values the last time it was called. If the input pins changed, then call that motor. Here is a psuedo code
Motor motor1;
Motor motor2;
onSomethingChanged() {
static int in1aprev, in1bprev;
static int in2aprev, in2bprev;
int in1a, in1b;
int in2a, in2b;
in1a = digitalRead(....
same for other in's
if( (in1a!=in1alast) || (in1b!=in1blast)) {
motor1.encoderInput(in1a,in1b);
in1alast = in1a;
in1blast = in1b;
}
if( (in2a!=in2alast) || (in2b!=in1blast)) {
motor2.encoderInput(in2a,in2b);
in1a2ast = in2a;
in1b2ast = in2b;
}
return;
}
Not so long ago this type of function would be handled in an entire chip (see programmable interrupt controller ). The chip implements all the logic to trigger and capture the source of the interrupt. The main CPU just gets a trigger "something happened". The handler polls the chip to ask "what happened".
Having offered this method, I'm not sure I would recommend. You cannot hide what is going on with your code. The motor must be physically wired to the correct pins. You have consumed a scarce resource -- you have to tell people.